Skip to content

Commit 4dc21f0

Browse files
committed
🚧 Json to Html
- IRenderable to implement RenderMark with MarkType and Text - test case for Default render Mark added - Plain node to HTML Completed
1 parent 3dfb629 commit 4dc21f0

3 files changed

Lines changed: 101 additions & 12 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using Contentstack.Utils.Models;
22
using Contentstack.Utils.Interfaces;
3+
using Contentstack.Utils.Enums;
4+
35
namespace Contentstack.Utils.Interfaces
46
{
57
public interface IRenderable
68
{
79
string RenderOption(IEmbeddedObject entry, Metadata metadata);
10+
string RenderMark(MarkType markType, string text);
811
}
912
}

Contentstack.Utils/Models/Options.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
using Contentstack.Utils.Interfaces;
1+
using Contentstack.Utils.Enums;
2+
using Contentstack.Utils.Interfaces;
23

34
namespace Contentstack.Utils.Models
45
{
56
public class Options: IRenderable
67
{
7-
#region Internal Constructors
8-
internal Options()
9-
{
10-
11-
}
12-
#endregion
13-
14-
#region Public
8+
#region #region Public
9+
public Options() {}
10+
1511
public Options(IEntryEmbedable entry)
1612
{
1713
this.entry = entry;
@@ -61,6 +57,27 @@ public virtual string RenderOption(IEmbeddedObject embeddedObject, Metadata meta
6157
}
6258
return "";
6359
}
60+
61+
public string RenderMark(MarkType markType, string text)
62+
{
63+
switch (markType) {
64+
case MarkType.Bold:
65+
return "<strong>"+text+"</strong>";
66+
case MarkType.Italic:
67+
return "<em>"+text+"</em>";
68+
case MarkType.Underline:
69+
return "<u>"+text+"</u>";
70+
case MarkType.Strikethrough:
71+
return "<strike>"+text+"</strike>";
72+
case MarkType.InlineCode:
73+
return "<span>"+text+"</span>";
74+
case MarkType.Subscript:
75+
return "<sub>" + text + "</sub>";
76+
case MarkType.Superscript:
77+
return "<sup>"+text+"</sup>";
78+
}
79+
return text;
80+
}
6481
#endregion
6582
}
6683
}

Contentstack.Utils/Utils.cs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
using Contentstack.Utils.Extensions;
55
using Contentstack.Utils.Interfaces;
66
using System;
7+
using Contentstack.Utils.Enums;
78

89
namespace Contentstack.Utils
910
{
1011
public class Utils
1112
{
13+
public static List<string> RenderContent(List<string> contents, Options options)
14+
{
15+
List<string> result = new List<string>();
16+
17+
contents.ForEach((content) =>
18+
{
19+
result.Add(RenderContent(content, options));
20+
});
21+
22+
return result;
23+
}
24+
1225
public static string RenderContent(string content, Options options)
1326
{
1427
HtmlDocument htmlDocument = new HtmlDocument();
@@ -29,18 +42,74 @@ public static string RenderContent(string content, Options options)
2942
return resultContent;
3043
}
3144

32-
public static List<string> RenderContent(List<string> contents, Options options)
45+
public static List<string> JsonToHtml(List<Node> nodes, Options options)
3346
{
47+
3448
List<string> result = new List<string>();
3549

36-
contents.ForEach((content) =>
50+
nodes.ForEach((node) =>
3751
{
38-
result.Add(RenderContent(content, options));
52+
result.Add(JsonToHtml(node, options));
3953
});
4054

4155
return result;
4256
}
4357

58+
public static string JsonToHtml(Node node, Options options)
59+
{
60+
return nodeChildrenToHtml(node.children, options);
61+
}
62+
63+
private static string nodeChildrenToHtml(List<Node> nodes, Options options)
64+
{
65+
return string.Join("", nodes.ConvertAll<string>(node => nodeToHtml(node, options)));
66+
67+
}
68+
69+
private static string nodeToHtml(Node node, Options options)
70+
{
71+
switch (node.type)
72+
{
73+
case Enums.NodeType.Text:
74+
return textToHtml((TextNode)node, options);
75+
}
76+
return "";
77+
}
78+
79+
private static string textToHtml(TextNode textNode, Options options)
80+
{
81+
var text = textNode.text;
82+
if (textNode.superscript)
83+
{
84+
text = options.RenderMark(MarkType.Superscript, text: text);
85+
}
86+
if (textNode.subscript)
87+
{
88+
text = options.RenderMark(MarkType.Subscript, text: text);
89+
}
90+
if (textNode.inlineCode)
91+
{
92+
text = options.RenderMark(MarkType.InlineCode, text: text);
93+
}
94+
if (textNode.strikethrough)
95+
{
96+
text = options.RenderMark(MarkType.Strikethrough, text: text);
97+
}
98+
if (textNode.underline)
99+
{
100+
text = options.RenderMark(MarkType.Underline, text: text);
101+
}
102+
if (textNode.italic)
103+
{
104+
text = options.RenderMark(MarkType.Italic, text: text);
105+
}
106+
if (textNode.bold)
107+
{
108+
text = options.RenderMark(MarkType.Bold, text: text);
109+
}
110+
return text;
111+
}
112+
44113
private static IEmbeddedObject findEmbeddedObject(Metadata metadata, IEntryEmbedable entryEmbedable)
45114
{
46115

0 commit comments

Comments
 (0)