diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bd0842..ac3b4b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### Version: 1.0.6 +#### Date: June-09-2025 +- Used 'title' and 'target' in a tags ### Version: 1.0.5 #### Date: Oct-10-2024 diff --git a/Contentstack.Utils.Tests/Constants/JsonToHtmlConstants.cs b/Contentstack.Utils.Tests/Constants/JsonToHtmlConstants.cs index 09d6761..2d6ecdb 100644 --- a/Contentstack.Utils.Tests/Constants/JsonToHtmlConstants.cs +++ b/Contentstack.Utils.Tests/Constants/JsonToHtmlConstants.cs @@ -16,11 +16,13 @@ public static class JsonToHtmlResultConstants public const string kTableHtml = "

Header 1

Header 2

Body row 1 data 1

Body row 1 data 2

Body row 2 data 1

Body row 2 data 2

"; public const string kBlockquoteHtml = "
Praesent eu ex sed nibh venenatis pretium.
"; public const string kCodeHtml = "Code template."; - public const string kLinkInPHtml = "

LINK

"; - public const string kLinkInPMailToHtml = "

LINK

"; + public const string kLinkInPHtml = "

LINK

"; + public const string kLinkInPMailToHtml = "

LINK

"; public const string kLinkInPMailToTARGEtHtml = "

LINK

"; public const string kEmbedHtml = ""; - public const string stringClassIdResult = "

This is a text to be ignored

" ; + public const string stringClassIdResult = "

This is a text to be ignored

"; + public const string kJSONRTEResult = "

demoText is demoText

This is same

demoText2

demoText3demoText4

demoText3demoText2

