Skip to content

Commit

Permalink
style: destructure
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Aug 14, 2020
1 parent 41577e0 commit ad129c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
55 changes: 29 additions & 26 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<h${level}>${text}</h${level}>`;
}
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]) {
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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 = `<img src="${encodeURL(href)}"`;
if (text) out += ` alt="${text}"`;
if (title) out += ` title="${title}"`;
if (options.lazyload) out += ' loading="lazy"';
if (lazyload) out += ' loading="lazy"';

out += '>';
return out;
Expand All @@ -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 }));
};
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<img data-src="${encodeURL(href)}">`;
Expand Down

0 comments on commit ad129c7

Please sign in to comment.