How to use the @sanity/block-tools.htmlToBlocks function in @sanity/block-tools

To help you get started, we’ve selected a few @sanity/block-tools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sanity-io / sanity / packages / example-studio / components / FunkyEditor / FunkyEditor.js View on Github external
function handlePaste(input) {
  const {event, type, path} = input
  const html = event.clipboardData.getData('text/html')
  // check if schema has the code type
  const hasCodeType = type.of.map(({name}) => name).includes('code')
  if (!hasCodeType) {
    console.log('Run `sanity install @sanity/code-input, and add `type: "code"` to your schema.')
  }
  if (html && hasCodeType) {
    const blocks = blockTools.htmlToBlocks(html, type, {
      rules: [
        {
          deserialize(el, next, block) {
            /**
             *  `el` and `next` is DOM Elements
             * learn all about them:
             * https://developer.mozilla.org/en-US/docs/Web/API/Element
             **/

            if (!el || !el.children || (el.tagName && el.tagName.toLowerCase() !== 'pre')) {
              return undefined
            }
            const code = el.children[0]
            const childNodes =
              code && code.tagName.toLowerCase() === 'code' ? code.childNodes : el.childNodes
            let text = ''
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor / plugins / PastePlugin.ts View on Github external
return wait(100).then(() => {
    onProgress({status: 'html'})
    const blocks = blockTools.htmlToBlocks(html, blockContentType)
    onProgress({status: 'blocks'})
    const value = deserialize(blocks, blockContentType)
    pasteController.setValue(value)
    ensureNoPlaceholder(editor)
    editor.insertFragment(pasteController.value.document)
    pasteController.setValue(deserialize(null, blockContentType))
    onProgress({status: null})
    return editor
  })
}
github sanity-io / sanity / packages / @sanity / form-builder / src / inputs / BlockEditor-slate / plugins / onPasteHtml.js View on Github external
function onPaste(event, data, change) {
    if (data.type != 'html') {
      return null
    }
    if (data.isShift) {
      return null
    }
    const blockContentType = blockEditor.props.type
    const blocks = blockTools.htmlToBlocks(data.html, {blockContentType})
    const {document} = blockTools.blocksToSlateState(blocks, blockContentType)
    return change.insertFragment(Document.fromJSON(document))
  }