4.0.2 • Published 19 days ago

datocms-structured-text-to-plain-text v4.0.2

Weekly downloads
256
License
MIT
Repository
github
Last release
19 days ago

Node.js CI

datocms-structured-text-to-plain-text

Plain text renderer for the Structured Text document.

Installation

Using npm:

npm install datocms-structured-text-to-plain-text

Using yarn:

yarn add datocms-structured-text-to-plain-text

Usage

import { render } from 'datocms-structured-text-to-plain-text';

const structuredText = {
  value: {
    schema: 'dast',
    document: {
      type: 'root',
      children: [
        {
          type: 'heading',
          level: 1,
          children: [
            {
              type: 'span',
              value: 'This\nis a\ntitle!',
            },
          ],
        },
      ],
    },
  },
};

render(structuredText); // -> "This is a title!"

You can also pass custom renderers for itemLink, inlineItem, block as optional parameters like so:

import { render } from 'datocms-structured-text-to-plain-text';

const graphqlResponse = {
  value: {
    schema: 'dast',
    document: {
      type: 'root',
      children: [
        {
          type: 'paragraph',
          children: [
            {
              type: 'span',
              value: 'A ',
            },
            {
              type: 'itemLink',
              item: '344312',
              children: [
                {
                  type: 'span',
                  value: 'record hyperlink',
                },
              ],
            },
            {
              type: 'span',
              value: ' and an inline record: ',
            },
            {
              type: 'inlineItem',
              item: '344312',
            },
          ],
        },
        {
          type: 'block',
          item: '812394',
        },
      ],
    },
  },
  blocks: [
    {
      id: '812394',
      image: { url: 'http://www.datocms-assets.com/1312/image.png' },
    },
  ],
  links: [{ id: '344312', title: 'Foo', slug: 'foo' }],
};

const options = {
  renderBlock({ record }) {
    return `[Image ${record.image.url}]`;
  },
  renderInlineRecord({ record, adapter: { renderNode } }) {
    return `[Inline ${record.slug}]${children}[/Inline]`;
  },
  renderLinkToRecord({ record, children, adapter: { renderNode } }) {
    return `[Link to ${record.slug}]${children}[/Link]`;
  },
};

render(document, options);
// -> A [Link to foo]record hyperlink[/Link] and an inline record: [Inline foo]Foo[/Inline]
//    [Image http://www.datocms-assets.com/1312/image.png]
4.1.0-alpha.0

19 days ago

4.0.2

3 months ago

4.0.1

8 months ago

4.0.0

10 months ago

3.0.2

11 months ago

3.0.1

11 months ago

3.0.0

1 year ago

2.1.12

1 year ago

2.0.3

3 years ago

2.0.4

3 years ago

1.2.3-alpha.0

3 years ago

1.2.3-alpha.1

3 years ago

1.3.0

3 years ago

1.2.3-alpha.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.0-alpha.1

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.10

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

0.1.0-alpha.27

4 years ago

0.1.0-alpha.23

4 years ago

0.1.0-alpha.25

4 years ago

0.1.0-alpha.24

4 years ago

0.1.0-alpha.26

4 years ago

0.1.0-alpha.21

4 years ago

0.1.0-alpha.20

4 years ago

0.1.0-alpha.22

4 years ago

0.1.0-alpha.19

4 years ago

0.1.0-alpha.17

4 years ago

0.1.0-alpha.16

4 years ago