Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add more asset-searching locations and solve a known issue #271

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ marked:
```
* `![text](/path/to/image.jpg)` 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/".
* `![](image.jpg)` becomes `<img src="/2020/01/02/foo/image.jpg">`
* 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 `<img src="/2020/01/02/foo/image.jpg">` (asset is found in post asset folder since `source/_post/foo` `/` `image.jpg` exists.)
* `![](foo/image.jpg)` also becomes `<img src="/2020/01/02/foo/image.jpg">` (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.
Expand Down
22 changes: 15 additions & 7 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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({
Expand Down