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

Add marked:renderer filter #129

Merged
merged 7 commits into from
Dec 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
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});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume execFilter() is not used because marked is not async?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@curbengh marked support async, but in hexo-renderer-marked, no.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible for this plugin?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume execFilter() is not used because marked is not async?

I tried execFilter(), but fail. This is beyond my ability 😂 . @curbengh good job.


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": "^14.1.1"
},
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'));
});
});
});