From d37fa4e38aa2ff67bbec925032730e5a08cc8479 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 8 Sep 2019 01:58:12 +0100 Subject: [PATCH] feat: prepend root to image path --- index.js | 4 +++- lib/renderer.js | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index aa1605c..6c75006 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/lib/renderer.js b/lib/renderer.js index 61f9fb3..e630376 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -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); @@ -82,6 +83,24 @@ Renderer.prototype.paragraph = text => { return `

${text}

\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 `${title}`; +}; + marked.setOptions({ langPrefix: '', highlight(code, lang) { @@ -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)); };