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 = `';
return out;
@@ -115,29 +123,24 @@ marked.setOptions({
module.exports = function(data, options) {
hexo = this;
- const siteCfg = Object.assign({}, {
- config: {
- url: this.config.url,
- root: this.config.root,
- relative_link: this.config.relative_link,
- post_asset_folder: this.config.post_asset_folder
- }
- });
+ const { post_asset_folder, marked: markedCfg } = this.config;
+ const { prependRoot, postAsset } = markedCfg;
+ const { path, text } = data;
// exec filter to extend renderer.
const renderer = new Renderer();
this.execFilterSync('marked:renderer', renderer, {context: this});
let postId = '';
- if (data.path && this.config.post_asset_folder && this.config.marked.prependRoot && this.config.marked.postAsset) {
+ if (path && post_asset_folder && prependRoot && postAsset) {
const Post = this.model('Post');
// Windows compatibility, Post.findOne() requires forward slash
- const source = data.path.replace(this.source_dir, '').replace('\\', '/');
+ const source = path.replace(this.source_dir, '').replace('\\', '/');
const post = Post.findOne({ source });
postId = post ? post._id : '';
}
- return marked(data.text, Object.assign({
+ return marked(text, Object.assign({
renderer
- }, this.config.marked, options, siteCfg, { postId }));
+ }, markedCfg, options, { postId }));
};
diff --git a/test/index.js b/test/index.js
index 7324505..d810c5b 100644
--- a/test/index.js
+++ b/test/index.js
@@ -659,6 +659,8 @@ describe('Marked renderer', () => {
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 ``;