Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sanity/block-content-to-html

Package Overview
Dependencies
Maintainers
7
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sanity/block-content-to-html

Customizable HTML parser for block editor output

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4K
decreased by-18.37%
Maintainers
7
Weekly downloads
 
Created
Source

block-content-to-html-js

Converts the flat Sanity block content structure into HTML.

Installation

npm install --save @sanity/block-content-to-html

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-html
const BlockContentToHtml = require('@sanity/block-content-to-html')
const toHtml = new BlockContentToHtml()

const html = toHtml.convert(data)

This will result in html being:

<p>String with an <em>italicized</em> word.</p>

Interface

The constructor will take an object for options:

const toHtml = new BlockContentToHtml(options: Object)

Converting the block content is done by calling .parse:

toHtml.convert(data: Array|Object)

Constructor options

Options is an object with any of the following keys:

customTypeHandlers: Object

An object with keys for your custom block types (which is not of type block). Each key is mapped to a type, and their value is a function which will get the node as input. It returns HTML.

customTypeHandlers: {
  author: node => {
    return `<div>${node.attributes.name}</div>`
  }
}

The node in this example has the following structure:

{ type: 'author', attributes: { name: 'Test Person' } }
blockTypeHandlers: Object

Handlers for manipulating the output of the default, built in, block types. The default block type holds either a block of text or a list. A text block is built up of spans (with marks), where a list block is built up of list items, which can contain a text block.

The blockTypeHandlers object can contain the follow keys:

  • textBlock: Object

    Each text block has a style. With this option you can manipulate how each style is rendered. By default the style normal is wrapped in a <p>, where other default styles are mapped 1:1 (style h2 produces <h2>).

    The option works in a similar way as customTypeHandlers described above, with the distinction that is has a children property, and the key is the style name:

    textBlock: {
      normal: node => {
        return `<p class="funky-paragraph">${node.children}</p>`
      },
      h2: node => {
        return `<div class="big-heading">${node.children}</div>`
      }
    }
    
  • listBlock: Object

    By default lists are rendered with plain <ol>, <ul>and <li> tags. With this option you can tweak them into your own liking.

    The object takes the following keys:

    listBlock: {
      number: node => {
        return `<ol class="article-list">${node.children}</ol>`
      },
      bullet: node => {
        return `<ul class="article-list">${node.children}</ul>`
      },
      listItem: node => {
        return `<li class="article-list-item">${node.children}</li>`
      }
    }
    
  • span: Object

    Let you tweak how spans within blocks are rendered. By default the spans are just text and marks. As spans may have attributes with data, you can make your own render which manipulates the output based on their attributes:

    span: node => {
      let result = ''
      if (node.attributes.author) {
        result = `
          <div class="author-bio">
            <img src="${node.attributes.author.image.url}" />
            ${node.attributes.author.name}
          </div>`
      }
      if (node.attributes.link) {
        result += `<a href="${node.attributes.link.href}">${node.children}</a>`
      }
      if (Object.keys(node.attributes).length === 0) {
        result = node.children
      }
      return result
    }
    
  • marks: Object

    Marks are by default mapped 1:1. If the mark is 'em' the output will be <em>. With this option you can map marks to other tags, or just ignore them:

    marks: {
      em: null // Just igonore 'em' tags.
      code: 'pre' // Render 'code' marks to 'pre' tags
    }
    

Utility function

BlockContentToHtml.escapeHtml(unsafe: string)

Escape unsafe text to HTML safe text. To be used with your own handler functions.

More information / examples

Please see the tests.

License

MIT-licensed

FAQs

Package last updated on 06 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc