| name | contentstack-utils |
|---|---|
| description | Use for the contentstack.utils NuGet API—Utils, GQL, Options, variants, converters, code flows, and integration boundaries. |
- Implementing or extending JSON RTE → HTML rendering or embedded content behavior.
- Adding node types, converters, or variant/metadata helpers.
- Choosing whether logic belongs in this package vs the Contentstack .NET Delivery SDK.
- NuGet package id:
contentstack.utils(Contentstack.Utils.csproj). - Root namespace:
Contentstack.Utils. Sub-namespaces follow folders:Contentstack.Utils.Models,Contentstack.Utils.Interfaces,Contentstack.Utils.Enums,Contentstack.Utils.Extensions,Contentstack.Utils.Converters.
Source files under Contentstack.Utils/Models/:
JsonRTENode.cs,JsonRTENodes.cs— GQL-shaped RTE wrappers.Metadata.cs— embed metadata; implicit conversions from HtmlAgilityPackHtmlNodeand RTENode.Node.cs,TextNode.cs— RTE tree nodes.Options.cs— default rendering; subclass for custom HTML.
IEntryEmbedable—IEntryEmbedable.cs:embeddedItemsmap for resolving references.IEmbeddedObject,IEmbeddedEntry,EditableEntry,IEmbeddedAsset—IEmbeddedObject.cs.IRenderable,NodeChildrenCallBack—IOptions.cs.IEdges<T>—IEdges.cs(GQL edge list). SpellingIEntryEmbedablematches the existing public API.
Utils.cs exposes static helpers including:
JsonToHtml: Overloads forNode,List<Node>— see Code flows.RenderContent: Overloads forstringandList<string>HTML — see Code flows.Utils.GQL:JsonToHtmlforJsonRTENode<T>/JsonRTENodes<T>whereT : IEmbeddedObject— see Code flows.addEditableTags: Adds Live Preview–styledata-cslp(or object-shaped) metadata onEditableEntrygraphs for a given content type and locale (seeUtils.addEditableTags).- Variants / metadata:
GetVariantAliases— reads each entry’spublish_details→variantsobject and collects non-emptyaliasvalues per variant entry (implementation:ExtractVariantAliasesFromEntryinUtils.cs, approx. lines 401–426).GetVariantMetadataTags— wraps alias data into aJObjectwith keydata-csvariants(compact JSON string).GetDataCsvariantsAttribute— Obsolete; useGetVariantMetadataTagsinstead (same behavior; see XML comments onUtils).
Subclass or configure Options.cs (Options : IRenderable) to override:
RenderOption(IEmbeddedObject, Metadata)RenderNode(string nodeType, Node, NodeChildrenCallBack)RenderMark(MarkType, string text, string className, string Id)
NodeJsonConverter.csandRTEJsonConverter.csintegrate with Newtonsoft.Json for RTE node graphs. New node shapes should follow the same converter and model patterns.
- HtmlAgilityPack: HTML load, query, and embedded-object replacement for
RenderContent. - Newtonsoft.Json: JSON models (
JObject,JArray, converters). Upgrades should stay compatible with consumers and pass Snyk/CI.
- This library does not ship an HTTP client for Contentstack Delivery or Management APIs. Apps typically use Contentstack .NET SDK (or REST) to fetch entries, then use Utils to render RTE HTML or process JSON. Keep HTTP, auth, and caching in application or SDK layers.
- Signatures and full behavior live in C# under
Contentstack.Utils/. This skill summarizes; when in doubt, read the implementation (especiallyUtils.cs).
Stepwise behavior with pointers into Utils.cs. Line numbers are approximate; verify against the file after large edits.
JsonToHtml(Node, Options)(approx. lines 97–113) builds areferenceToHtmldelegate: forreferencenodes it callsfindEmbeddedObjectusingoptions.entry.embeddedItems, thenoptions.RenderOptionwhen a match exists.nodeChildrenToHtml(lines 115–118) concatenates HTML for each child vianodeToHtml.nodeToHtml(lines 121–131):type == "text"→textToHtml(lines 133+), which applies marks in order viaRenderMark;type == "reference"→referenceToHtml; otherwise →options.RenderNodewith a callback that recurses intonodeChildrenToHtml.
- Reuses
nodeChildrenToHtml/nodeToHtml.referenceToHtmlis built byGQL.refernceToHtml(lines 32–50): findIEdges<T>whereedge.NodematchesMetadata.ItemUidandContentTypeUid, thenoptions.RenderOption(edge.Node, metadata). Entry points:GQL.JsonToHtml(JsonRTENode<T>, ...)(lines 17–20) and list overload (lines 22–30).
RenderContent(string, Options)(lines 64–82):HtmlDocument.LoadHtml, thenFindEmbeddedObjectextension onHtmlDocumentExtension.cs(selects nodes with classembedded-assetorembedded-entry).- Each HtmlNode is passed to the callback;
Metadatais produced via implicit conversion fromHtmlNode(Metadata.cs, lines 46–68). findEmbeddedObjectresolvesIEmbeddedObjectfromoptions.entry.embeddedItems; result string replacesmetadata.OuterHTMLin the accumulated HTML.
ExtractVariantAliasesFromEntry: requiresentry["publish_details"]asJObject, thenpublish_details["variants"]asJObject; for each property, readsaliasfrom the nested object when present (lines 401–426).GetVariantMetadataTagsbuilds{ "data-csvariants": <compact JSON string> }fromGetVariantAliasesresults (lines 365–380).
flowchart TD
rteEntry[JsonToHtml_Node_or_List]
rteEntry --> nodeChildren[nodeChildrenToHtml]
nodeChildren --> nodeTo[nodeToHtml]
nodeTo -->|text| textTo[textToHtml_RenderMark_chain]
nodeTo -->|reference| refDel[referenceToHtml_findEmbeddedObject_RenderOption]
nodeTo -->|other_types| renderNode[RenderNode_recursive_callback]
flowchart TD
htmlEntry[RenderContent_string]
htmlEntry --> load[HtmlDocument_LoadHtml]
load --> find[FindEmbeddedObject_extension]
find --> meta[HtmlNode_to_Metadata_implicit]
meta --> resolve[findEmbeddedObject_embeddedItems]
resolve --> replace[Replace_OuterHTML_with_RenderOption]
- Product README — install and usage examples.
- Contentstack .NET Utils on NuGet — package listing (verify version).