From ad129c7e9603fabd75fac449a7ce7dce54c7898a Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Fri, 14 Aug 2020 05:14:45 +0000 Subject: [PATCH] style: destructure --- lib/renderer.js | 55 ++++++++++++++++++++++++++----------------------- test/index.js | 2 ++ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 0ffe80a..51f5bbf 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -19,12 +19,16 @@ class Renderer extends MarkedRenderer { // Add id attribute to headings heading(text, level) { - if (!this.options.headerIds) { + const { headerIds, modifyAnchors } = this.options; + const { _headingId } = this; + + if (!headerIds) { return `${text}`; } - const transformOption = this.options.modifyAnchors; + + const transformOption = modifyAnchors; let id = anchorId(stripHTML(text), transformOption); - const headingId = this._headingId; + const headingId = _headingId; // Add a number after id if repeated if (headingId[id]) { @@ -39,14 +43,15 @@ class Renderer extends MarkedRenderer { // Support AutoLink option link(href, title, text) { - const { options } = this; - const { external_link } = options; - if (options.sanitizeUrl) { + const { autolink, external_link, sanitizeUrl } = this.options; + const { url: urlCfg } = hexo.config; + + if (sanitizeUrl) { if (href.startsWith('javascript:') || href.startsWith('vbscript:') || href.startsWith('data:')) { href = ''; } } - if (!options.autolink && href === text && title == null) { + if (!autolink && href === text && title == null) { return href; } @@ -58,7 +63,7 @@ class Renderer extends MarkedRenderer { const target = ' target="_blank"'; const noopener = ' rel="noopener"'; const nofollowTag = ' rel="noopener external nofollow noreferrer"'; - if (isExternalLink(href, options.config.url, external_link.exclude)) { + if (isExternalLink(href, urlCfg, external_link.exclude)) { if (external_link.enable && external_link.nofollow) { out += target + nofollowTag; } else if (external_link.enable) { @@ -85,23 +90,26 @@ class Renderer extends MarkedRenderer { // Prepend root to image path image(href, title, text) { + const { relative_link } = hexo.config; const { options } = this; + const { lazyload, prependRoot, postId } = options; + const { hostname } = parse(href); - if (!parse(href).hostname && !options.config.relative_link - && options.prependRoot) { - if (options.postId) { + if (!hostname && !relative_link + && prependRoot) { + if (postId) { const PostAsset = hexo.model('PostAsset'); - const asset = PostAsset.findOne({ post: options.postId, slug: href }); + const asset = PostAsset.findOne({ post: postId, slug: href }); if (asset) href = encodeURL(resolve('/', asset.path)); } - href = url_for.call(options, href); + href = url_for.call(hexo, href); } let out = `${text} { describe('exec filter to extend', () => { it('should execute filter registered to marked:renderer', () => { const hexo = new Hexo(__dirname, {silent: true}); + hexo.config.marked = {}; + hexo.extend.filter.register('marked:renderer', renderer => { renderer.image = function(href, title, text) { return ``;