/
renderer.js
85 lines (72 loc) · 2.2 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const MarkdownIt = require('markdown-it');
const path = require('path');
class Renderer {
/**
* constructor
*
* @param {*} hexo context of hexo
*/
constructor(hexo) {
this.hexo = hexo;
let { markdown } = hexo.config;
// Temporary backward compatibility
if (typeof markdown === 'string') {
markdown = {
preset: markdown,
};
hexo.log.warn(
`Deprecated config detected. Please use\n\nmarkdown:\n preset: ${markdown.preset}\n\nSee https://github.com/hexojs/hexo-renderer-markdown-it#options`
);
}
const { preset, render, enable_rules, disable_rules, plugins, anchors, images } = markdown;
this.parser = new MarkdownIt(preset, render);
if (enable_rules) {
this.parser.enable(enable_rules);
}
if (disable_rules) {
this.parser.disable(disable_rules);
}
if (plugins) {
this.parser = plugins.reduce((parser, pugs) => {
if (pugs instanceof Object && pugs.name) {
const resolved = require.resolve(pugs.name, {
paths: [
// find from root hexo base directory node_modules
path.join(hexo.base_dir, 'node_modules'),
// find from current installed library node_modules
path.join(__dirname, '../node_modules'),
// find from root hexo base directory
hexo.base_dir,
// find from current library directory
path.join(__dirname, '../'),
],
});
return parser.use(require(resolved), pugs.options);
}
return parser.use(require(pugs));
}, this.parser);
}
if (anchors) {
this.parser.use(require('./anchors'), anchors);
}
if (images) {
this.parser.use(require('./images'), {
images,
hexo: this.hexo,
});
}
}
render(data, options) {
this.hexo.execFilterSync('markdown-it:renderer', this.parser, { context: this });
if (options != null && options.inline === true) {
return this.parser.renderInline(data.text, {
postPath: data.path
});
}
return this.parser.render(data.text, {
postPath: data.path,
});
}
}
module.exports = Renderer;