Skip to content

Commit dbc195b

Browse files
chore(deps): adapt to marked@12
1 parent 680f3ae commit dbc195b

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

lib/renderer.js

+49-32
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ let JSDOM,
66
createDOMPurify;
77

88
const { encodeURL, slugize, stripHTML, url_for, isExternalLink, escapeHTML: escape, unescapeHTML: unescape } = require('hexo-util');
9-
const MarkedRenderer = marked.Renderer;
10-
const MarkedTokenizer = marked.Tokenizer;
119
const { basename, dirname, extname, join } = require('path').posix;
1210
const rATag = /<a(?:\s+?|\s+?[^<>]+\s+?)?href=["'](?:#)([^<>"']+)["'][^<>]*>/i;
1311
const rDlSyntax = /(?:^|\s)(\S.+)<br>:\s+(\S.+)/;
@@ -16,17 +14,27 @@ const anchorId = (str, transformOption) => {
1614
return slugize(stripHTML(unescape(str)).trim(), { transform: transformOption });
1715
};
1816

19-
class Renderer extends MarkedRenderer {
20-
constructor(hexo) {
21-
super();
22-
this._headingId = {};
23-
this.hexo = hexo;
17+
function mangleEmail(text) {
18+
let out = '';
19+
let i,
20+
ch;
21+
22+
const l = text.length;
23+
for (i = 0; i < l; i++) {
24+
ch = text.charCodeAt(i);
25+
if (Math.random() > 0.5) {
26+
ch = 'x' + ch.toString(16);
27+
}
28+
out += '&#' + ch + ';';
2429
}
2530

31+
return out;
32+
}
33+
34+
const renderer = {
2635
// Add id attribute to headings
2736
heading(text, level) {
28-
const { anchorAlias, headerIds, modifyAnchors } = this.options;
29-
const { _headingId } = this;
37+
const { anchorAlias, headerIds, modifyAnchors, _headingId } = this.options;
3038

3139
if (!headerIds) {
3240
return `<h${level}>${text}</h${level}>`;
@@ -57,17 +65,25 @@ class Renderer extends MarkedRenderer {
5765

5866
// add headerlink
5967
return `<h${level} id="${id}"><a href="#${id}" class="headerlink" title="${stripHTML(text)}"></a>${text}</h${level}>`;
60-
}
68+
},
6169

6270
link(href, title, text) {
63-
const { external_link, sanitizeUrl } = this.options;
64-
const { url: urlCfg } = this.hexo.config;
71+
const { external_link, sanitizeUrl, hexo, mangle } = this.options;
72+
const { url: urlCfg } = hexo.config;
6573

6674
if (sanitizeUrl) {
6775
if (href.startsWith('javascript:') || href.startsWith('vbscript:') || href.startsWith('data:')) {
6876
href = '';
6977
}
7078
}
79+
if (mangle) {
80+
if (href.startsWith('mailto:')) {
81+
const email = href.substring(7);
82+
const mangledEmail = mangleEmail(email);
83+
84+
href = `mailto:${mangledEmail}`;
85+
}
86+
}
7187

7288
let out = '<a href="';
7389

@@ -99,7 +115,7 @@ class Renderer extends MarkedRenderer {
99115

100116
out += `>${text}</a>`;
101117
return out;
102-
}
118+
},
103119

104120
// Support Basic Description Lists
105121
paragraph(text) {
@@ -112,11 +128,12 @@ class Renderer extends MarkedRenderer {
112128
}
113129

114130
return `<p>${text}</p>\n`;
115-
}
131+
},
116132

117133
// Prepend root to image path
118134
image(href, title, text) {
119-
const { hexo, options } = this;
135+
const { options } = this;
136+
const { hexo } = options;
120137
const { relative_link } = hexo.config;
121138
const { lazyload, figcaption, prependRoot, postPath } = options;
122139

@@ -142,11 +159,7 @@ class Renderer extends MarkedRenderer {
142159
}
143160
return out;
144161
}
145-
}
146-
147-
marked.setOptions({
148-
langPrefix: ''
149-
});
162+
};
150163

151164
// https://github.com/markedjs/marked/blob/b6773fca412c339e0cedd56b63f9fa1583cfd372/src/Lexer.js#L8-L24
152165
const smartypants = (str, quotes) => {
@@ -171,15 +184,15 @@ const smartypants = (str, quotes) => {
171184
.replace(/\.{3}/g, '\u2026');
172185
};
173186

174-
class Tokenizer extends MarkedTokenizer {
187+
const tokenizer = {
175188
// Support AutoLink option
176-
url(src, mangle) {
177-
const { options } = this;
178-
const { autolink } = options;
189+
url(src) {
190+
const { autolink } = this.options;
179191

180192
if (!autolink) return;
181-
return super.url(src, mangle);
182-
}
193+
// return false to use original url tokenizer
194+
return false;
195+
},
183196

184197
// Override smartypants
185198
inlineText(src) {
@@ -202,21 +215,20 @@ class Tokenizer extends MarkedTokenizer {
202215
};
203216
}
204217
}
205-
}
218+
};
206219

207220
module.exports = function(data, options) {
208221
const { post_asset_folder, marked: markedCfg, source_dir } = this.config;
209-
const { prependRoot, postAsset, dompurify } = markedCfg;
222+
const { prependRoot, postAsset, dompurify, mangle } = markedCfg;
210223
const { path, text } = data;
211224

212225
// exec filter to extend marked
213226
this.execFilterSync('marked:use', marked.use, { context: this });
214227

215228
// exec filter to extend renderer.
216-
const renderer = new Renderer(this);
217229
this.execFilterSync('marked:renderer', renderer, { context: this });
218230

219-
const tokenizer = new Tokenizer();
231+
// exec filter to extend tokenizer
220232
this.execFilterSync('marked:tokenizer', tokenizer, { context: this });
221233

222234
const extensions = [];
@@ -250,8 +262,13 @@ module.exports = function(data, options) {
250262
}
251263
sanitizer = function(html) { return DOMPurify.sanitize(html, param); };
252264
}
253-
return sanitizer(marked(text, Object.assign({
265+
266+
marked.use({
254267
renderer,
255268
tokenizer
256-
}, markedCfg, options, { postPath })));
269+
});
270+
return sanitizer(marked.parse(text, Object.assign({
271+
// headerIds was removed in marked v8.0.0, but we still need it
272+
headerIds: true
273+
}, markedCfg, options, { postPath, hexo: this, _headingId: {} })));
257274
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"dompurify": "^3.0.3",
3535
"hexo-util": "^3.1.0",
3636
"jsdom": "^20.0.1",
37-
"marked": "^4.3.0"
37+
"marked": "^12.0.1"
3838
},
3939
"devDependencies": {
4040
"c8": "^8.0.0",

test/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const { sep } = require('path');
99
describe('Marked renderer', () => {
1010
const hexo = new Hexo(__dirname, {silent: true});
1111
const defaultCfg = JSON.parse(JSON.stringify(Object.assign(hexo.config, {
12-
marked: {}
12+
marked: {
13+
headerIds: true,
14+
mangle: true
15+
}
1316
})));
1417

1518
before(async () => {

0 commit comments

Comments
 (0)