Skip to content

Commit

Permalink
[feat]: add marked extensions support (#227)
Browse files Browse the repository at this point in the history
* feat: add marked:extensions support

* test: fix typo

* test(extensions): add marked:extensions test

* docs(extensions): add extensions example
  • Loading branch information
Eumeryx committed Oct 5, 2022
1 parent 4b5b702 commit 12cd797
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,39 @@ hexo.extend.filter.register('marked:tokenizer', function(tokenizer) {
});
```

#### Extensions

It is also possible to customize the [extensions](https://marked.js.org/using_pro#extensions).
For example, use [KaTeX](https://katex.org/) to render block-level math:

```js
const katex = require('katex');

hexo.extend.filter.register('marked:extensions', function(extensions) {
// Info: `extensions` is an array.
extensions.push({
name: 'blockMath',
level: 'block',
tokenizer(src) {
const cap = /^\s{0,3}\$\$((?:[^\n]|\n[^\n])+?)\n{0,1}\$\$/.exec(src);

if (cap !== null) {
return {
type: 'blockMath',
raw: cap[0],
math: cap[1]
};
}

return undefined;
},
renderer(token) {
return `<p>${katex.renderToString(token.math, {displayMode: true})}</p>\n`;
}
});
});
```

[Markdown]: https://daringfireball.net/projects/markdown/
[marked]: https://github.com/chjj/marked
[PHP Markdown Extra]: https://michelf.ca/projects/php-markdown/extra/#def-list
4 changes: 4 additions & 0 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ module.exports = function(data, options) {
const tokenizer = new Tokenizer();
this.execFilterSync('marked:tokenizer', tokenizer, {context: this});

const extensions = [];
this.execFilterSync('marked:extensions', extensions, {context: this});
marked.use({extensions});

let postPath = '';
if (path && post_asset_folder && prependRoot && postAsset) {
const Post = this.model('Post');
Expand Down
34 changes: 33 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ describe('Marked renderer', () => {
].join('\n'));
});

it('should execute filter registered to marked:renderer', () => {
it('should execute filter registered to marked:tokenizer', () => {
const smartypants = str => {
return str.replace(/---/g, '\u2014');
};
Expand All @@ -921,6 +921,38 @@ describe('Marked renderer', () => {
const result = r({text: body});
result.should.eql(`<p>${smartypants(body)}</p>\n`);
});

it('should execute filter registered to marked:extensions', () => {
hexo.extend.filter.register('marked:extensions', extensions => {
extensions.push({
name: 'blockMath',
level: 'block',
tokenizer(src) {
const cap = /^\s{0,3}\$\$((?:[^\n]|\n[^\n])+?)\n{0,1}\$\$/.exec(src);

if (cap !== null) {
return {
type: 'blockMath',
raw: cap[0],
math: cap[1]
};
}

return undefined;
},
renderer(token) {
return `<p class="math block">${escapeHTML(token.math)}</p>\n`;
}
});
});

const body = '$$E=mc^2$$';

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

const result = r({text: body});
result.should.eql(`<p class="math block">${escapeHTML('E=mc^2')}</p>\n`);
});
});

describe('nunjucks', () => {
Expand Down

0 comments on commit 12cd797

Please sign in to comment.