Svelte/SvelteKit > Structured Text fields

Structured Text fields

Rich text in DatoCMS is stored in Structured Text fields, which lets us use it in many different contexts, from HTML in the browser to speech fulfillments in voice interfaces, if that's what you want.

There's a lot to be said about Structured Text and the extensibility of it, but for now let's just say that it returns content in a particular JSON format called dast which will resemble this example:

{
"schema": "dast",
"document": {
"type": "root",
"children": [
{
"type": "heading",
"level": 1,
"children": [
{
"type": "span",
"marks": [],
"value": "Hello world!"
}
]
}
]
}
}

To make it easy to convert this format in HTML inside your Svelte projects, we released a package called @datocms/svelte that exposes a <StructuredText /> component that does all the tedious work for you.

To take advantage of it, install the @datocms/svelte package if you haven't already:

yarn add @datocms/svelte

Then, inside your page, make a GraphQL query to fetch a Structured Text field, and feed the result to the data prop of a <StructuredText /> component:

<script>
import { onMount } from 'svelte';
import { StructuredText } from '@datocms/svelte';
const query = `
query {
blogPost {
title
content {
value
}
}
}
`;
export let data = null;
onMount(async () => {
const response = await fetch('https://graphql.datocms.com/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: "Bearer faeb9172e232a75339242faafb9e56de8c8f13b735f7090964",
},
body: JSON.stringify({ query })
})
const json = await response.json()
data = json.data;
});
</script>
<article>
{#if data}
<h1>{{ data.blogPost.title }}</h1>
<StructuredText data={data.blogPost.content} />
{/if}
</article>

Rendering special nodes

Other than "regular" formatting nodes (paragraphs, lists, etc.), Structured Text documents can contain three special types of node:

  • itemLink nodes are just like regular HTML hyperlinks, but point to other records instead of URLs;

  • inlineItem nodes lets you directly embed a reference to a record in-between regular text;

  • block nodes lets you embed a DatoCMS block record in-between regular paragraphs;

If a Structured Text document contains one of these nodes, then we need to change the GraphQL query, so that we also fetch all the records and blocks it references. As an example, if the field can link to other Blog posts, and can embed blocks of type "Image block", then the query should change like this:

const HOMEPAGE_QUERY = `query HomePage($limit: IntType) {
allBlogPosts(first: $limit) {
id
title
content {
value
blocks {
__typename
... on ImageBlockRecord {
id
image { url alt }
}
}
links {
__typename
... on BlogPostRecord {
id
slug
title
}
}
}
}
}`;

You must also tell <StructuredText /> how to render such nodes. By using the components prop, you can declare an array of tuples composed of a predicate (a predicate is a function that takes one item as input and returns either true or false based on whether the item satisfies some condition) and a component: the predicate receives a node, and when it returns true, the custom component declared will be used to render the node:

<script>
import { isBlock, isInlineItem, isItemLink } from 'datocms-structured-text-utils';
import { StructuredText } from '@datocms/svelte';
import Block from './Block.svelte';
import InlineItem from './InlineItem.svelte';
import ItemLink from './ItemLink.svelte';
</script>
<StructuredText
data={blogPost.content}
components={[
[isInlineItem, InlineItem],
[isItemLink, ItemLink],
[isBlock, Block]
]}
/>