Skip to content

Commit a47eeb2

Browse files
authored
Merge pull request #21 from contentstack/fix/DX-1482
Staging Merge
2 parents dee9929 + 116d1ce commit a47eeb2

9 files changed

Lines changed: 40 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
### Version: 1.0.5
3+
#### Date: Oct-10-2024
4+
- Used Class Name and Id property in JsonToHTML converter
5+
26
### Version: 1.0.4
37
#### Date: Aug-21-2024
48
- Classname and ID property support in text node

Contentstack.Utils.Tests/Constants/JsonToHtmlConstants.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static class JsonToHtmlResultConstants
2020
public const string kLinkInPMailToHtml = "<p style=\"text-align:left;\"><strong><em><u><sub></sub></u></em></strong><a href=\"mailto:support@contentstack.com\">LINK</a></p>";
2121
public const string kLinkInPMailToTARGEtHtml = "<p style=\"text-align:left;\"><strong><em><u><sub></sub></u></em></strong><a href=\"mailto:support@contentstack.com\" target=\"_self\">LINK</a></p>";
2222
public const string kEmbedHtml = "<iframe src=\"https://www.youtube.com/watch?v=AOP0yARiW8U\"></iframe>";
23+
public const string stringClassIdResult = "<p><span id=\"i3\">This</span> is a <span class=\"c1\" id=\"i1\">text</span> to be <span class=\"c2\">ignored</span></p>" ;
2324
}
2425

2526
public static class JsonToHtmlConstants
@@ -47,8 +48,8 @@ public static class JsonToHtmlConstants
4748
public const string kEntryReferenceLinkJson = "{ \"uid\":\"06e34a7 5e4 e549d\", \"_version\":1, \"attrs\":{ }, \"children\":[{\"uid\":\"7626ea98e0e95d602210\",\"type\":\"reference\",\"attrs\":{\"target\":\"_self\",\"href\":\"/copy-of-entry-final-02\",\"display-type\":\"link\",\"entry-uid\":\"UID_08\",\"content-type-uid\":\"embeddedrte\",\"locale\":\"en-us\",\"type\":\"entry\",\"class-name\":\"embedded-entry\"},\"children\":[{\"text\":\"/copy-of-entry-final-02\"}]}],\"type\":\"doc\"}";
4849
public const string kEntryReferenceInlineJson = "{ \"uid\":\"06e34a7 5e4 e549d\", \"_version\":1, \"attrs\":{ }, \"children\":[{\"uid\":\"506 4878f3f46 s21f0cbc aff\",\"type\":\"reference\",\"attrs\":{\"display-type\":\"inline\",\"entry-uid\":\"UID_09\",\"content-type-uid\":\"embeddedrte\",\"locale\":\"en-us\",\"type\":\"entry\",\"class-name\":\"embedded-entry\"},\"children\":[{\"text\":\"\"}]}],\"type\":\"doc\"}";
4950
public const string kHRJson = "{ \"uid\":\"06e34a7 5e4 e549d\", \"_version\":1, \"attrs\":{ }, \"children\":[{\"uid\":\"f5a7b57 40a8a5c3 576828276b\",\"type\":\"hr\",\"children\":[{\"text\":\"\"}],\"attrs\":{ }}],\"type\":\"doc\"}";
50-
51-
51+
public const string classId = "{ \"uid\":\"06e34a7 5e4 e549d\", \"_version\":1, \"attrs\":{ }, \"children\":[{\"uid\":\"f5a7b57 40a8a5c3 576828276b\",\"type\":\"doc\",\"children\":[{\"text\":\"Thorisbest\",\"classname\": \"c2\",\"id\": \"i2\"}],\"attrs\":{ }}],\"type\":\"text\"}";
52+
public const string stringClassId = "{ \"type\": \"doc\", \"attrs\": {}, \"uid\": \"8622288a91dc4c76985d776d2540b395\", \"children\": [ { \"type\": \"p\", \"uid\": \"396ee25abd0f4296a45eac63809450ef\", \"attrs\": {}, \"children\": [ { \"text\": \"This\", \"classname\": \"\", \"id\": \"i3\" }, { \"text\": \" is a \" }, { \"text\": \"text\", \"classname\": \"c1\", \"id\": \"i1\" }, { \"text\": \" to be \" }, { \"text\": \"ignored\", \"classname\": \"c2\", \"id\": \"\" } ] } ], \"_version\": 4 }";
5253
public const string KAssetNode = "\"embedded_itemsConnection\": { \"edges\": [{ \"node\": { \"system\": { \"content_type_uid\": \"sys_assets\", \"uid\": \"UID_12\" }, \"created_at\": \"2020-08-19T09:13:32.785Z\", \"updated_at\": \"2020-08-19T09:13:32.785Z\", \"created_by\": \"Created_at\", \"updated_by\": \"Created_at\", \"content_type\": \"application/pdf\", \"file_size\": \"13264\", \"filename\": \"dummy.pdf\", \"url\":\"/v3/assetsUID_12/dummy.pdf\", \"_version\": 1, \"title\": \"dummy.pdf\" } } ]}";
5354
public const string KEntryBlocNode = "\"embedded_itemsConnection\": { \"edges\": [{ \"node\": { \"title\": \"Update this title\", \"url\": \"\", \"locale\": \"en-us\", \"system\": { \"uid\": \"UID_07\", \"content_type_uid\": \"content_block\" }, \"_version\": 5, \"_in_progress\": false, \"multi_line\": \"\", \"rich_text_editor\": \"\" } } ]}";
5455
public const string KEntryLinkNode = "\"embedded_itemsConnection\": { \"edges\": [{ \"node\": { \"title\": \"Entry with embedded entry\", \"rich_text_editor\": [ \"\" ], \"locale\": \"en-us\", \"system\": { \"uid\": \"UID_08\", \"content_type_uid\": \"embeddedrte\" }, \"_in_progress\": false } } ]}";

