Skip to content

bem-contrib/md-to-bemjson

Repository files navigation

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

Parameter Type Description
github Object, boolean Enables github support with remark plugin remark-github. Default false.
exportType enum remark-bemjson option. Exports to certain type with .stringify. Supported exports.
exportName string remark-bemjson option. Used with exportType=(modules, umd, YModules) stringify bemjson with exported given name.
augment Function, Object Options for augmentation resulting bemjson by every node. As function accepts bemNode and must return it.
plugins Array Options for additional plugins to be included. Plugin format: { plugin: Function, options: Object }

Options.augment

Parameter Type Description
prefix string Add prefix to all blocks. Important: for root replace original prefix.
scope string Replace root block with scope. And replace all blocks with elems.
map Object Replace block names with provided in map. Available blocks.
html Object Options 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

Parameter Type Description
markdown string Markdown 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

Parameter Type Description
markdown string Markdown 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

Parameter Type Description
markdown string Markdown text
options Object Options 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

Parameter Type Description
markdown string Markdown text
options Object Options 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

Parameter Type Description
markdown string Markdown text
options Object plugin 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

Parameter Type Description
markdown string Markdown text
options Object plugin 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

Parameter Type Description
markdown string Markdown text
options Object plugin 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

Parameter Type Description
markdown string Markdown text
options Object plugin 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.