SvelteKit

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:

Terminal window
yarn add @datocms/svelte

Now let's make a GraphQL query to fetch a Structured Text field:

src/routes/+page.server.ts
const query = `
query HomeQuery {
blogPost {
title
content {
value
}
}
}
`;
export const load = () => {
return executeQuery(query);