diff --git a/lib/renderer.js b/lib/renderer.js index cb6a232..e16894b 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -6,8 +6,6 @@ let JSDOM, createDOMPurify; const { encodeURL, slugize, stripHTML, url_for, isExternalLink, escapeHTML: escape, unescapeHTML: unescape } = require('hexo-util'); -const MarkedRenderer = marked.Renderer; -const MarkedTokenizer = marked.Tokenizer; const { basename, dirname, extname, join } = require('path').posix; const rATag = /]+\s+?)?href=["'](?:#)([^<>"']+)["'][^<>]*>/i; const rDlSyntax = /(?:^|\s)(\S.+)
:\s+(\S.+)/; @@ -16,17 +14,27 @@ const anchorId = (str, transformOption) => { return slugize(stripHTML(unescape(str)).trim(), { transform: transformOption }); }; -class Renderer extends MarkedRenderer { - constructor(hexo) { - super(); - this._headingId = {}; - this.hexo = hexo; +function mangleEmail(text) { + let out = ''; + let i, + ch; + + const l = text.length; + for (i = 0; i < l; i++) { + ch = text.charCodeAt(i); + if (Math.random() > 0.5) { + ch = 'x' + ch.toString(16); + } + out += '&#' + ch + ';'; } + return out; +} + +const renderer = { // Add id attribute to headings heading(text, level) { - const { anchorAlias, headerIds, modifyAnchors } = this.options; - const { _headingId } = this; + const { anchorAlias, headerIds, modifyAnchors, _headingId } = this.options; if (!headerIds) { return `${text}`; @@ -57,17 +65,25 @@ class Renderer extends MarkedRenderer { // add headerlink return `${text}`; - } + }, link(href, title, text) { - const { external_link, sanitizeUrl } = this.options; - const { url: urlCfg } = this.hexo.config; + const { external_link, sanitizeUrl, hexo, mangle } = this.options; + const { url: urlCfg } = hexo.config; if (sanitizeUrl) { if (href.startsWith('javascript:') || href.startsWith('vbscript:') || href.startsWith('data:')) { href = ''; } } + if (mangle) { + if (href.startsWith('mailto:')) { + const email = href.substring(7); + const mangledEmail = mangleEmail(email); + + href = `mailto:${mangledEmail}`; + } + } let out = ' { const hexo = new Hexo(__dirname, {silent: true}); const defaultCfg = JSON.parse(JSON.stringify(Object.assign(hexo.config, { - marked: {} + marked: { + mangle: true + } }))); before(async () => {