-
Notifications
You must be signed in to change notification settings - Fork 305
/
markdown.js
195 lines (164 loc) · 4.99 KB
/
markdown.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
// Define some pseudo module globals
var isPro = require('../libs/debug').isPro;
var isDev = require('../libs/debug').isDev;
var isDbg = require('../libs/debug').isDbg;
//
var marked = require('marked');
var hljs = require('highlight.js');
var sanitizeHtml = require('sanitize-html');
var jsdom = require("jsdom");
var { JSDOM } = jsdom;
var htmlWhitelistPost = require('./htmlWhitelistPost.json');
var renderer = new marked.Renderer();
var blockRenderers = [
'blockquote',
'html',
'list',
'paragraph',
'table'
];
// Transform exact Github Flavored Markdown generated style tags to bootstrap custom classes
// to allow the sanitizer to whitelist on th and td tags for table alignment
function gfmStyleToBootstrapClass(aTagName, aAttribs) {
if (aAttribs.style) {
switch (aAttribs.style) {
case 'text-align:center':
return {
tagName: aTagName,
attribs: { class: 'text-center' }
};
case 'text-align:left':
return {
tagName: aTagName,
attribs: { class: 'text-left' }
};
case 'text-align:right':
return {
tagName: aTagName,
attribs: { class: 'text-right' }
};
}
}
return {
tagName: aTagName,
attribs: aAttribs
};
}
htmlWhitelistPost.transformTags = {
'th' : gfmStyleToBootstrapClass,
'td' : gfmStyleToBootstrapClass
};
function sanitize(aHtml) {
return sanitizeHtml(aHtml, htmlWhitelistPost);
}
// Sanitize the output from the block level renderers
blockRenderers.forEach(function (aType) {
renderer[aType] = function () {
// Sanitize first to close any tags
var sanitized = sanitize(marked.Renderer.prototype[aType].apply(renderer, arguments));
// Autolink most usernames
var dom = new JSDOM('<div id="sandbox"></div>');
var win = dom.window;
var doc = win.document;
var hookNode = doc.querySelector('#sandbox');
var xpr = null;
var i = null;
var textNode = null;
var textChunk = null;
var htmlContainer = null;
var thisNode = null;
hookNode.innerHTML = sanitized;
xpr = doc.evaluate(
'.//text()',
hookNode,
null,
win.XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
if (xpr) {
for (i = 0; textNode = xpr.snapshotItem(i++);) {
switch(textNode.parentNode.tagName) {
case 'PRE':
case 'CODE':
case 'A':
break;
default:
// replace all instance of @whatever with autolinked
textChunk = textNode.textContent.replace(/(^|\s)@([^\s\\\/:*?\'\"<>|#;@=&,]+)/gm,
'$1<a href="/users/$2">@$2</a>');
// Import to virtual DOM element
htmlContainer = doc.createElement('span');
htmlContainer.classList.add('autolink');
htmlContainer.innerHTML = textChunk;
// Clone everything to remove span element
for (thisNode = htmlContainer.firstChild; thisNode; thisNode = thisNode.nextSibling) {
textNode.parentNode.insertBefore(thisNode.cloneNode(true), textNode);
}
textNode.parentNode.removeChild(textNode);
}
}
sanitized = hookNode.innerHTML
}
return sanitized;
};
});
// Automatically generate an anchor for each header
renderer.heading = function (aText, aLevel) {
var escapedText = aText.toLowerCase().replace(/<\/?[^>]+?>/g, '')
.replace(/[^\w]+/g, '-');
var id = escapedText;
var html = '<h' + aLevel + '>';
html += '<a id="' + id + '" rel="bookmark"></a>';
html += sanitize(aText);
html += '<a href="#' + id + '" class="anchor">';
html += '<i class="fa fa-link"></i>';
html += '</a>';
html += '</h' + aLevel + '>';
return html;
};
renderer.link = function (aHref, aTitle, aText) {
return marked.Renderer.prototype.link.call(renderer, aHref, aTitle, aText);
};
// Set the options to use for rendering markdown
// Keep in sync with ./views/includes/scripts/markdownEditor.html
marked.setOptions({
highlight: function (aCode, aLang) {
var obj = null;
if (aLang && hljs.getLanguage(aLang)) {
try {
return hljs.highlight(aLang, aCode).value;
} catch (aErr) {
}
}
try {
obj = hljs.highlightAuto(aCode);
switch (obj.language) {
// Transform list of auto-detected language highlights
case 'dust':
case '1c':
case 'qml':
// Narrow auto-detection to something that is more likely
return hljs.highlightAuto(aCode, ['css', 'html', 'js', 'json']).value;
break;
// Any other detected go ahead and return
default:
return obj.value;
}
} catch (aErr) {
}
// If any external package failure don't block return e.g. prevent empty
return aCode;
},
renderer: renderer,
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: false, // we use sanitize-html to sanitize HTML
smartLists: true,
smartypants: false
});
exports.renderMd = function (aText) {
return marked(aText);
};