block-content-to-tree-js
Converts the flat Sanity block content structure into a generic tree structure for easier transformation into other formats (HTML, React etc).
Installation
npm install --save @sanity/block-content-to-tree
Quick example
const data = {
"_type": "block",
"style": "normal",
"spans": [
{
"_type": "span",
"text": "String with an ",
"marks": []
},
{
"_type": "span",
"text": "italicized",
"marks": [
"em"
]
},
{
"_type": "span",
"text": " word.",
"marks": []
}
]
}
const BlockContentToTree = require('@sanity/block-content-to-tree')
const blockContentToTree = new BlockContentToTree()
const tree = blockContentToTree.convert(data)
This will result in the variable tree
being:
{
type: 'block',
style: 'normal',
content: [
'String with an ',
{
type: 'span',
attributes: {},
mark: 'em',
content: [
'italicized'
]
},
' word.'
]
}
Entities
Block (text)
{
type: 'block',
style: 'normal'
mark: 'em',
content: [
'Hello world! ',
{
type: 'span',
attributes: {link: {href: 'http://...'}},
mark: 'strong',
content: [
'I am a link!'
]
}
]
}
List
Root element
{
type: 'list',
itemStyle: 'bullet'
items: [
{block},
{block}
]
}
Custom blocks
{
type: 'author'
attributes: {
name: 'John Ronald Reuel Tolkien',
}
}
More information / examples
Please see the tests.
License
MIT-licensed