"; + } public static class JsonToHtmlConstants @@ -48,7 +50,7 @@ public static class JsonToHtmlConstants 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\"}"; 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\"}"; public const string kHRJson = "{ \"uid\":\"06e34a7 5e4 e549d\", \"_version\":1, \"attrs\":{ }, \"children\":[{\"uid\":\"f5a7b57 40a8a5c3 576828276b\",\"type\":\"hr\",\"children\":[{\"text\":\"\"}],\"attrs\":{ }}],\"type\":\"doc\"}"; - 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\"}"; + public const string classId = "{ \"uid\":\"06e34a7 5e4 e549d\", \"_version\":1, \"attrs\":{ }, \"children\":[{\"uid\":\"f5a7b57 40a8a5c3 576828276b\",\"type\":\"doc\",\"children\":[{\"text\":\"demoTextisdemoText\",\"classname\": \"c2\",\"id\": \"i2\"}],\"attrs\":{ }}],\"type\":\"text\"}"; 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 }"; 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\" } } ]}"; 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\": \"\" } } ]}"; @@ -56,7 +58,7 @@ public static class JsonToHtmlConstants public const string KEntryInlineNode = "\"embedded_itemsConnection\": { \"edges\": [{ \"node\": { \"title\": \"updated title\", \"rich_text_editor\": [ \"\" ], \"locale\": \"en-us\", \"system\": { \"uid\": \"UID_09\", \"content_type_uid\": \"embeddedrte\", }, \"_in_progress\": false } } ]}"; public static string KGQLModel(string node, string embedConnection = null) { - return $"{{\"multiplerte\":{{\"json\":[{node}]{(embedConnection != null ? ","+embedConnection : "")} }}, \"singlerte\":{{\"json\":{node}{(embedConnection != null ? "," + embedConnection : "")}}} }}"; + return $"{{\"multiplerte\":{{\"json\":[{node}]{(embedConnection != null ? "," + embedConnection : "")} }}, \"singlerte\":{{\"json\":{node}{(embedConnection != null ? "," + embedConnection : "")}}} }}"; } public const string kAssetMeta = "{\"uid\":\"asset_uid\",\"created_by\":\"create_by\",\"updated_by\":\"update_by\",\"content_type\":\"image/jpeg\",\"file_size\":\"62181\",\"tags\":[],\"filename\":\"crop_area.jpeg\",\"url\":\"http://image.contenstack.com/crop_area.jpeg\",\"is_dir\":false,\"parent_uid\":null,\"path\":[],\"_version\":1,\"title\":\"crop_area.jpeg\",\"dimension\":{\"height\":712,\"width\":864},\"_metadata\":{\"extensions\":{\"extension_uid\":{\"local_metadata\":{\"local_data\":\"main\",\"presets\":[{\"uid\":\"preset_01\",\"name\":\"Local Preset\",\"options\":{\"transform\":{\"height\":500,\"width\":500,\"flip-mode\":\"horiz\",\"rotate\":40},\"focal-point\":{\"x\":-0.668935003427432,\"y\":-0.9220385351936531},\"quality\":\"100\",\"image-type\":\"jpeg\"}},{\"uid\":\"preset_02\",\"name\":\"WithCrop\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":\"569.6\",\"width\":\"569.6\",\"flip-mode\":\"both\"},\"image-type\":\"jpeg\",\"crop\":{\"height\":\"569.6\",\"width\":\"569.6\",\"x\":\"147.2\",\"y\":\"71.2\"}}},{\"uid\":\"preset_03\",\"name\":\"Filter\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":712,\"width\":864},\"image-type\":\"jpeg\",\"effects\":{\"brightness\":52,\"contrast\":15,\"saturate\":-30,\"blur\":16,\"sharpen\":{\"amount\":9,\"radius\":669,\"threshold\":207}}}}]},\"global_metadata\":{\"presets\":[{\"uid\":\"preset_04\",\"name\":\"Global Preset\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":\"712\",\"width\":\"864\",\"flip-mode\":\"verti\"},\"image-type\":\"jpeg\"}}]}}}}}"; @@ -65,6 +67,7 @@ public static string KGQLModel(string node, string embedConnection = null) public const string kAssetPresets = "{\"uid\":\"asset_uid\",\"created_by\":\"create_by\",\"updated_by\":\"update_by\",\"content_type\":\"image/jpeg\",\"file_size\":\"62181\",\"tags\":[],\"filename\":\"crop_area.jpeg\",\"url\":\"http://image.contenstack.com/crop_area.jpeg\",\"is_dir\":false,\"parent_uid\":null,\"path\":[],\"_version\":1,\"title\":\"crop_area.jpeg\",\"dimension\":{\"height\":712,\"width\":864},\"_metadata\":{\"extensions\":{\"extension_uid\":{\"local_metadata\":{\"local_data\":\"main\",\"presets\":[{\"uid\":\"preset_01\",\"name\":\"Local Preset\",\"options\":{}},{\"uid\":\"preset_02\",\"name\":\"WithCrop\",\"options\":{\"quality\":\"100\",\"transform\":{},\"image-type\":\"jpeg\",\"crop\":{}}},{\"uid\":\"preset_03\",\"name\":\"Filter\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":712,\"width\":864},\"image-type\":\"jpeg\",\"effects\":{}}}]},\"global_metadata\":{\"presets\":[{\"uid\":\"preset_04\",\"name\":\"Global Preset\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":\"712\",\"width\":\"864\",\"flip-mode\":\"verti\"},\"image-type\":\"jpeg\"}}]}}}}}"; public const string kAsset = "{\"uid\":\"asset_uid\",\"created_by\":\"create_by\",\"updated_by\":\"update_by\",\"content_type\":\"image/jpeg\",\"file_size\":\"62181\",\"tags\":[],\"filename\":\"crop_area.jpeg\",\"url\":\"http://image.contenstack.com/crop_area.jpeg\",\"is_dir\":false,\"parent_uid\":null,\"path\":[],\"_version\":1,\"title\":\"crop_area.jpeg\",\"dimension\":{\"height\":712,\"width\":864},\"_metadata\":{}}"; public const string kAssetMetaURLQuery = "{\"uid\":\"asset_uid\",\"created_by\":\"create_by\",\"updated_by\":\"update_by\",\"content_type\":\"image/jpeg\",\"file_size\":\"62181\",\"tags\":[],\"filename\":\"crop_area.jpeg\",\"url\":\"http://image.contenstack.com/crop_area.jpeg?render=full&noval\",\"is_dir\":false,\"parent_uid\":null,\"path\":[],\"_version\":1,\"title\":\"crop_area.jpeg\",\"dimension\":{\"height\":712,\"width\":864},\"_metadata\":{\"extensions\":{\"extension_uid\":{\"local_metadata\":{\"local_data\":\"main\",\"presets\":[{\"uid\":\"preset_01\",\"name\":\"Local Preset\",\"options\":{\"transform\":{\"height\":500,\"width\":500,\"flip-mode\":\"horiz\",\"rotate\":40},\"focal-point\":{\"x\":-0.668935003427432,\"y\":-0.9220385351936531},\"quality\":\"100\",\"image-type\":\"jpeg\"}},{\"uid\":\"preset_02\",\"name\":\"WithCrop\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":\"569.6\",\"width\":\"569.6\",\"flip-mode\":\"both\"},\"image-type\":\"jpeg\",\"crop\":{\"height\":\"569.6\",\"width\":\"569.6\",\"x\":\"147.2\",\"y\":\"71.2\"}}},{\"uid\":\"preset_03\",\"name\":\"Filter\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":712,\"width\":864},\"image-type\":\"jpeg\",\"effects\":{\"brightness\":52,\"contrast\":15,\"saturate\":-30,\"blur\":16,\"sharpen\":{\"amount\":9,\"radius\":669,\"threshold\":207}}}}]},\"global_metadata\":{\"presets\":[{\"uid\":\"preset_04\",\"name\":\"Global Preset\",\"options\":{\"quality\":\"100\",\"transform\":{\"height\":\"712\",\"width\":\"864\",\"flip-mode\":\"verti\"},\"image-type\":\"jpeg\"}}]}}}}}"; + public const string kJSONRTE = "{ \"uid\":\"uid\",\"_version\":1,\"locale\":\"en-us\",\"children\":[{\"type\":\"p\",\"children\":[{\"text\":\"demoText\",\"bold\":true},{\"text\":\" is\",\"underline\":true},{\"text\":\" demoText\",\"italic\":true}]},{\"type\":\"p\",\"children\":[{\"text\":\"This\",\"italic\":true,\"subscript\":true},{\"text\":\" \",\"italic\":true},{\"text\":\"is\",\"italic\":true,\"superscript\":true},{\"text\":\" \",\"italic\":true},{\"text\":\"same \",\"italic\":true,\"strikethrough\":true}]},{\"type\":\"p\",\"children\":[{\"text\":\"demoText2\",\"strikethrough\":true,\"inlineCode\":true}]},{\"type\":\"p\",\"children\":[{\"text\":\"demoText3\",\"strikethrough\":true,\"inlineCode\":true},{\"text\":\"demoText4\"}]},{\"type\":\"p\",\"children\":[{\"text\":\"demoText3\"},{\"type\":\"a\",\"attrs\":{\"url\":\"https://www.google.com/\",\"target\":\"_blank\",\"title\":\"demoText5\"},\"children\":[{\"text\":\"demoText2\"}]}]}],\"type\":\"doc\"}"; } } diff --git a/Contentstack.Utils.Tests/DefaultRenderTest.cs b/Contentstack.Utils.Tests/DefaultRenderTest.cs index 776f6c9..a13bd5b 100644 --- a/Contentstack.Utils.Tests/DefaultRenderTest.cs +++ b/Contentstack.Utils.Tests/DefaultRenderTest.cs @@ -1,4 +1,4 @@ -using Contentstack.Utils.Models; +using Contentstack.Utils.Models; using Xunit; using Contentstack.Utils.Tests.Mocks; using Contentstack.Utils.Enums; @@ -178,7 +178,7 @@ public void testLinkhDocument() string result = defaultRender.RenderNode("a", nodeLink, (nodes) => { return text; }); - Assert.Equal($"{text}", result); + Assert.Equal($"Text To set Link", result); } [Fact] diff --git a/Contentstack.Utils.Tests/JsonToHtmlTest.cs b/Contentstack.Utils.Tests/JsonToHtmlTest.cs index 9f4b4e1..e49da43 100644 --- a/Contentstack.Utils.Tests/JsonToHtmlTest.cs +++ b/Contentstack.Utils.Tests/JsonToHtmlTest.cs @@ -489,5 +489,13 @@ public void TestForClassandId() Assert.Equal(new List() { JsonToHtmlResultConstants.stringClassIdResult }, result); } + + [Fact] + public void Should_Render_All_Marks_And_Target_Attribute() + { + Node node = NodeParser.parse(JsonToHtmlConstants.kJSONRTE); + var result = Utils.JsonToHtml(node, defaultRender); + Assert.Equal(JsonToHtmlResultConstants.kJSONRTEResult, result); + } } } diff --git a/Contentstack.Utils.sln b/Contentstack.Utils.sln index 12afb40..382b162 100644 --- a/Contentstack.Utils.sln +++ b/Contentstack.Utils.sln @@ -21,7 +21,7 @@ Global {EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution - version = 1.0.5 + version = 1.0.6 Policies = $0 $0.DotNetNamingPolicy = $1 $1.DirectoryNamespaceAssociation = PrefixedHierarchical diff --git a/Contentstack.Utils/Models/Options.cs b/Contentstack.Utils/Models/Options.cs index 7cab2fc..724322a 100644 --- a/Contentstack.Utils/Models/Options.cs +++ b/Contentstack.Utils/Models/Options.cs @@ -1,6 +1,6 @@ -using System; +using System; using System.Collections.Generic; -using Contentstack.Utils.Enums; +using Contentstack.Utils.Enums; using Contentstack.Utils.Interfaces; using Newtonsoft.Json.Linq; @@ -23,14 +23,14 @@ public virtual string RenderOption(IEmbeddedObject embeddedObject, Metadata meta switch (metadata.StyleType) { case Enums.StyleType.Block: - string renderString = "

" + embeddedObject.Uid + "

"; + string renderString = "

" + embeddedObject.Uid + "

"; if (embeddedObject is IEmbeddedEntry) { - renderString += "

Content type: " + ((IEmbeddedEntry)embeddedObject).Title + "

"; + renderString += "

Content type: " + ((IEmbeddedEntry)embeddedObject).Title + "

"; } else { - renderString += "

Content type: " + embeddedObject.ContentTypeUid + "

"; + renderString += "

Content type: " + embeddedObject.ContentTypeUid + "

"; } renderString = renderString + "
"; return renderString; @@ -38,32 +38,32 @@ public virtual string RenderOption(IEmbeddedObject embeddedObject, Metadata meta case Enums.StyleType.Inline: if (embeddedObject is IEmbeddedEntry) { - return "" + ((IEmbeddedEntry)embeddedObject).Title + ""; + return "" + ((IEmbeddedEntry)embeddedObject).Title + ""; } - return "" + embeddedObject.Uid + ""; + return "" + embeddedObject.Uid + ""; case Enums.StyleType.Link: if (embeddedObject is IEmbeddedEntry) { - return "" + (metadata.Text ?? ((IEmbeddedEntry)embeddedObject).Title) + ""; + return "" + (metadata.Text ?? ((IEmbeddedEntry)embeddedObject).Title) + ""; } - return "" + (metadata.Text ?? embeddedObject.Uid) + ""; + return "" + (metadata.Text ?? embeddedObject.Uid) + ""; case Enums.StyleType.Display: if (embeddedObject is IEmbeddedAsset) { - return "\"""; + return "\"""; } - return "\"""; + return "\"""; case Enums.StyleType.Download: if (embeddedObject is IEmbeddedAsset) { - return "" + (metadata.Text ?? ((IEmbeddedAsset)embeddedObject).Title) + ""; + return "" + (metadata.Text ?? ((IEmbeddedAsset)embeddedObject).Title) + ""; } - return "" + (metadata.Text ?? embeddedObject.Uid) + ""; + return "" + (metadata.Text ?? embeddedObject.Uid) + ""; } - return ""; + return ""; } public virtual string RenderMark(MarkType markType, string text, string className = "", string id = "") @@ -97,8 +97,10 @@ public virtual string RenderNode(string nodeType, Node node, NodeChildrenCallBac { string href = ""; string styleAttrs = ""; + string target = ""; + string title = ""; - if (node.attrs.ContainsKey("style")) + if (node.attrs?.ContainsKey("style") == true) { var styleVal = node.attrs["style"]; if (styleVal != null) @@ -125,11 +127,20 @@ public virtual string RenderNode(string nodeType, Node node, NodeChildrenCallBac case "p": return $"{callBack(node.children)}

"; case "a": - if (node.attrs.ContainsKey("url")) + if (node.attrs?.ContainsKey("url")==true) { href = (string)node.attrs["url"]; } - return $"{callBack(node.children)}"; + if (node.attrs?.ContainsKey("target") == true) + { + target = (string)node.attrs["target"]; + } + if (node.attrs?.ContainsKey("title") == true) + { + title = (string)node.attrs["title"]; + } + return $"{callBack(node.children)}"; + case "img": if (node.attrs.ContainsKey("url")) { diff --git a/Directory.Build.props b/Directory.Build.props index 18f9aae..2a251d5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 1.0.5 + 1.0.6