From e5ddc90a47b176aba27ebcf8caf9cd965de16513 Mon Sep 17 00:00:00 2001 From: Lucien Shaw Date: Thu, 18 Jan 2024 19:53:26 +0800 Subject: [PATCH 1/3] feat: add more asset-searching locations --- lib/renderer.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 8a206c4..2a6941e 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -119,14 +119,22 @@ class Renderer extends MarkedRenderer { const { hexo, options } = this; const { relative_link } = hexo.config; const { lazyload, figcaption, prependRoot, postPath } = options; + const searchAssetLocation = ['./', '../']; 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(join(postPath, href.replace(/\\/g, '/'))); - // asset.path is backward slash in Windows - if (asset) href = asset.path.replace(/\\/g, '/'); + for (let appendPath of searchAssetLocation){ + const destPath = join(postPath, appendPath); + // findById requires forward slash + const asset = PostAsset.findById(join(destPath, href.replace(/\\/g, '/'))); + // asset.path is backward slash in Windows + if (asset) { + href = asset.path.replace(/\\/g, '/'); + // asset is found, stop searching + break; + } + } } href = url_for.call(hexo, href); } From a32559095374115cc794a8349270b3fd8c6b7bbd Mon Sep 17 00:00:00 2001 From: Lucien Shaw Date: Thu, 18 Jan 2024 19:55:29 +0800 Subject: [PATCH 2/3] docs: add explanations related to postAsset --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd6cbcd..4c6eb67 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,10 @@ marked: ``` * `![text](/path/to/image.jpg)` becomes `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/". - * `![](image.jpg)` becomes `` + * Search for assets first in the post asset folder, then the parent folder of the post asset folder. The search process is aborted once the image is found. + * "image.jpg" is located at "/2020/01/02/foo/image.jpg" and locally `source/_post/foo/image.jpg` , which is a post asset of "/2020/01/02/foo/". + * `![](image.jpg)` becomes `` (asset is found in post asset folder since `source/_post/foo` `/` `image.jpg` exists.) + * `![](foo/image.jpg)` also becomes `` (asset is found in the parent folder of post asset folder since `source/_post` `/` `foo/image.jpg` exists.) * Requires **prependRoot** to be enabled. - **external_link** * **enable** - Open external links in a new tab. From efb29c2394a19cd283c012b6047c2003cec18704 Mon Sep 17 00:00:00 2001 From: Lucien Shaw Date: Tue, 30 Apr 2024 10:14:23 +0800 Subject: [PATCH 3/3] fix: resolve two lint problems --- lib/renderer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 2a6941e..05d5514 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -124,7 +124,7 @@ class Renderer extends MarkedRenderer { if (!/^(#|\/\/|http(s)?:)/.test(href) && !relative_link && prependRoot) { if (!href.startsWith('/') && !href.startsWith('\\') && postPath) { const PostAsset = hexo.model('PostAsset'); - for (let appendPath of searchAssetLocation){ + for (const appendPath of searchAssetLocation) { const destPath = join(postPath, appendPath); // findById requires forward slash const asset = PostAsset.findById(join(destPath, href.replace(/\\/g, '/'))); @@ -212,7 +212,7 @@ class Tokenizer extends MarkedTokenizer { } } -module.exports = function(data, options) { +module.exports = function (data, options) { const { post_asset_folder, marked: markedCfg, source_dir } = this.config; const { prependRoot, postAsset, dompurify } = markedCfg; const { path, text } = data; @@ -240,7 +240,7 @@ module.exports = function(data, options) { } } - let sanitizer = function(html) { return html; }; + let sanitizer = function (html) { return html; }; if (dompurify) { if (createDOMPurify === undefined && JSDOM === undefined) { @@ -253,7 +253,7 @@ module.exports = function(data, options) { if (dompurify !== true) { param = dompurify; } - sanitizer = function(html) { return DOMPurify.sanitize(html, param); }; + sanitizer = function (html) { return DOMPurify.sanitize(html, param); }; } return sanitizer(marked(text, Object.assign({ renderer,