diff --git a/README.md b/README.md index 4a1b6d4..a19f98e 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. diff --git a/lib/renderer.js b/lib/renderer.js index e16894b..c37967c 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -136,14 +136,22 @@ const renderer = { const { hexo } = options; 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 (const 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); } @@ -217,7 +225,7 @@ const tokenizer = { } }; -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; @@ -247,7 +255,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) { @@ -260,7 +268,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); }; } marked.use({