Skip to content

Commit

Permalink
feat: prepend root to image path
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Sep 8, 2019
1 parent bdb075b commit 454081a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ marked:
autolink: true
sanitizeUrl: false
headerIds: true
prependRoot: false
```
- **gfm** - Enables [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown)
Expand All @@ -42,6 +43,13 @@ marked:
- **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `<a href="https://hexo.io">https://hexo.io</a>`.
- **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`.
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading.
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading.
- **prependRoot** - Prepend root value to (internal) image path.
* Example `_config.yml`:
``` yml
root: /blog/
```
* `![text](/path/to/image.jpg)` becomes `<img src="/blog/path/to/image.jpg" alt="text">`

## Extras

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ hexo.config.marked = Object.assign({
modifyAnchors: '',
autolink: true,
sanitizeUrl: false,
headerIds: true
headerIds: true,
// TODO: enable prependRoot by default in v3
prependRoot: false
}, hexo.config.marked);

hexo.extend.renderer.register('md', 'html', renderer, true);
Expand Down
22 changes: 20 additions & 2 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const marked = require('marked');
const stripIndent = require('strip-indent');
const { stripHTML, highlight, slugize } = require('hexo-util');
const { stripHTML, highlight, slugize, encodeURL } = require('hexo-util');
const MarkedRenderer = marked.Renderer;
const { parse } = require('url');

function Renderer() {
MarkedRenderer.apply(this);
Expand Down Expand Up @@ -82,6 +83,23 @@ Renderer.prototype.paragraph = text => {
return `<p>${text}</p>\n`;
};

// Prepend root to image path
Renderer.prototype.image = function(href, title, text) {
const url_for = require('hexo/lib/plugins/helper/url_for').bind(this);
Object.assign(this, {
config: {
root: this.options.root,
relative_link: this.options.relative_link
}
});

if (!parse(href).hostname && this.options.prependRoot) {
href = url_for(href);
}

return `<img src="${encodeURL(href)}" alt="${text}">`;
};

marked.setOptions({
langPrefix: '',
highlight(code, lang) {
Expand All @@ -96,5 +114,5 @@ marked.setOptions({
module.exports = function(data, options) {
return marked(data.text, Object.assign({
renderer: new Renderer()
}, this.config.marked, options));
}, this.config.marked, options, this.config));
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
],
"license": "MIT",
"dependencies": {
"hexo": "^3.9.0",
"hexo-util": "^1.0.1",
"marked": "^0.7.0",
"strip-indent": "^3.0.0"
Expand Down

0 comments on commit 454081a

Please sign in to comment.