Skip to content

Commit

Permalink
feat: marked:renderer filter (#129)
Browse files Browse the repository at this point in the history
* Add marked:renderer filter

* Fix eslint

* Add hexo instance fo test

* Del package-lock.json ....

* Update test/index.js

Co-Authored-By: Sukka <isukkaw@gmail.com>

* Update test/index.js

Co-Authored-By: Sukka <isukkaw@gmail.com>

* Fix eslint

Co-authored-by: Sukka <isukkaw@gmail.com>
  • Loading branch information
jiangtj and SukkaW committed Dec 23, 2019
1 parent ee789d3 commit 64660b1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ module.exports = function(data, options) {
}
});

// exec filter to extend renderer.
const renderer = new Renderer();
this.execFilterSync('marked:renderer', renderer, {context: this});

return marked(data.text, Object.assign({
renderer: new Renderer()
renderer
}, this.config.marked, options, siteCfg));
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"chai": "^4.2.0",
"eslint": "^6.0.1",
"eslint-config-hexo": "^4.0.0",
"hexo": "^4.2.0",
"mocha": "^6.1.4",
"nyc": "^15.0.0"
},
Expand Down
67 changes: 52 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

require('chai').should();
const { highlight, encodeURL } = require('hexo-util');
const Hexo = require('hexo');

describe('Marked renderer', () => {
const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {}
}
};
});

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

it('default', () => {
const code = 'console.log("Hello world");';
Expand Down Expand Up @@ -140,13 +142,14 @@ describe('Marked renderer', () => {
});

describe('autolink option tests', () => {
const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
autolink: true
}
}
};
});

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

Expand Down Expand Up @@ -192,13 +195,14 @@ describe('Marked renderer', () => {
});

describe('sanitizeUrl option tests', () => {
const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
sanitizeUrl: true
}
}
};
});

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

Expand Down Expand Up @@ -239,13 +243,14 @@ describe('Marked renderer', () => {

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

const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
modifyAnchors: ''
}
}
};
});

it('should not modify anchors with default options', () => {
const r = renderer.bind(ctx);
Expand Down Expand Up @@ -294,7 +299,8 @@ describe('Marked renderer', () => {

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

const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
prependRoot: false
Expand All @@ -303,7 +309,7 @@ describe('Marked renderer', () => {
root: '/blog/',
relative_link: false
}
};
});

it('should not modify image path with default option', () => {
const r = renderer.bind(ctx);
Expand Down Expand Up @@ -344,7 +350,8 @@ describe('Marked renderer', () => {
describe('external_link', () => {
const renderer = require('../lib/renderer');

const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
external_link: {
Expand All @@ -353,7 +360,7 @@ describe('Marked renderer', () => {
},
url: 'http://example.com'
}
};
});

it('disable', () => {
const body = '[foo](http://bar.com/)';
Expand Down Expand Up @@ -422,7 +429,8 @@ describe('Marked renderer', () => {
describe('nofollow', () => {
const renderer = require('../lib/renderer');

const ctx = {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
external_link: {
Expand All @@ -433,7 +441,7 @@ describe('Marked renderer', () => {
},
url: 'http://example.com'
}
};
});

const body = [
'[foo](http://foo.com/)',
Expand Down Expand Up @@ -560,4 +568,33 @@ describe('Marked renderer', () => {
'<img src="http://bar.com/b.jpg" alt="a&quot;b" title="c&gt;d"></p>\n'
].join('\n'));
});

describe('exec filter to extend', () => {
it('should execute filter registered to marked:renderer', () => {
const hexo = new Hexo(__dirname, {silent: true});
hexo.extend.filter.register('marked:renderer', renderer => {
renderer.image = function(href, title, text) {
return `<img data-src="${encodeURL(href)}">`;
};
});

const urlA = '/foo/bár.jpg';
const urlB = 'http://fóo.com/bar.jpg';

const body = [
`![](${urlA})`,
`![](${urlB})`
].join('\n');

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

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

result.should.eql([
`<p><img data-src="${encodeURL(urlA)}">`,
`<img data-src="${encodeURL(urlB)}"></p>\n`
].join('\n'));
});
});
});

0 comments on commit 64660b1

Please sign in to comment.