Contentstack.Utils.Tests/JsonToHtmlTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,15 @@ public void Should_Return_Result_For_Array_Hr_Document()
479479

480480
Assert.Equal(new List<string>() { "<hr>" }, result);
481481
}
482+
483+
[Fact]
484+
public void TestForClassandId()
485+
{
486+
Node node = NodeParser.parse(JsonToHtmlConstants.stringClassId);
487+
488+
var result = Utils.JsonToHtml(new List<Node>() { node }, defaultRender);
489+
490+
Assert.Equal(new List<string>() { JsonToHtmlResultConstants.stringClassIdResult }, result);
491+
}
482492
}
483493
}

Contentstack.Utils.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Global
2121
{EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}.Release|Any CPU.Build.0 = Release|Any CPU
2222
EndGlobalSection
2323
GlobalSection(MonoDevelopProperties) = preSolution
24-
version = 1.0.4
24+
version = 1.0.5
2525
Policies = $0
2626
$0.DotNetNamingPolicy = $1
2727
$1.DirectoryNamespaceAssociation = PrefixedHierarchical

Contentstack.Utils/Enums/MarkType.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public enum MarkType
3030
/// <summary>
3131
/// This will specify the Mark type of text node as Superscript
3232
/// </summary>
33-
Superscript
34-
}
33+
Superscript,
34+
/// <summary>
35+
/// This will specify the class selector
36+
/// </summary>
37+
Class,
38+
/// <summary>
39+
/// This will specify the id selector
40+
/// </summary>
41+
Id
42+
}
3543
}

Contentstack.Utils/Interfaces/IOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Contentstack.Utils.Interfaces
1010
public interface IRenderable
1111
{
1212
string RenderOption(IEmbeddedObject entry, Metadata metadata);
13-
string RenderMark(MarkType markType, string text);
13+
string RenderMark(MarkType markType, string text, string className, string Id);
1414
string RenderNode(string nodeType, Node node, NodeChildrenCallBack callBack);
1515
}
1616
}

Contentstack.Utils/Models/Options.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public virtual string RenderOption(IEmbeddedObject embeddedObject, Metadata meta
6666
return "";
6767
}
6868

69-
public virtual string RenderMark(MarkType markType, string text)
69+
public virtual string RenderMark(MarkType markType, string text, string className = "", string id = "")
7070
{
7171
switch (markType)
7272
{
@@ -84,6 +84,11 @@ public virtual string RenderMark(MarkType markType, string text)
8484
return $"<sub>{text}</sub>";
8585
case MarkType.Superscript:
8686
return $"<sup>{text}</sup>";
87+
case MarkType.Id:
88+
case MarkType.Class:
89+
string classAttr = !string.IsNullOrEmpty(className) ? $" class=\"{className}\"" : "";
90+
string idAttr = !string.IsNullOrEmpty(id) ? $" id=\"{id}\"" : "";
91+
return $"<span{classAttr}{idAttr}>{text}</span>";
8792
}
8893
return text;
8994
}

Contentstack.Utils/Utils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ private static string textToHtml(TextNode textNode, Options options)
159159
{
160160
text = options.RenderMark(MarkType.Bold, text: text);
161161
}
162+
if (!string.IsNullOrEmpty(textNode.classname) || !string.IsNullOrEmpty(textNode.id))
163+
{
164+
text = options.RenderMark(MarkType.Class, text: text, textNode.classname, textNode.id);
165+
}
162166
return text;
163167
}
164168

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.0.4</Version>
3+
<Version>1.0.5</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)