1.0.6 • Published 3 years ago

@jswork/next-slate-serialize v1.0.6

Weekly downloads
4
License
MIT
Repository
-
Last release
3 years ago

next-slate-serialize

Serializing/deserializing html/text for slate.

version license size download

installation

npm install -S @jswork/next-slate-serialize

apis

apiparamsdescription
parseinNodes, inOptionsTransform nodes to html.

usage

import NxSlateSerialize from '@jswork/next-slate-serialize';

// apis
const nodes = [
  {
    children: [
      {
        type: 'paragraph',
        children: [
          { text: 'An opening paragraph with a ' },
          {
            type: 'link',
            url: 'https://example.com',
            children: [{ text: 'link' }]
          },
          { text: ' in it.' }
        ]
      },
      {
        type: 'quote',
        children: [{ text: 'A wise quote.' }]
      },
      {
        type: 'paragraph',
        children: [{ text: 'A closing paragraph!' }]
      }
    ]
  }
];

const options = {
  process: (node, children) => {
    if (!children) return node.text;
    switch (node.type) {
      case 'quote':
        return `<blockquote><p>${children}</p></blockquote>`;
      case 'paragraph':
        return `<p>${children}</p>`;
      case 'link':
        return `<a href="${node.url}">${children}</a>`;
      default:
        return children;
    }
  },
  joined: ''
};

const html = NxSlateSerialize.parse(nodes, options);
// <p>An opening paragraph with a <a href="https://example.com">link</a> in it.</p><blockquote><p>A wise quote.</p></blockquote><p>A closing paragraph!</p>

resources

license

Code released under the MIT license.