 # datocms-structured-text-to-html-string HTML renderer for the DatoCMS Structured Text field type. ## Installation Using [npm](http://npmjs.org/): ```sh npm install datocms-structured-text-to-html-string ``` Using [yarn](https://yarnpkg.com/): ```sh yarn add datocms-structured-text-to-html-string ``` ## Usage ```javascript import { render } from 'datocms-structured-text-to-html-string'; render({ schema: 'dast', document: { type: 'root', children: [ { type: 'paragraph', children: [ { type: 'span', value: 'Hello world!', }, ], }, ], }, }); // ->
Hello world!
render({ type: 'root', children: [ { type: 'paragraph', content: [ { type: 'span', value: 'Hello', marks: ['strong'], }, { type: 'span', value: ' world!', marks: ['underline'], }, ], }, ], }); // ->Hello world!
``` You can pass custom renderers for nodes and text as optional parameters like so: ```javascript import { render, renderNodeRule } from 'datocms-structured-text-to-html-string'; import { isHeading } from 'datocms-structured-text-utils'; const structuredText = { type: 'root', children: [ { type: 'heading', level: 1, content: [ { type: 'span', value: 'Hello world!', }, ], }, ], }; const options = { renderText: (text) => text.replace(/Hello/, 'Howdy'), customNodeRules: [ renderNodeRule( isHeading, ({ adapter: { renderNode }, node, children, key }) => { return renderNode(`h${node.level + 1}`, { key }, children); }, ), ], customMarkRules: [ renderMarkRule('strong', ({ adapter: { renderNode }, children, key }) => { return renderNode('b', { key }, children); }), ], }; render(document, options); // ->A record hyperlink and an inline record: Foo
//