summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/src/toHTML.js
blob: e115b71fb13298a3be5c95c0559d2170a8733c62 (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
36
37
38
39
40
41
42
43
44
45
46
47
const { State, Block, BLOCKS } = require('markup-it');
const { Document } = require('slate');
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 fromMD = State.create(markdown);
    const document = fromMD.deserializeToDocument(src);

    // Create a document with a single unstyled node
    const newDocument = Document.create({
        nodes: [
            Block.create({
                type: BLOCKS.TEXT,
                nodes: document.nodes.get(0).nodes
            })
        ]
    });

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

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