summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/src/toHTML.js
blob: e887e11696d80667dbc653a91e28c59445c4f67c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const { State } = require('markup-it');
const markdown = require('markup-it/lib/markdown');
const html = require('markup-it/lib/html');

/**
 * Convert Markdown block to HTML
 *
 * @param {String} src (markdown)
 * @return {String} (html)
 */
function convertMdToHTMLBlock(src) {
    const fromMD = State.create(markdown);
    const document = fromMD.deserializeToDocument(src);

    const toHTML = State.create(html);
    return toHTML.serializeDocument(document);
}

/**
 * Convert Markdown inline to HTML
 *
 * @param {String} src (markdown)
 * @return {String} (html)
 */
function convertMdToHTMLInline(src) {
    const content  = markdown.toInlineContent(src);
    const textHtml = html.toInlineText(content);

    return textHtml;
}

module.exports = {
    block: convertMdToHTMLBlock,
    inline: convertMdToHTMLInline
};