-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
104 lines (97 loc) · 3.12 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"use strict";
const merge = require("lodash.merge");
const union = require("lodash.union");
const functionize = (object = {}) => {
for (let [k, v] of Object.entries(object || {})) {
if (typeof v === "string" && v.startsWith("function (") && v.endsWith("}")) {
object[k] = eval("(" + v + ")");
} else if (typeof v === "object") {
object[k] = functionize(v);
}
}
return object;
};
const def_pugs_list = [
"markdown-it-abbr",
"markdown-it-anchor",
"markdown-it-attrs",
"markdown-it-checkbox",
"markdown-it-deflist",
"markdown-it-emoji",
"markdown-it-footnote",
"markdown-it-ins",
"markdown-it-mark",
"markdown-it-sub",
"markdown-it-sup",
"markdown-it-texmath",
];
const def_pugs_conf = {
"markdown-it-anchor": {
level: 1,
permalink: undefined,
slugify: (s) => encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, "-")),
tabIndex: -1,
uniqueSlugStartIndex: 1,
},
"markdown-it-attrs": {
leftDelimiter: "{",
rightDelimiter: "}",
allowedAttributes: [],
},
"markdown-it-checkbox": {
divWrap: false,
divClass: "checkbox",
idPrefix: "checkbox",
},
"markdown-it-texmath": {
katexCssSrc: "//cdn.jsdelivr.net/npm/katex/dist/katex.min.css",
texmathCssSrc: "//cdn.jsdelivr.net/npm/markdown-it-texmath/css/texmath.min.css",
texmathDelimiters: "dollars",
throwOnError: false,
errorColor: "#cc0000",
macros: { "\\RR": "\\mathbb{R}" },
},
};
const def_main_conf = {
html: true,
xhtmlOut: false,
breaks: true,
langPrefix: "language-",
linkify: true,
typographer: true,
quotes: "“”‘’",
};
const renderer = function (data, options) {
let config = merge({}, this.config.markdown);
let main_conf = merge({}, def_main_conf, config.render);
let pugs_conf = merge({}, def_pugs_conf, config.plugins);
let pugs_list = union(def_pugs_list, Object.keys(pugs_conf));
let parser = require("markdown-it")(functionize(main_conf));
parser = pugs_list.reduce((parser, pugs) => {
let pugs_opt = merge({}, pugs_conf[pugs]);
if (pugs_opt.disabled) return parser;
pugs_opt = functionize(pugs_opt);
if (pugs === "markdown-it-texmath" && data.path) {
let { katexCssSrc, texmathCssSrc, texmathDelimiters, ...katexOptions } = pugs_opt;
data.text += `
\n<link rel="stylesheet" href="${katexCssSrc}">
\n<link rel="stylesheet" href="${texmathCssSrc}">
`;
pugs_opt = {
engine: require("katex"),
delimiters: texmathDelimiters,
katexOptions,
};
}
if (pugs_opt._parser) return eval("(" + pugs_opt._parser + ")");
return parser.use(require(pugs), pugs_opt);
}, parser);
return parser.render(data.text);
};
hexo.extend.renderer.register("md", "html", renderer, true);
hexo.extend.renderer.register("markdown", "html", renderer, true);
hexo.extend.renderer.register("mkd", "html", renderer, true);
hexo.extend.renderer.register("mkdn", "html", renderer, true);
hexo.extend.renderer.register("mdwn", "html", renderer, true);
hexo.extend.renderer.register("mdtxt", "html", renderer, true);
hexo.extend.renderer.register("mdtext", "html", renderer, true);