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 d37fa4e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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
23 changes: 21 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,24 @@ 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);
}

if (!title) title = '';
return `<img src="${encodeURL(href)}" alt="${title}">`;
};

marked.setOptions({
langPrefix: '',
highlight(code, lang) {
Expand All @@ -96,5 +115,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));
};

0 comments on commit d37fa4e

Please sign in to comment.