summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/src/toMarkdown.js
blob: 5b8deee63e10a82e131afa379f56b234f0604011 (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
51
52
53
54
55
56
const escape = require('markdown-escape');

// Return N time a string
function ns(s, n) {
    return Array(n + 1).join(s);
}

/*
 * This module provides markup rules for gitbook-html
 * These rules are being used to generate SUMMARY/GLOSSARY/LANGS
 */
module.exports = {
    onText(text) {
        return escape(text);
    },

    onTitleStart(level) {
        return ns('#', level) + ' ';
    },
    onTitleEnd(level) {
        return this.onBL();
    },

    onParagraphStart() {
        return this.onSection();
    },
    onParagraphEnd() {
        return this.onSection();
    },

    onLinkStart() {
        return '[';
    },
    onLinkEnd(href) {
        return '](' + href + ')';
    },

    onListStart(level) {
        return '';
    },
    onListEnd() {
        return '';
    },

    onListItemStart(level) {
        return ns(' ', level * 4) + '* ';
    },
    onListItemEnd() {
        return '';
    },

    onHR() {
        return '-----';
    }
};