Skip to content

Commit 9b1151b

Browse files
committed
🚧 Json To HTML
- Node to Metadata - Metadata test case added - Refactor Metadata attribute - Reference To Html functionality completed
1 parent 4dc21f0 commit 9b1151b

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

Contentstack.Utils/Models/Metadata.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public struct Metadata
3636
/// <summary>
3737
/// Attributes collection for embed tag
3838
/// </summary>
39-
public HtmlAttributeCollection attributes;
39+
public object attributes;
4040

4141
/// <summary>
4242
/// Html string of embed tag
@@ -67,5 +67,45 @@ public static implicit operator Metadata(HtmlNode node)
6767
attributes = node.Attributes
6868
};
6969
}
70+
71+
public static implicit operator Metadata(Node node)
72+
{
73+
StyleType styleType;
74+
if (!node.attrs.ContainsKey("display-type") || !(Enum.TryParse((string)node.attrs["display-type"], true, out styleType)))
75+
{
76+
styleType = StyleType.Block;
77+
}
78+
79+
EmbedItemType embedItemType;
80+
if (!node.attrs.ContainsKey("type") || !(Enum.TryParse((string)node.attrs["type"], true, out embedItemType)))
81+
{
82+
embedItemType = EmbedItemType.Entry;
83+
}
84+
string text = "";
85+
if (node.children != null && node.children.Count > 0 && node.children[0].GetType() == typeof(TextNode))
86+
{
87+
text = ((TextNode)node.children[0]).text;
88+
}
89+
string itemUID = "";
90+
if (node.attrs.ContainsKey("entry-uid"))
91+
{
92+
itemUID = (string)node.attrs["entry-uid"];
93+
}else if (node.attrs.ContainsKey("asset-uid"))
94+
{
95+
itemUID = (string)node.attrs["asset-uid"];
96+
}
97+
98+
return new Metadata()
99+
{
100+
Text = text,
101+
OuterHTML = "",
102+
StyleType = styleType,
103+
ItemType = embedItemType,
104+
ItemUid = itemUID,
105+
ContentTypeUid = node.attrs.ContainsKey("content-type-uid") ? (string)node.attrs["content-type-uid"] : "",
106+
attributes = node.attrs
107+
};
108+
109+
}
70110
}
71111
}

Contentstack.Utils/Utils.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ private static string nodeToHtml(Node node, Options options)
7272
{
7373
case Enums.NodeType.Text:
7474
return textToHtml((TextNode)node, options);
75+
case Enums.NodeType.Reference:
76+
return referenceToHtml(node, options);
77+
}
78+
return "";
79+
}
80+
81+
private static string referenceToHtml(Node node, Options options)
82+
{
83+
Metadata metadata = node;
84+
IEmbeddedObject embeddedObject = findEmbeddedObject(metadata, options.entry);
85+
if (embeddedObject != null)
86+
{
87+
return options.RenderOption(embeddedObject, metadata);
7588
}
7689
return "";
7790
}
@@ -113,7 +126,7 @@ private static string textToHtml(TextNode textNode, Options options)
113126
private static IEmbeddedObject findEmbeddedObject(Metadata metadata, IEntryEmbedable entryEmbedable)
114127
{
115128

116-
if (entryEmbedable.embeddedItems.Count > 0)
129+
if (entryEmbedable != null && entryEmbedable.embeddedItems.Count > 0)
117130
{
118131
foreach (var embed in entryEmbedable.embeddedItems)
119132
{

0 commit comments

Comments
 (0)