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!"}]}]}
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."}]}]}
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!