Sorry, no results found for "".

Legacy Next.js > 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:

1
{
2
"schema": "dast",
3
"document": {
4
"type": "root",
5
"children": [
6
{
7
"type": "heading",
8
"level": 1,
9
"children": [
10
{
11
"type": "span",
12
"marks": [],
13
"value": "Hello world!"
14
}
15
]
16
}
17
]
18
}
19
}

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

To take advantage of it, install the react-datocms package if you haven't already:

Terminal window
yarn add react-datocms

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:

1
import { request } from "../lib/datocms";
2
import { StructuredText } from "react-datocms";
3
4
const HOMEPAGE_QUERY = `query HomePage($limit: IntType) {
5
allBlogPosts(first: $limit) {
6
id
7
title
8
content {
9
value
10
}
11
}
12
}`;
13
14
export async function getStaticProps() {
15
const data = await request({
16
query: HOMEPAGE_QUERY,
17
variables: { limit: 10 }
18
});
19
20
return {
21
props: {
22
data
23
}
24
};
25
}
26
27
export default function Home({ data }) {
28
return (
29
<div>
30
{data.allBlogPosts.map(blogPost => (
31
<article key={blogPost.id}>
32
<h6>{blogPost.title}</h6>
33
<StructuredText data={blogPost.content} />
34
</article>
35
))}
36
</div>
37
);
38
}

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:

1
const HOMEPAGE_QUERY = `query HomePage($limit: IntType) {
2
allBlogPosts(first: $limit) {
3
id
4
title
5
content {
6
value
7
blocks {
8
__typename
9
... on ImageBlockRecord {
10
id
11
image { url alt }
12
}
13
}
14
links {
15
__typename
16
... on BlogPostRecord {
17
id
18
slug
19
title
20
}
21
}
22
}
23
}
24
}`;

We also need to tell <StructuredText /> how you want such nodes to be rendered:

1
return (
2
<StructuredText
3
data={blogPost.content}
4
renderInlineRecord={({ record }) => {
5
switch (record.__typename) {
6
case "BlogPostRecord":
7
return <a href={`/blog/${record.slug}`}>{record.title}</a>;
8
default:
9
return null;
10
}
11
}}
12
renderLinkToRecord={({ record, children }) => {
13
switch (record.__typename) {
14
case "BlogPostRecord":
15
return <a href={`/blog/${record.slug}`}>{children}</a>;
16
default:
17
return null;
18
}
19
}}
20
renderBlock={({ record }) => {
21
switch (record.__typename) {
22
case "ImageBlockRecord":
23
return <img src={record.image.url} alt={record.image.alt} />;
24
default:
25
return null;
26
}
27
}}
28
/>
29
);

To see structured text in action with Next.js, check out this tutorial: