-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
429 lines (389 loc) · 16.7 KB
/
Utils.cs
File metadata and controls
429 lines (389 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
using System.Collections.Generic;
using Contentstack.Utils.Models;
using HtmlAgilityPack;
using Contentstack.Utils.Extensions;
using Contentstack.Utils.Interfaces;
using System;
using Contentstack.Utils.Enums;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Contentstack.Utils
{
public class Utils
{
public class GQL
{
public static string JsonToHtml<T>(JsonRTENode<T> jsonRTE, Options options) where T: IEmbeddedObject
{
return Utils.nodeChildrenToHtml(jsonRTE.Json.children, options, GQL.refernceToHtml(options, jsonRTE.Edges));
}
public static List<string> JsonToHtml<T>(JsonRTENodes<T> jsonRTE, Options options) where T : IEmbeddedObject
{
List<string> result = new List<string>();
jsonRTE.Json.ForEach((content) =>
{
result.Add(nodeChildrenToHtml(content.children, options, GQL.refernceToHtml(options, jsonRTE.Edges)));
});
return result;
}
private static Func<Node, string> refernceToHtml<T>(Options options, List<IEdges<T>> edges) where T : IEmbeddedObject
{
return (n) =>
{
Metadata metadata = n;
if (edges != null && edges.Count > 0)
{
IEdges<T> edge = edges.Find(entry => {
return entry.Node.Uid == metadata.ItemUid && entry.Node.ContentTypeUid == metadata.ContentTypeUid;
});
if (edge != null && edge.Node != null)
{
return options.RenderOption(edge.Node, metadata);
}
}
return "";
};
}
}
public static List<string> RenderContent(List<string> contents, Options options)
{
List<string> result = new List<string>();
contents.ForEach((content) =>
{
result.Add(RenderContent(content, options));
});
return result;
}
public static string RenderContent(string content, Options options)
{
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(content);
var resultContent = htmlDocument.DocumentNode.OuterHtml;
htmlDocument.FindEmbeddedObject((Metadata metadata) =>
{
var replaceString = "";
IEmbeddedObject embeddedObject = findEmbeddedObject(metadata, options.entry.embeddedItems);
if (embeddedObject != null)
{
replaceString = options.RenderOption(embeddedObject, metadata);
}
resultContent = resultContent.Replace(metadata.OuterHTML, replaceString);
});
return resultContent;
}
public static List<string> JsonToHtml(List<Node> nodes, Options options)
{
List<string> result = new List<string>();
nodes.ForEach((node) =>
{
result.Add(JsonToHtml(node, options));
});
return result;
}
public static string JsonToHtml(Node node, Options options)
{
Func<Node, string> referenceToHtml = (n) =>
{
Metadata metadata = n;
if (options.entry != null)
{
IEmbeddedObject embeddedObject = findEmbeddedObject(metadata, options.entry.embeddedItems);
if (embeddedObject != null)
{
return options.RenderOption(embeddedObject, metadata);
}
}
return "";
};
return nodeChildrenToHtml(node.children, options, referenceToHtml);
}
private static string nodeChildrenToHtml(List<Node> nodes, Options options, Func<Node, string> referenceToHtml)
{
return string.Join("", nodes.ConvertAll<string>(node => nodeToHtml(node, options, referenceToHtml)));
}
private static string nodeToHtml(Node node, Options options, Func<Node, string> referenceToHtml)
{
switch (node.type)
{
case "text":
return textToHtml((TextNode)node, options);
case "reference":
return referenceToHtml(node);
}
return options.RenderNode(node.type, node, (nodes) => { return nodeChildrenToHtml(nodes, options, referenceToHtml); });
}
private static string textToHtml(TextNode textNode, Options options)
{
var text = textNode.text;
if (textNode.superscript)
{
text = options.RenderMark(MarkType.Superscript, text: text);
}
if (textNode.subscript)
{
text = options.RenderMark(MarkType.Subscript, text: text);
}
if (textNode.inlineCode)
{
text = options.RenderMark(MarkType.InlineCode, text: text);
}
if (textNode.strikethrough)
{
text = options.RenderMark(MarkType.Strikethrough, text: text);
}
if (textNode.underline)
{
text = options.RenderMark(MarkType.Underline, text: text);
}
if (textNode.italic)
{
text = options.RenderMark(MarkType.Italic, text: text);
}
if (textNode.bold)
{
text = options.RenderMark(MarkType.Bold, text: text);
}
if (!string.IsNullOrEmpty(textNode.classname) || !string.IsNullOrEmpty(textNode.id))
{
text = options.RenderMark(MarkType.Class, text: text, textNode.classname, textNode.id);
}
return text;
}
private static IEmbeddedObject findEmbeddedObject(Metadata metadata, Dictionary<string, List<IEmbeddedObject>> embeddedItems)
{
if (embeddedItems != null && embeddedItems.Count > 0)
{
foreach (var embed in embeddedItems)
{
if (embed.Value.Count > 0)
{
return findEmbedded(metadata, embed.Value);
}
}
}
return null;
}
private static IEmbeddedObject findEmbedded(Metadata metadata, List<IEmbeddedObject> items)
{
if (items != null && items.Count > 0)
{
IEmbeddedObject embeddedObject = items.Find(entry => {
return entry.Uid == metadata.ItemUid && entry.ContentTypeUid == metadata.ContentTypeUid;
});
if (embeddedObject != null)
{
return embeddedObject;
}
}
return null;
}
public static void addEditableTags(EditableEntry entry, string contentTypeUid, bool tagsAsObject, string locale = "en-us")
{
if (entry != null)
entry["$"] = GetTag(entry, $"{contentTypeUid}.{entry.Uid}.{locale}", tagsAsObject, locale);
}
private static Dictionary<string, object> GetTag(object content, string prefix, bool tagsAsObject, string locale)
{
var tags = new Dictionary<string, object>();
foreach (var property in (Dictionary<string, object>)content)
{
var key = property.Key;
var value = property.Value;
if (key == "$")
continue;
switch (value)
{
case object obj when obj is object[] array:
for (int index = 0; index < array.Length; index++)
{
object objValue = array[index];
string childKey = $"{key}__{index}";
string parentKey = $"{key}__parent";
tags[childKey] = GetTagsValue($"{prefix}.{key}.{index}", tagsAsObject);
tags[parentKey] = GetParentTagsValue($"{prefix}.{key}", tagsAsObject);
if (objValue != null &&
objValue.GetType().GetProperty("_content_type_uid") != null &&
objValue.GetType().GetProperty("Uid") != null)
{
var typedObj = (EditableEntry)objValue;
string locale_ = Convert.ToString(typedObj.GetType().GetProperty("locale").GetValue(typedObj));
string ctUid = Convert.ToString(typedObj.GetType().GetProperty("_content_type_uid").GetValue(typedObj));
string uid = Convert.ToString(typedObj.GetType().GetProperty("uid").GetValue(typedObj));
string localeStr = "";
if (locale_ != null)
{
localeStr = locale_;
} else
{
localeStr = locale;
}
typedObj["$"] = GetTag(typedObj, $"{ctUid}.{uid}.{localeStr}", tagsAsObject, locale);
}
else if (value is object)
{
((EditableEntry)value)["$"] = GetTag(value, $"{prefix}.{key}.{index}", tagsAsObject, locale);
}
}
tags[key] = GetTagsValue($"{prefix}.{key}", tagsAsObject);
break;
case object obj when obj != null:
if (value != null)
{
((EditableEntry)value)["$"] = GetTag(value, $"{prefix}.{key}", tagsAsObject, locale);
}
break;
}
}
return tags;
}
private static object GetTagsValue(string dataValue, bool tagsAsObject)
{
if (tagsAsObject)
{
return new Dictionary<string, object> { { "data-cslp", dataValue } };
}
else
{
return $"data-cslp={dataValue}";
}
}
private static object GetParentTagsValue(string dataValue, bool tagsAsObject)
{
if (tagsAsObject)
{
return new Dictionary<string, object> { { "data-cslp-parent-field", dataValue } };
}
else
{
return $"data-cslp-parent-field={dataValue}";
}
}
public static JObject GetVariantAliases(JObject entry, string contentTypeUid)
{
if (string.IsNullOrEmpty(contentTypeUid))
{
throw new ArgumentException("ContentType is required.");
}
if (entry == null)
{
throw new ArgumentException("Entry must not be null.");
}
if (!entry.ContainsKey("uid") || entry["uid"] == null || entry["uid"].Type == JTokenType.Null)
{
throw new ArgumentException("Entry must contain uid.");
}
string entryUid = entry["uid"]?.ToString() ?? "";
JArray variantsArray = ExtractVariantAliasesFromEntry(entry);
JObject result = new JObject
{
["entry_uid"] = entryUid,
["contenttype_uid"] = contentTypeUid,
["variants"] = variantsArray
};
return result;
}
public static JArray GetVariantAliases(JArray entries, string contentTypeUid)
{
if (string.IsNullOrEmpty(contentTypeUid))
{
throw new ArgumentException("ContentType is required.");
}
if (entries == null)
{
return new JArray();
}
JArray variantResults = new JArray();
foreach (JToken token in entries)
{
JObject entry = token as JObject;
if (entry != null && entry.ContainsKey("uid") && entry["uid"] != null && entry["uid"].Type != JTokenType.Null)
{
variantResults.Add(GetVariantAliases(entry, contentTypeUid));
}
}
return variantResults;
}
/// <summary>
/// Builds the JSON object used for the <c>data-csvariants</c> HTML attribute payload from a single entry.
/// </summary>
/// <param name="entry">Entry JSON (e.g. from the Delivery API), or <c>null</c> to produce an empty payload.</param>
/// <param name="contentTypeUid">Content type UID for the entry.</param>
/// <returns>A <see cref="JObject"/> with a <c>data-csvariants</c> key whose value is a compact JSON array string.</returns>
public static JObject GetVariantMetadataTags(JObject entry, string contentTypeUid)
{
if (entry == null)
{
JObject result = new JObject();
result["data-csvariants"] = "[]";
return result;
}
JArray entries = new JArray();
entries.Add(entry);
return GetVariantMetadataTags(entries, contentTypeUid);
}
/// <summary>
/// Builds the JSON object used for the <c>data-csvariants</c> HTML attribute payload from multiple entries.
/// </summary>
/// <param name="entries">Array of entry JSON objects, or <c>null</c> to produce an empty payload.</param>
/// <param name="contentTypeUid">Content type UID shared by these entries.</param>
/// <returns>A <see cref="JObject"/> with a <c>data-csvariants</c> key whose value is a compact JSON array string.</returns>
public static JObject GetVariantMetadataTags(JArray entries, string contentTypeUid)
{
JObject result = new JObject();
if (entries == null)
{
result["data-csvariants"] = "[]";
return result;
}
if (string.IsNullOrEmpty(contentTypeUid))
{
throw new ArgumentException("ContentType is required.");
}
JArray variantResults = GetVariantAliases(entries, contentTypeUid);
result["data-csvariants"] = variantResults.ToString(Formatting.None);
return result;
}
/// <summary>
/// Prefer <see cref="GetVariantMetadataTags(JObject, string)"/>. This alias exists for backward compatibility and will be removed in a future major release.
/// </summary>
[Obsolete("Use GetVariantMetadataTags instead. This method will be removed in a future major release.")]
public static JObject GetDataCsvariantsAttribute(JObject entry, string contentTypeUid)
{
return GetVariantMetadataTags(entry, contentTypeUid);
}
/// <summary>
/// Prefer <see cref="GetVariantMetadataTags(JArray, string)"/>. This alias exists for backward compatibility and will be removed in a future major release.
/// </summary>
[Obsolete("Use GetVariantMetadataTags instead. This method will be removed in a future major release.")]
public static JObject GetDataCsvariantsAttribute(JArray entries, string contentTypeUid)
{
return GetVariantMetadataTags(entries, contentTypeUid);
}
private static JArray ExtractVariantAliasesFromEntry(JObject entry)
{
JArray variantArray = new JArray();
JObject publishDetails = entry["publish_details"] as JObject;
if (publishDetails == null)
{
return variantArray;
}
JObject variants = publishDetails["variants"] as JObject;
if (variants == null)
{
return variantArray;
}
foreach (JProperty prop in variants.Properties())
{
if (prop.Value is JObject valueObj)
{
string alias = valueObj["alias"]?.ToString();
if (!string.IsNullOrEmpty(alias))
{
variantArray.Add(alias.Trim());
}
}
}
return variantArray;
}
}
}