Skip to content

Commit 9dcdc74

Browse files
authored
Merge pull request #1 from contentstack/feature/supercharge-rte
Feature/supercharge rte
2 parents 98551e0 + 33f90f7 commit 9dcdc74

16 files changed

Lines changed: 635 additions & 48 deletions

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.1
3+
#### Date: July-16-2021
4+
- Json RTE content to Html Support added.
5+
26
### Version: 1.0.0
37
#### Date: Apr-05-2021
48
- Introduce ContentStack Utils SDK for DOTNET.

Contentstack.Utils/Contentstack.Utils.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
<Folder Include="Models\" />
3636
<Folder Include="Enums\" />
3737
<Folder Include="Extensions\" />
38+
<Folder Include="Converters\" />
3839
</ItemGroup>
3940
<ItemGroup>
4041
<PackageReference Include="HtmlAgilityPack" Version="1.11.28" />
42+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
4143
</ItemGroup>
4244
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using Contentstack.Utils.Models;
5+
6+
namespace Contentstack.Utils.Converters
7+
{
8+
public class NodeJsonConverter : JsonConverter<Node>
9+
{
10+
public override Node ReadJson(JsonReader reader, Type objectType, Node existingValue, bool hasExistingValue, JsonSerializer serializer)
11+
{
12+
Node node = null;
13+
JObject jObject = JObject.Load(reader);
14+
if (jObject["type"] == null)
15+
{
16+
node = new TextNode();
17+
node.type = "text";
18+
}else
19+
{
20+
node = new Node();
21+
}
22+
serializer.Populate(jObject.CreateReader(), node);
23+
return node;
24+
}
25+
26+
public override void WriteJson(JsonWriter writer, Node value, JsonSerializer serializer)
27+
{
28+
29+
}
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
6+
7+
namespace Contentstack.Utils.Converters
8+
{
9+
public class RTEJsonConverter : JsonConverter
10+
{
11+
public override bool CanConvert(Type objectType)
12+
{
13+
throw new NotImplementedException();
14+
}
15+
16+
public override object ReadJson(JsonReader reader, Type objectType,
17+
object existingValue, JsonSerializer serializer)
18+
{
19+
JObject jo = JObject.Load(reader);
20+
object targetObj = Activator.CreateInstance(objectType);
21+
22+
foreach (PropertyInfo prop in objectType.GetProperties()
23+
.Where(p => p.CanRead && p.CanWrite))
24+
{
25+
JsonPropertyAttribute att = prop.GetCustomAttributes(true)
26+
.OfType<JsonPropertyAttribute>()
27+
.FirstOrDefault();
28+
29+
string jsonPath = (att != null ? att.PropertyName : prop.Name);
30+
JToken token = jo.SelectToken(jsonPath);
31+
32+
if (token != null && token.Type != JTokenType.Null)
33+
{
34+
object value = token.ToObject(prop.PropertyType, serializer);
35+
prop.SetValue(targetObj, value, null);
36+
}
37+
}
38+
39+
return targetObj;
40+
}
41+
42+
43+
public override void WriteJson(JsonWriter writer, object value,
44+
JsonSerializer serializer)
45+
{
46+
47+
}
48+
}
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
namespace Contentstack.Utils.Enums
3+
{
4+
public enum MarkType
5+
{
6+
/// <summary>
7+
/// This will specify the Mark type of text node as Bold
8+
/// </summary>
9+
Bold,
10+
/// <summary>
11+
/// This will specify the Mark type of text node as Italic
12+
/// </summary>
13+
Italic,
14+
/// <summary>
15+
/// This will specify the Mark type of text node as Underline
16+
/// </summary>
17+
Underline,
18+
/// <summary>
19+
/// This will specify the Mark type of text node as Strikethrough
20+
/// </summary>
21+
Strikethrough,
22+
/// <summary>
23+
/// This will specify the Mark type of text node as InlineCode
24+
/// </summary>
25+
InlineCode,
26+
/// <summary>
27+
/// This will specify the Mark type of text node as Subscript
28+
/// </summary>
29+
Subscript,
30+
/// <summary>
31+
/// This will specify the Mark type of text node as Superscript
32+
/// </summary>
33+
Superscript
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Contentstack.Utils.Interfaces
5+
{
6+
public class IEdges<T> where T: IEmbeddedObject
7+
{
8+
[JsonProperty("node")]
9+
public T Node
10+
{
11+
get;
12+
set;
13+
}
14+
}
15+
}
16+
17+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
using Contentstack.Utils.Models;
22
using Contentstack.Utils.Interfaces;
3+
using Contentstack.Utils.Enums;
4+
using System.Collections.Generic;
5+
36
namespace Contentstack.Utils.Interfaces
47
{
8+
public delegate string NodeChildrenCallBack(List<Node> nodes);
9+
510
public interface IRenderable
611
{
712
string RenderOption(IEmbeddedObject entry, Metadata metadata);
13+
string RenderMark(MarkType markType, string text);
14+
string RenderNode(string nodeType, Node node, NodeChildrenCallBack callBack);
815
}
916
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Contentstack.Utils.Converters;
4+
using Contentstack.Utils.Interfaces;
5+
using Newtonsoft.Json;
6+
7+
namespace Contentstack.Utils.Models
8+
{
9+
[Newtonsoft.Json.JsonConverter(typeof(RTEJsonConverter))]
10+
public class JsonRTENode<T> where T: IEmbeddedObject
11+
{
12+
[JsonProperty("json")]
13+
public Node Json { get; set; }
14+
[JsonProperty("embedded_itemsConnection.edges")]
15+
public List<IEdges<T>> Edges { get; set; }
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Contentstack.Utils.Converters;
4+
using Contentstack.Utils.Interfaces;
5+
using Newtonsoft.Json;
6+
7+
namespace Contentstack.Utils.Models
8+
{
9+
[Newtonsoft.Json.JsonConverter(typeof(RTEJsonConverter))]
10+
public class JsonRTENodes<T> where T : IEmbeddedObject
11+
{
12+
[JsonProperty("json")]
13+
public List<Node> Json { get; set; }
14+
[JsonProperty("embedded_itemsConnection.edges")]
15+
public List<IEdges<T>> Edges { get; set; }
16+
}
17+
}

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
}

0 commit comments

Comments
 (0)