summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/src/index.js
blob: 9d560f13018d978cdd11e043fb2912d63385ae68 (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
48
49
50
const ToText = require('./totext');

const htmlParser = {
    summary: require('./summary'),
    glossary: require('./glossary'),
    langs: require('./langs'),
    readme: require('./readme'),
    page: require('./page')
};

// Compose a function with a transform function for the first argument only
function compose(toHTML, fn) {
    return (...args) => {
        args[0] = toHTML(args[0]);
        return fn(...args);
    };
}

/**
 * Create a GitBook parser from an HTML converter.
 * @param  {Object} toHTML
 *         {Function} [toHTML.inline]
 *         {Function} [toHTML.block]
 * @param  {Object} toText
 * @return {[type]}        [description]
 */
function createParser(toHTML, toText = {}) {
    const parser = {
        summary: compose(toHTML.block, htmlParser.summary),
        glossary: compose(toHTML.block, htmlParser.glossary),
        langs: compose(toHTML.block, htmlParser.langs),
        readme: compose(toHTML.block, htmlParser.readme),
        page: compose(toHTML.block, htmlParser.page),
        inline: compose(toHTML.inline, htmlParser.page)
    };

    const _toText = new ToText(toText);

    parser.summary.toText  = summary => _toText.summary(summary);
    parser.langs.toText    = langs => _toText.langs(langs);
    parser.glossary.toText = glossary => _toText.glossary(glossary);

    return parser;
}

module.exports = createParser({
    block:  html => html,
    inline: html => html
});
module.exports.createParser = createParser;