This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@sanity/block-content-to-tree

0.3.0 • Public • Published

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

// The flat block content structure
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": []
    }
  ]
}


// Now convert it with block-content-to-tree
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' // Text style
  mark: 'em', // Mark indicating all content is wrapped in bold.
  content: [
    'Hello world! ', // Span with no marks or attributes (plain text).
    {
      type: 'span',
      // Data attributes for this span
      attributes: {link: {href: 'http://...'}},
      mark: 'strong',
      // The  text content for the span
      // Always an array with a single plain text.
      // An array to keep format consistent with block.content.
      content: [
        'I am a link!'
      ]
    }
  ]
}

List

Root element

{
  type: 'list',
  itemStyle: 'bullet' // The list style
  // Items are blocks (see above)
  items: [
    {block},
    {block}
  ]
}

Custom blocks

{
  type: 'author' // Whatever, but never 'block' (built in type)
  attributes: { // Custom attributes for the type
    name: 'John Ronald Reuel Tolkien',
  }
}

More information / examples

Please see the tests.

License

MIT-licensed

Readme

Keywords

none

Package Sidebar

Install

npm i @sanity/block-content-to-tree

Weekly Downloads

6

Version

0.3.0

License

MIT

Last publish

Collaborators

  • armandocerna
  • daniel.malmer
  • jordanl17
  • tambet
  • jtpetty
  • drewsanity
  • refiito
  • sergeisarviro
  • ash
  • indrek.karner
  • cngonzalez-sanity
  • rdunk
  • rneatherway-sanity
  • ricokahler
  • pedro-sanity
  • jonabc
  • kenjonespizza
  • pauloborgesf
  • binoy14
  • simen.svale
  • svirs
  • josh_sanity_io
  • joneidejohnsen
  • nina.andal
  • rankers
  • snorreeb
  • mattcraig
  • vincentquigley
  • stipsan
  • michael-sanity
  • rubioz
  • tonina
  • ritasdias
  • simeonsanity
  • kmelve
  • bjoerge
  • rexxars
  • skogsmaskin
  • robinpyon
  • mariuslundgard
  • sanity-io
  • evenw
  • radhe_sanity
  • rbotten
  • judofyr
  • obliadp
  • dcilke
  • fredcarlsen
  • hermanw
  • sgulseth
  • atombender