1.0.3 • Published 3 years ago

cnc-structured-text v1.0.3

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
3 years ago

⎁ cnc-structured-text

Convert StructuredText to html.

Installation

npm install cnc-structured-text --save

Usage

const tohtml = require('cnc-structured-text');

const structuredtext = [{
    type: 'paragraph',
    text: 'Hi everyone, I am an awesome text!',
    spans: [
      {  start: 0, end: 11, type: 'strong' },
      {  start: 3, end: 11, type: 'em' },
      {  start: 13, end: 17, type: 'strong' },
      {  start: 17, end: 28, type: 'em' }
    ]
}];


tohtml(structuredtext, /* [options] */);
// -> <p><strong>Hi <em>everyone</em></strong>, <strong>I am</strong><em> an awesome</em> text!</p>

Options

tohtml(structuredtext, {

    // [optional] how to resolve internal links
    linkResolver: function(doc, isBroken) {

      // default resolver:
      if (isBroken) return '#broken';

      // if target document has a url field -> use it
      if (doc.document && doc.document.url) return doc.document.url;

      // otherwise fallback to id
      return '/' + doc.id;
    }

    // [optional] how to render fragments (Links, Images....)
    serializer: function() {
      // default serializer:

      // add target='_blank' on external links
      if (element.type === 'hyperlink' && element.url && element.url.substring(0, 4) === 'http') {
        return '<a href="' + element.url + '" target="_blank">' + content + '</a>';
      }

      return null;
    }
})
;