-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkedEx.js
More file actions
110 lines (92 loc) · 3.38 KB
/
markedEx.js
File metadata and controls
110 lines (92 loc) · 3.38 KB
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
(function() {
var mdRenderer = new marked.Renderer();
// --- Copied from marked.js ---
mdRenderer.escape = function(html, encode) {
if (encode) {
if (this.escapeTest.test(html))
return html.replace(this.escapeReplace, function (ch) { return mdRenderer.escapeReplacements[ch]; });
} else {
if (this.escapeTestNoEncode.test(html))
return html.replace(this.escapeReplaceNoEncode, function (ch) { return mdRenderer.escapeReplacements[ch]; });
}
return html;
}
mdRenderer.escapeTest = /[&<>"']/;
mdRenderer.escapeReplace = /[&<>"']/g;
mdRenderer.escapeReplacements = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
mdRenderer.escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
mdRenderer.escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
mdRenderer.unescape = function(html) {
// explicitly match decimal, hex, and named HTML entities
return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, function(_, n) {
n = n.toLowerCase();
if (n === 'colon') return ':';
if (n.charAt(0) === '#') {
return n.charAt(1) === 'x'
? String.fromCharCode(parseInt(n.substring(2), 16))
: String.fromCharCode(+n.substring(1));
}
return '';
});
}
// --- Modifyed from marked.js ---
// Renderer-Heading: Add permalink class.
mdRenderer.heading = function(text, level, raw, slugger) {
if(this.options.headerIds) {
var id = this.options.headerPrefix + slugger.slug(raw);
if(this.options._headingsEx)
return `<h${level} id="${id}" class="heading">${text}<a class="permalink" href="#${id}"></a></h${level}>\n`;
return `<h${level} id="${id}">${text}</h${level}>\n`;
}
// ignore IDs
return `<h${level}>${text}</h${level}>\n`;
};
// Renderer-Code: Add extension function support.
mdRenderer.code = function(code, infostring, escaped) {
var lang = (infostring || ''); // .match(/\S*/)[0];
if(this.options._extensions) {
var oParams = lang.match(/^\{(\w+) *(.*)\}$/);
if(oParams)
return this._extension(escaped ? this.unescape(code) : code, oParams);
}
if(this.options.highlight) {
var out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
code = out;
}
}
if(!lang)
return `<pre><code>${escaped ? code : this.escape(code, true)}</code></pre>\n`;
return `<pre><code class="${this.options.langPrefix + this.escape(lang, true)}">${escaped ? code : this.escape(code, true)}</code></pre>\n`;
};
mdRenderer._extension = function(sContent, oParams) {
if(!(oParams[1] in this._extensions))
return `<p><em>ERROR: Unknown extension function '${oParams[1]}'</em></p>`;
try {
return this._extensions[oParams[1]](sContent, oParams[2]);
} catch(e) {
console.error(e);
return `<p><em>ERROR: Extension function '${oParams[1]}' - check console</em></p>`;
}
}
// --- Extensions ---
mdRenderer._extensions = {
// {collapse [Label = "Click to Expand"]}
collapse: function(sContent, sParam) {
return `
<fieldset class="collapse collapse-hidden">
<legend>${sParam || "Click to Expand"}</legend>
${marked(sContent)}
</fieldset>
`;
}
};
marked.setOptions({renderer: mdRenderer});
})();