Developer docs / Integrations
Rich text JSON
The rich text document: its nodes and marks, how the content API delivers it, and how to draw it.
The document
Why: a site, an app and the storefront each draw rich text their own way, so it is stored as typed data, never as HTML: nothing in it can carry a script or a style, and every reader decides how a heading or a link looks. How: a rich text field holds a document, a tree of nodes in the shape of Contentful's rich text. Every node has nodeType, data and content; a text node has value and marks instead of content.
{
"nodeType": "document", "data": {},
"content": [
{ "nodeType": "heading-2", "data": {}, "content": [{ "nodeType": "text", "value": "Care", "marks": [], "data": {} }] },
{ "nodeType": "paragraph", "data": {}, "content": [
{ "nodeType": "text", "value": "Wash ", "marks": [], "data": {} },
{ "nodeType": "text", "value": "by hand", "marks": [{ "type": "bold" }], "data": {} },
{ "nodeType": "hyperlink", "data": { "uri": "/care" }, "content": [{ "nodeType": "text", "value": " (more)", "marks": [], "data": {} }] }
] },
{ "nodeType": "embedded-entry-block", "content": [], "data": { "target": { "sys": { "type": "Link", "linkType": "Entry", "id": "ce_123" } } } }
]
}| Node | Holds |
|---|---|
| paragraph, heading-2, heading-3, heading-4 | Inline nodes: text, hyperlink, entry-hyperlink, embedded-entry-inline. A \n in a text value is a line break. |
| unordered-list, ordered-list | list-item nodes; a list-item holds a paragraph, then nested lists (6 levels at most) |
| blockquote | Paragraphs |
| hr | Nothing |
| code-block | One text node, its lines joined with \n, no marks |
| table | table-row nodes, each holding table-cell or table-header-cell nodes, each holding paragraphs (100 rows, 20 columns at most) |
| hyperlink | Text; data.uri is https://, http://, mailto:, a /path or a #section |
| entry-hyperlink | Text; data.target.sys.id names an entry |
| embedded-entry-block, embedded-entry-inline | Nothing; data.target.sys.id names an entry |
| embedded-asset-block | Nothing; data.src is an /images/ path of the store (or an https URL) and data.alt its alt text |
Marks are bold, italic, underline, strikethrough and code. Headings stop at 2 because the page title is the h1. Differences from Contentful: there is a code-block node, and an image carries its path and alt text in data, since assets here are addressed by path, as media fields are.
Writing rich text
Why: a document that a field does not allow must not reach a page, whichever client wrote it. How: the owner API and the import check every rich text value. An unknown node, a link to javascript: or another unsafe target, an image from elsewhere, a node or mark the field turned off, or an embedded entry of a type the field does not embed is refused with 400 bad_content, naming the field and where in the document.
- A field's rules are in its config.validations: nodes (the node types from the table above that it allows), marks, and embeds (content type keys an embed or entry link may name). A missing list allows everything of that kind.
- The older form, { "paragraphs": [ "text", { "runs": [{ "text": "...", "bold": true, "italic": true, "href": "..." }] } ] }, and plain text (a blank line between paragraphs) are still taken and stored as the document.
- Limits: 100,000 characters and 5,000 nodes per value.
- Embedded entries and entry links count as references: the entry they name must exist, and it cannot be deleted while a rich text uses it.
Delivery
Why: a client written before the document must keep working, and a new one should get every heading, list and table. How: GET /api/content/entries answers rich text fields as the document, old stored values converted. Entries a document embeds or links to count as links, so include=1 puts them in includes.entries. Add rich_text=paragraphs to get the older { paragraphs } form instead; headings, list items, quotes and table cells come out as paragraphs then.
GET /api/content/page, /api/content/entry/:id and /api/content/product/:slug draw a page as the storefront does: a rich text item has the document as its value and, in links, each embedded or linked entry that is published, by id, with its type, title, page path and fields.
GET https://<store>/api/content/entries?content_type=article&include=1
GET https://<store>/api/content/entries?content_type=article&rich_text=paragraphsDrawing it
- Walk the tree and draw each node as its element: p, h2 to h4, ul and ol with li, blockquote, hr, pre and code, table with tr, th and td, strong, em, u, s and code for marks.
- Write every value as text, never as HTML, and check a link's target again before drawing it; an unknown node draws nothing.
- Draw an embedded entry or an entry link only when you can resolve the entry (a published one); otherwise draw the link's words and skip the embed.
Converting stored values
Readers convert old values as they read them, and each save stores the document, so nothing has to be done. To give a store's data one shape at once, POST /api/owner/content/rich-text/convert rewrites every old value in entries, translations and variants (cms.types.write). Add ?dry_run=1 to count first. Running it again changes nothing.