hexo-renderer-marked
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -17,2 +17,3 @@ /* global hexo */ | ||
headerIds: true, | ||
lazyload: false, | ||
// TODO: enable prependRoot by default in v3 | ||
@@ -19,0 +20,0 @@ prependRoot: false, |
@@ -6,3 +6,2 @@ 'use strict'; | ||
const MarkedRenderer = marked.Renderer; | ||
const { parse } = require('url'); | ||
@@ -14,5 +13,6 @@ const anchorId = (str, transformOption) => { | ||
class Renderer extends MarkedRenderer { | ||
constructor() { | ||
constructor(hexo) { | ||
super(); | ||
this._headingId = {}; | ||
this.hexo = hexo; | ||
} | ||
@@ -22,8 +22,12 @@ | ||
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; | ||
@@ -43,5 +47,6 @@ // Add a number after id if repeated | ||
link(href, title, text) { | ||
const { options } = this; | ||
const { external_link } = options; | ||
if (options.sanitizeUrl) { | ||
const { autolink, external_link, sanitizeUrl } = this.options; | ||
const { url: urlCfg } = this.hexo.config; | ||
if (sanitizeUrl) { | ||
if (href.startsWith('javascript:') || href.startsWith('vbscript:') || href.startsWith('data:')) { | ||
@@ -51,3 +56,3 @@ href = ''; | ||
} | ||
if (!options.autolink && href === text && title == null) { | ||
if (!autolink && href === text && title == null) { | ||
return href; | ||
@@ -64,3 +69,3 @@ } | ||
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) { | ||
@@ -92,7 +97,15 @@ out += target + nofollowTag; | ||
image(href, title, text) { | ||
const { options } = this; | ||
const { hexo, options } = this; | ||
const { relative_link } = hexo.config; | ||
const { lazyload, prependRoot, postPath } = options; | ||
if (!parse(href).hostname && !options.config.relative_link | ||
&& options.prependRoot) { | ||
href = url_for.call(options, href); | ||
if (!/^(#|\/\/|http(s)?:)/.test(href) && !relative_link && prependRoot) { | ||
if (!href.startsWith('/') && !href.startsWith('\\') && postPath) { | ||
const PostAsset = hexo.model('PostAsset'); | ||
// findById requires forward slash | ||
const asset = PostAsset.findById(postPath + href.replace(/\\/g, '/')); | ||
// asset.path is backward slash in Windows | ||
if (asset) href = asset.path.replace(/\\/g, '/'); | ||
} | ||
href = url_for.call(hexo, href); | ||
} | ||
@@ -103,2 +116,3 @@ | ||
if (title) out += ` title="${title}"`; | ||
if (lazyload) out += ' loading="lazy"'; | ||
@@ -115,17 +129,22 @@ out += '>'; | ||
module.exports = function(data, options) { | ||
const siteCfg = Object.assign({}, { | ||
config: { | ||
url: this.config.url, | ||
root: this.config.root, | ||
relative_link: this.config.relative_link | ||
} | ||
}); | ||
const { post_asset_folder, marked: markedCfg, source_dir } = this.config; | ||
const { prependRoot, postAsset } = markedCfg; | ||
const { path, text } = data; | ||
// exec filter to extend renderer. | ||
const renderer = new Renderer(); | ||
const renderer = new Renderer(this); | ||
this.execFilterSync('marked:renderer', renderer, {context: this}); | ||
return marked(data.text, Object.assign({ | ||
let postPath = ''; | ||
if (path && post_asset_folder && prependRoot && postAsset) { | ||
const Post = this.model('Post'); | ||
// Windows compatibility, Post.findOne() requires forward slash | ||
const source = path.substring(this.source_dir.length).replace(/\\/g, '/'); | ||
const post = Post.findOne({ source }); | ||
postPath = post ? source_dir + '/_posts/' + post.slug + '/' : ''; | ||
} | ||
return marked(text, Object.assign({ | ||
renderer | ||
}, this.config.marked, options, siteCfg)); | ||
}, markedCfg, options, { postPath })); | ||
}; |
{ | ||
"name": "hexo-renderer-marked", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Markdown renderer plugin for Hexo", | ||
@@ -38,3 +38,3 @@ "main": "index", | ||
"eslint-config-hexo": "^4.1.0", | ||
"hexo": "^4.2.0", | ||
"hexo": "^5.0.0", | ||
"mocha": "^8.0.1", | ||
@@ -41,0 +41,0 @@ "nyc": "^15.0.0" |
@@ -34,3 +34,5 @@ # hexo-renderer-marked | ||
headerIds: true | ||
lazyload: false | ||
prependRoot: false | ||
postAsset: false | ||
external_link: | ||
@@ -51,2 +53,3 @@ enable: false | ||
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading. | ||
- **lazyload** - Lazy loading images via `loading="lazy"` attribute. | ||
- **prependRoot** - Prepend root value to (internal) image path. | ||
@@ -58,2 +61,6 @@ * Example `_config.yml`: | ||
* `` becomes `<img src="/blog/path/to/image.jpg" alt="text">` | ||
- **postAsset** - Resolve post asset's image path to relative path and prepend root value when [`post_asset_folder`](https://hexo.io/docs/asset-folders) is enabled. | ||
* "image.jpg" is located at "/2020/01/02/foo/image.jpg", which is a post asset of "/2020/01/02/foo/". | ||
* `` becomes `<img src="/2020/01/02/foo/image.jpg">` | ||
* Requires `prependRoot:` to be enabled. | ||
- **external_link** | ||
@@ -60,0 +67,0 @@ * **enable** - Open external links in a new tab. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13096
148
141
0