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

feat: prepend root to image path #111

Merged
merged 9 commits into from
Sep 23, 2019
Merged
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
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, url_for } = require('hexo-util');
const MarkedRenderer = marked.Renderer;
const { parse } = require('url');

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

// Prepend root to image path
Renderer.prototype.image = function(href, title, text) {
if (!parse(href).hostname && !this.options.config.relative_link
&& this.options.prependRoot) {
href = url_for.call(this.options, href);
}

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

marked.setOptions({
langPrefix: '',
highlight(code, lang) {
Expand All @@ -94,7 +105,14 @@ marked.setOptions({
});

module.exports = function(data, options) {
const url_forCfg = Object.assign({}, {
config: {
root: this.config.root,
relative_link: this.config.relative_link
}
});

return marked(data.text, Object.assign({
renderer: new Renderer()
}, this.config.marked, options));
}, this.config.marked, options, url_forCfg));
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"license": "MIT",
"dependencies": {
"hexo-util": "^1.0.1",
"hexo": "^3.9.0",
"hexo-util": "^1.3.0",
"marked": "^0.7.0",
"strip-indent": "^3.0.0"
},
Expand Down
71 changes: 71 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,75 @@ describe('Marked renderer', () => {
].join('\n'));
});
});

describe('prependRoot option tests', () => {
const body = [
'![](/bar/baz.jpg)',
'![foo](/aaa/bbb.jpg)'
].join('\n');

const renderer = require('../lib/renderer');

const ctx = {
config: {
marked: {
prependRoot: false
},
root: '/blog/',
relative_link: false
}
};

it('should not modify image path with default option', () => {
const r = renderer.bind(ctx);
const result = r({text: body});

result.should.eql([
'<p><img src="/bar/baz.jpg" alt="">',
'<img src="/aaa/bbb.jpg" alt="foo"></p>\n'
].join('\n'));
});

it('should not modify image path when enable relative_link', () => {
ctx.config.relative_link = true;
const r = renderer.bind(ctx);
const result = r({text: body});

result.should.eql([
'<p><img src="/bar/baz.jpg" alt="">',
'<img src="/aaa/bbb.jpg" alt="foo"></p>\n'
].join('\n'));

ctx.config.relative_link = false;
});

it('should prepend image path with root', () => {
ctx.config.marked.prependRoot = true;
const r = renderer.bind(ctx);
const result = r({text: body});

result.should.eql([
'<p><img src="/blog/bar/baz.jpg" alt="">',
'<img src="/blog/aaa/bbb.jpg" alt="foo"></p>\n'
].join('\n'));
ctx.config.marked.prependRoot = false;
});
});

it('should encode image url', () => {
const body = [
'![](/foo/bár.jpg)',
'![](http://fóo.com/bar.jpg)'
].join('\n');

const renderer = require('../lib/renderer');
const r = renderer.bind(ctx);

const result = r({text: body});

result.should.eql([
'<p><img src="/foo/b%C3%A1r.jpg" alt="">',
'<img src="http://xn--fo-5ja.com/bar.jpg" alt=""></p>\n'
].join('\n'));
});
});