This repository was archived by the owner on Mar 22, 2019. It is now read-only.
forked from webpack/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderMarkdown.js
134 lines (131 loc) · 3.37 KB
/
renderMarkdown.js
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
131
132
133
134
var marked = require("marked");
var titleToLink = require("./titleToLink");
var hljs = require("./highlight.js");
function unescape(html) {
return html
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/&/g, '&');
}
function escape(html) {
return html
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
module.exports = function renderMarkdown(md, noRefs) {
md = md.replace(/\[\[([^\]]+?)\s*\|\s*([a-z0-9 \-_\.]+)\]\]/gi, function(intraLink) {
// [[text | intra link]]
var match = /\[\[([^\]]+?)\s*\|\s*([a-z0-9 \-_\.]+)\]\]/gi.exec(intraLink);
var link = titleToLink(match[2]);
return "[" + match[1] + "](" + link + ".html)";
}).replace(/\[\[([a-z0-9 \-_\.]+)\]\]/gi, function(intraLink) {
// [[intra link]]
var match = /\[\[(.+)\]\]/gi.exec(intraLink);
var link = titleToLink(match[1]);
return "[" + match[1] + "](" + link + ".html)";
});
var listOfHeadings = [];
var renderer = new marked.Renderer();
renderer.code = function(code, lang) {
var html = false;
if(lang === "html") lang = "xml";
try {
if(lang) html = hljs.highlight(lang, code).value;
else html = hljs.highlightAuto(code).value;
} catch(e) {
html = escape(code);
}
return "<pre><code>" + html + "</code></pre>";
};
renderer.codespan = function(code) {
code = unescape(code);
var html = hljs.highlight("javascript", code).value;
return "<code>" + html + "</code>";
};
renderer.heading = function(text, level, raw, options) {
if(noRefs) {
return '<h'
+ level
+ '>'
+ text
+ '</h'
+ level
+ '>\n';
} else {
var anchor = text.toLowerCase().replace(/\(.*?\)/g, "").trim().replace(/<.*?>|\&.*?;/g, "").replace(/[^\w]+/g, '-').replace(/^\-+|\-+$/g, "");
listOfHeadings.push({
anchor: anchor,
text: text.replace(/\(.*?\)/g, ""),
level: level
});
return '<h'
+ level
+ ' id="'
+ anchor
+ '"><a class="anchor" href="#'
+ anchor
+ '">→</a>'
+ text
+ '</h'
+ level
+ '>\n';
}
};
renderer.table = function(header, body) {
return '<table class="table table-bordered table-striped table-hover">\n'
+ '<thead>\n'
+ header
+ '</thead>\n'
+ '<tbody>\n'
+ body
+ '</tbody>\n'
+ '</table>\n';
};
var html = marked(md, {
renderer: renderer,
gfm: true,
tables: true,
breaks: false,
smartLists: true,
smartypants: true
});
var contents = [];
var currentLevel = listOfHeadings.map(function(h) {
return h.level;
}).reduce(function(a, b) { return Math.min(a, b); }, 1000) - 1;
listOfHeadings.forEach(function(heading) {
if(currentLevel === heading.level) {
contents.push("</li><li>");
} else if(currentLevel < heading.level - 1) {
return;
} else if(currentLevel < heading.level) {
contents.push("<ul><li>");
currentLevel++;
} else if(currentLevel > heading.level) {
while(currentLevel > heading.level) {
contents.push("</li></ul>");
currentLevel--;
}
contents.push("</li><li>");
}
contents.push("<a href=\"#",
heading.anchor,
"\">",
heading.text,
"</a>");
});
while(listOfHeadings.length > 0 && currentLevel > 0) {
contents.push("</li></ul>");
currentLevel--;
}
contents = contents.join("");
if(contents) {
return "<div class=\"contents\">" + contents + "</div>" + html;
}
return html;
}