1.0.2 • Published 5 years ago

md-to-bemjson v1.0.2

Weekly downloads
5
License
MPL-2.0
Repository
github
Last release
5 years ago

md-to-bemjson

Converts markdown to bemjson.

NPM Status Travis Status Coverage Status Dependency Status Greenkeeper badge

Requirements

Install

$ npm install md-to-bemjson

Usage

const toBemjson = require('md-to-bemjson').convertSync;
const bjson = toBemjson('# Hello world');

console.log(JSON.stringify(bjson, null, 4));

Yields:

{
    "block": "md-root",
    "content": {
        "block": "heading",
        "content": "Hello world",
        "level": 1,
        "mods": {
            "level": 1
        }
    }
}

Markdown converter to bemjson

Module use remark with several plugins and custom compiler to convert markdown to bemjson. Plugins divided into two groups: necessary(you can't disable this plugins) and optional.

Necessary plugins:

Optional plugins:

Compiler

API

constructor([options])

Options

ParameterTypeDescription
githubObject, booleanEnables github support with remark plugin remark-github. Default false.
exportTypeenumremark-bemjson option. Exports to certain type with .stringify. Supported exports.
exportNamestringremark-bemjson option. Used with exportType=(modules, umd, YModules) stringify bemjson with exported given name.
augmentFunction, ObjectOptions for augmentation resulting bemjson by every node. As function accepts bemNode and must return it.
pluginsArrayOptions for additional plugins to be included. Plugin format: { plugin: Function, options: Object }

Options.augment

ParameterTypeDescription
prefixstringAdd prefix to all blocks. Important: for root replace original prefix.
scopestringReplace root block with scope. And replace all blocks with elems.
mapObjectReplace block names with provided in map. Available blocks.
htmlObjectOptions for converting html to bemjson with html2bemjson.

Important: Augmentation flow is serial. Order: map, prefix, scope. Important: Other augmentations does not affect html.

convert(markdown) => Promise

ParameterTypeDescription
markdownstringMarkdown text

Asynchronously converts markdown to bemjson.

const Converter = require('md-to-bemjson');
const md2Bemjson = new Converter();

md2Bemjson.convert('# Hello world').then(bjson => console.log(JSON.stringify(bjson, null, 4)))

Yields:

{
    "block": "md-root",
    "content": {
        "block": "heading",
        "content": "Hello world",
        "level": 1,
        "mods": {
            "level": 1
        }
    }
}

convertSync(markdown) => Bemjson

ParameterTypeDescription
markdownstringMarkdown text

Synchronously converts markdown to bemjson.

const Converter = require('md-to-bemjson');
const md2Bemjson = new Converter();

console.log(JSON.stringify(md2Bemjson.convertSync('# Hello world'), null, 4));

Yields:

{
    "block": "md-root",
    "content": {
        "block": "heading",
        "content": "Hello world",
        "level": 1,
        "mods": {
            "level": 1
        }
    }
}

stringify(markdown [, options]) => Promise

ParameterTypeDescription
markdownstringMarkdown text
optionsObjectOptions prefixed with export*. Important: Creates new processor. For better performance set options via constructor.

Asynchronously converts and stringify markdown to bemjson module with exports.

const Converter = require('md-to-bemjson');
const md2Bemjson = new Converter();

md2Bemjson.stringify('# Hello world').then(content => console.log(content))

Yields:

module.exports = {
    block: 'md-root',
    content: {
        block: 'heading',
        content: 'Hello world',
        "level": 1,
        mods: {
            'level': 1
        }
    }
};

stringifySync(markdown [, options]) => String

ParameterTypeDescription
markdownstringMarkdown text
optionsObjectOptions prefixed with export*. Important: Creates new processor. For better performance set options via constructor.

Synchronously converts and stringify markdown to bemjson module with exports.

const Converter = require('md-to-bemjson');
const md2Bemjson = new Converter();

console.log(md2Bemjson.stringifySync('# Hello world'));

Yields:

module.exports = {
    block: 'md-root',
    content: {
        block: 'heading',
        content: 'Hello world',
        level: 1,
        mods: {
            'level': 1
        }
    }
};

static convert(markdown [, options]) => Promise

ParameterTypeDescription
markdownstringMarkdown text
optionsObjectplugin options

Asynchronously converts markdown to bemjson.

const toBemjson = require('md-to-bemjson').convert;

toBemjson('# Hello world').then(bjson => console.log(JSON.stringify(bjson, null, 4)))

Yields:

{
   "block": "md-root",
   "content": {
       "block": "heading",
       "content": "Hello world",
       "level": 1,
       "mods": {
           "level": 1
       }
   }
}

static convertSync(markdown [, options]) => Bemjson

ParameterTypeDescription
markdownstringMarkdown text
optionsObjectplugin options

Synchronously converts markdown to bemjson.

const toBemjson = require('md-to-bemjson').convertSync;

console.log(JSON.stringify(toBemjson('# Hello world'), null, 4));

Yields:

{
    "block": "md-root",
    "content": {
        "block": "heading",
        "content": "Hello world",
        "level": 1,
        "mods": {
            "level": 1
        }
    }
}

static stringify(markdown [, options]) => Promise

ParameterTypeDescription
markdownstringMarkdown text
optionsObjectplugin options

Asynchronously converts and stringify markdown to bemjson module with exports.

const toBemjsonString = require('md-to-bemjson').stringify;

toBemjsonString('# Hello world').then(bjson => console.log(JSON.stringify(bjson, null, 4)));

Yields:

module.exports = {
    block: 'md-root',
    content: {
        block: 'heading',
        content: 'Hello world',
        level: 1,
        mods: {
            level: 1
        }
    }
};

static stringifySync(markdown [, options]) => String

ParameterTypeDescription
markdownstringMarkdown text
optionsObjectplugin options

Synchronously converts and stringify markdown to bemjson module with exports.

const toBemjsonString = require('md-to-bemjson').stringifySync;

console.log(toBemjsonString('# Hello world'));

Yields:

module.exports = {
    block: 'md-root',
    content: {
        block: 'heading',
        content: 'Hello world',
        level: 1,
        mods: {
            'level': 1
        }
    }
};

License

Code and documentation copyright 2017 YANDEX LLC. Code released under the Mozilla Public License 2.0.

1.0.2

5 years ago

1.0.1

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago

0.9.3

6 years ago

0.9.2

6 years ago

0.9.0

6 years ago

0.8.0

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago