From 454081a37c734d0d886377d0aa5e7c437efe419a Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 8 Sep 2019 02:54:50 +0100 Subject: [PATCH 1/9] feat: prepend root to image path --- README.md | 8 ++++++++ index.js | 4 +++- lib/renderer.js | 22 ++++++++++++++++++++-- package.json | 1 + 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8ad7a36..bb8f5b3 100644 --- a/README.md +++ b/README.md @@ -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) @@ -42,6 +43,13 @@ marked: - **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `https://hexo.io`. - **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`. - **headerIds** - Insert header id, e.g. `

text

`. Useful for inserting anchor link to each paragraph with a heading. +- **headerIds** - Insert header id, e.g. `

text

`. 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 `text` ## Extras 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..ff27968 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,23 @@ 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); + } + + return `${text}`; +}; + marked.setOptions({ langPrefix: '', highlight(code, lang) { @@ -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)); }; diff --git a/package.json b/package.json index 5f1f25b..cad0d49 100644 --- a/package.json +++ b/package.json @@ -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" From 4e175c1457c6695a796bdb484071eb1b90363b49 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 8 Sep 2019 02:55:51 +0100 Subject: [PATCH 2/9] test: should prepend root to image path --- test/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/index.js b/test/index.js index 169b9b9..beb8ac5 100644 --- a/test/index.js +++ b/test/index.js @@ -191,4 +191,43 @@ 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/' + } + }; + + it('should not modify image path with default option', () => { + const r = renderer.bind(ctx); + const result = r({text: body}); + + result.should.eql([ + '

', + 'foo

\n' + ].join('\n')); + }); + + 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([ + '

', + 'foo

\n' + ].join('\n')); + }); + }); }); From 37b62d3bdd1a83ed849361b7e6d7399d3fd73fc2 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 8 Sep 2019 03:20:18 +0100 Subject: [PATCH 3/9] chore(deps): update hexo-util from ^1.0.1 to ^1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cad0d49..9b0e688 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "license": "MIT", "dependencies": { "hexo": "^3.9.0", - "hexo-util": "^1.0.1", + "hexo-util": "^1.1.0", "marked": "^0.7.0", "strip-indent": "^3.0.0" }, From d49b437740080b8ef84a1cea94bdcec19f647d94 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 8 Sep 2019 03:53:00 +0100 Subject: [PATCH 4/9] refactor: parse only the necessary config to url_for() --- lib/renderer.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index ff27968..0340da7 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -85,13 +85,7 @@ Renderer.prototype.paragraph = text => { // 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 - } - }); + const url_for = require('hexo/lib/plugins/helper/url_for').bind(this.options); if (!parse(href).hostname && this.options.prependRoot) { href = url_for(href); @@ -112,7 +106,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)); + }, this.config.marked, options, url_forCfg)); }; From d42262ac667536252b43d4b8c883d457126f0c89 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 9 Sep 2019 01:25:34 +0100 Subject: [PATCH 5/9] fix: do not prepend when relative_link is enabled --- lib/renderer.js | 3 ++- test/index.js | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 0340da7..91536d1 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -87,7 +87,8 @@ Renderer.prototype.paragraph = text => { Renderer.prototype.image = function(href, title, text) { const url_for = require('hexo/lib/plugins/helper/url_for').bind(this.options); - if (!parse(href).hostname && this.options.prependRoot) { + if (!parse(href).hostname && !this.options.config.relative_link + && this.options.prependRoot) { href = url_for(href); } diff --git a/test/index.js b/test/index.js index beb8ac5..8b79e9c 100644 --- a/test/index.js +++ b/test/index.js @@ -205,11 +205,13 @@ describe('Marked renderer', () => { marked: { prependRoot: false }, - root: '/blog/' + root: '/blog/', + relative_link: false } }; it('should not modify image path with default option', () => { + ctx.config.relative_link = true; const r = renderer.bind(ctx); const result = r({text: body}); @@ -217,6 +219,7 @@ describe('Marked renderer', () => { '

', 'foo

\n' ].join('\n')); + ctx.config.relative_link = false; }); it('should prepend image path with root', () => { From 475ef9e3877892429d300ded2bae3b41ecfc4d0a Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 9 Sep 2019 08:13:58 +0100 Subject: [PATCH 6/9] test: reset config after each test --- test/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/index.js b/test/index.js index 8b79e9c..05633b7 100644 --- a/test/index.js +++ b/test/index.js @@ -211,6 +211,16 @@ describe('Marked renderer', () => { }; it('should not modify image path with default option', () => { + const r = renderer.bind(ctx); + const result = r({text: body}); + + result.should.eql([ + '

', + 'foo

\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}); @@ -219,6 +229,7 @@ describe('Marked renderer', () => { '

', 'foo

\n' ].join('\n')); + ctx.config.relative_link = false; }); @@ -231,6 +242,7 @@ describe('Marked renderer', () => { '

', 'foo

\n' ].join('\n')); + ctx.config.marked.prependRoot = false; }); }); }); From 9a7c7bb268db2b2e9b5bb837e1f726520c370bd8 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 15 Sep 2019 15:19:08 +0100 Subject: [PATCH 7/9] test: should encode image url --- lib/renderer.js | 2 +- package.json | 2 +- test/index.js | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 91536d1..99d53b2 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -85,7 +85,7 @@ Renderer.prototype.paragraph = text => { // Prepend root to image path Renderer.prototype.image = function(href, title, text) { - const url_for = require('hexo/lib/plugins/helper/url_for').bind(this.options); + const url_for = require('hexo-util').url_for.bind(this.options); if (!parse(href).hostname && !this.options.config.relative_link && this.options.prependRoot) { diff --git a/package.json b/package.json index 9b0e688..ef04ccf 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "license": "MIT", "dependencies": { "hexo": "^3.9.0", - "hexo-util": "^1.1.0", + "hexo-util": "^1.2.0", "marked": "^0.7.0", "strip-indent": "^3.0.0" }, diff --git a/test/index.js b/test/index.js index 05633b7..a3b99ad 100644 --- a/test/index.js +++ b/test/index.js @@ -245,4 +245,21 @@ describe('Marked renderer', () => { 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([ + '

', + '

\n' + ].join('\n')); + }); }); From e90125d4a46973c36bc9810e4935903d9c106888 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 20 Sep 2019 21:21:41 +0100 Subject: [PATCH 8/9] refactor: initialize url_for globally --- lib/renderer.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 99d53b2..c67103c 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -2,7 +2,7 @@ const marked = require('marked'); const stripIndent = require('strip-indent'); -const { stripHTML, highlight, slugize, encodeURL } = require('hexo-util'); +const { stripHTML, highlight, slugize, encodeURL, url_for } = require('hexo-util'); const MarkedRenderer = marked.Renderer; const { parse } = require('url'); @@ -85,11 +85,9 @@ Renderer.prototype.paragraph = text => { // Prepend root to image path Renderer.prototype.image = function(href, title, text) { - const url_for = require('hexo-util').url_for.bind(this.options); - if (!parse(href).hostname && !this.options.config.relative_link && this.options.prependRoot) { - href = url_for(href); + href = url_for.call(this.options, href); } return `${text}`; From 788db1b9f777c659084d332e1afc548f6eed7f6b Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 20 Sep 2019 21:39:55 +0100 Subject: [PATCH 9/9] chore(deps): update hexo-util from ^1.1.0 to ^1.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ef04ccf..2c9bba4 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "license": "MIT", "dependencies": { "hexo": "^3.9.0", - "hexo-util": "^1.2.0", + "hexo-util": "^1.3.0", "marked": "^0.7.0", "strip-indent": "^3.0.0" },