@jswork/next-slate-serialize

1.0.6 • Public • Published

next-slate-serialize

Serializing/deserializing html/text for slate.

version license size download

installation

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

apis

api params description
parse inNodes, inOptions Transform 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.

Package Sidebar

Install

npm i @jswork/next-slate-serialize

Weekly Downloads

3

Version

1.0.6

License

MIT

Unpacked Size

6.91 kB

Total Files

5

Last publish

Collaborators

  • afeiship