Plugin SDK > Structured Text customizations

Structured Text customizations

Structured Text is a consciously simple format, with a very small number of possible nodes — only the ones that are really helpful to capture the semantics of a standard piece of content, and with zero possibility to introduce styling and break the decoupling of content from presentation.

This is generally a very good thing, as it makes working on the frontend extremely simple and predictable: unlike HTML or Markdown, you don't have to be defensive and worry about some complex nesting of tags that you'd never think it could be possible, or unwanted styles coming from the editors.

There are, however, situations where it is critical to be able to add a small, controlled set of styles to your content, to represent nuances of different semantics within a piece of content.

Adding custom styles to nodes

Let's take this article as an example:

The third paragraph is conceptually similar to the others, but it's obviously more important, and we want the reader to pay more attention to what it says.

In these cases, we can use plugins to specify alternative styles for paragraph and heading nodes using the customBlockStylesForStructuredTextField hook:

import { connect, Field, FieldIntentCtx } from 'datocms-plugin-sdk';
connect({
customBlockStylesForStructuredTextField(field: Field, ctx: FieldIntentCtx) {
return [
{
id: 'emphasized',
node: 'paragraph',
label: 'Emphasized',
appliedStyle: {
fontFamily: 'Georgia',
fontStyle: 'italic',
fontSize: '1.4em',
lineHeight: '1.2',
}
}
];
},
});

The code above will add a custom "emphasized" style for paragraph nodes to every Structured Text field in the project. The appliedStyle property lets you customize how the style will be rendered inside of DatoCMS, when the user selects it:

You can also use the first argument of the hook (field) to only allow custom styles in some specific Structured Text fields. If that's the case, you'll probably want to add some settings to the plugin to let the final user decide which they are:

customBlockStylesForStructuredTextField(field: Field, ctx: FieldIntentCtx) {
const { fieldsInWhichAllowCustomStyles } = ctx.plugin.attributes.parameters;
if (!fieldsInWhichAllowCustomStyles.includes[field.attributes.api_key)) {
// No custom styles!
return [];
}
return [
{
id: 'emphasized',
node: 'paragraph',
// ...
},
];
}

The final Structured Text value will have the custom style applied in the style property:

{
"type": "root",
"children": [
{
"type": "paragraph",
"style": "emphasized",
"children": [
{
"type": "span",
"value": "Hello!"
}
]
}
]
}

Adding custom marks

The default Structured Text editor already supports a number of different marks (strong, code, underline, highlight, etc.), but you might want to annotate parts of the text using custom marks.

An example would be adding a "spoiler" mark, to signal a portion of text that we don't want to show the visitor unless they explicitly accept a spoiler alert.

The customMarksForStructuredTextField hook lets you do exactly that:

import { connect, Field, FieldIntentCtx } from 'datocms-plugin-sdk';
connect({
customMarksForStructuredTextField(field: Field, ctx: FieldIntentCtx) {
return [
{
id: 'spoiler',
label: 'Spoiler',
icon: 'bomb',
keyboardShortcut: 'mod+shift+l',
appliedStyle: {
backgroundColor: 'rgba(255, 0, 0, 0.3)',
},
},
];
},
});

The code above will add a custom "spoiler" mark to every Structured Text field in the project. The appliedStyle property lets you customize how the style will be rendered inside of DatoCMS, when the user selects it:

The final result on the Structured Text value will be the following:

{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "span",
"value": "In the "
},
{
"type": "span",
"marks": ["spoiler"],
"value": "final killing scene"
},
{
"type": "span",
"value": ", the director really outdid himself."
}
]
}
]
}

You're in charge of the frontend!

All of our Structured Text management libraries (React, Vue, etc.) allow you to specify custom rendering rules. When working with custom styles and marks, it's up to your frontend to decide how to render them!


Function Reference

customMarksForStructuredTextField()

Return value

The function must return StructuredTextCustomMark[] | undefined.

Context object

The following properties and methods are available in the ctx argument:

customBlockStylesForStructuredTextField()

Return value

The function must return StructuredTextCustomBlockStyle[] | undefined.

Context object

The following properties and methods are available in the ctx argument: