aboutsummaryrefslogtreecommitdiff
path: root/lib/markdown-writer.js
blob: 1bfa942f4ca0a6721ae11039ae160bd4c8207388 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
function mdElement(start, end) {
    if (end === undefined)
        end = start;

    this.open = function(attributes) {
        return start;
    };

    this.close = function() {
        return end;
    };
}
function mdLink(link, text) {
    var attrs = {};

    this.open = function(attributes) {
        attrs = attributes;
        return "[";
    };

    this.close = function() {
        return (attrs[text] || "") + "](" + (attrs[link] || "") + ")";
    };
}
function mdListItem() {
    this.open = function(attributes) {
        var output = "";

        for(var i = 0; i < attributes["indent"]; ++i)
            output += "\t";

        return output + (attributes["type"] === "ul" ? "-" : attributes["count"] + ".") + " ";
    };

    this.close = function() {
        return "\n";
    };
}

// map HTML tags to Markdown syntax
var htmlmd = {
    "h1": new mdElement("# ", "\n\n"),
    "h2": new mdElement("## ", "\n\n"),
    "h3": new mdElement("### ", "\n\n"),
    "h4": new mdElement("#### ", "\n\n"),
    "h5": new mdElement("##### ", "\n\n"),
    "h6": new mdElement("###### ", "\n\n"),
    "p": new mdElement("", "\n\n"),
    "br": new mdElement("", "  \n"),
    "ul": new mdElement("\n", ""),
    "ol": new mdElement("\n", ""),
    "li": new mdListItem(),
    "strong": new mdElement("**"),
    "em": new mdElement("*"),
    "a": new mdLink("href"),
    "img": new mdLink("src", "alt"),
};

function mdWriter() {
    var fragments = [];
    var list = {
        indent: -1,
        type: "ul",
        count: 0
    };
    
    function isList(tagName) {
        return tagName == "ul" || tagName == "ol";
    }

    function open(tagName, attributes) {
        if(htmlmd[tagName]) {
            if(tagName == "li") {
                attributes = attributes || {};
                attributes["indent"] = list.indent;
                attributes["type"] = list.type;
                attributes["count"] = ++list.count;
            }

            fragments.push(htmlmd[tagName].open(attributes));

            if(isList(tagName)) {
                list.type = tagName;
                list.indent++;
                list.count = 0;
            }
        }
    }
    
    function close(tagName) {
        if(htmlmd[tagName]) {
            fragments.push(htmlmd[tagName].close());

            if(isList(tagName)) {
                list.indent--;
            }
        }
    }
    
    function selfClosing(tagName, attributes) {
        if(htmlmd[tagName]) {
            fragments.push(htmlmd[tagName].open(attributes));
            fragments.push(htmlmd[tagName].close());
        }
    }
    
    function text(value) {
        fragments.push(value);
    }
    
    function append(html) {
        fragments.push(html);
    }
    
    function asString() {
        return fragments.join("").replace(/\n{2,}/, "\n\n");
    }

    return {
        asString: asString,
        open: open,
        close: close,
        text: text,
        selfClosing: selfClosing,
        append: append
    };
}

exports.writer = mdWriter;