-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (47 loc) · 2.48 KB
/
index.js
File metadata and controls
59 lines (47 loc) · 2.48 KB
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
hexo.extend.filter.register('before_post_render', function (data) {
const codeStore = [];
const working = data.content.replace(
/<hexoPostRenderCodeBlock>[\s\S]*?<\/hexoPostRenderCodeBlock>/g,
(match) => {
const id = codeStore.length;
codeStore.push(match);
return `<hexoPostRenderCodeBlock id="${id}"/>`;
}
);
const restoreCodeBlocks = (str) =>
codeStore.length === 0
? str
: str.replace(/<hexoPostRenderCodeBlock id="(\d+)"\/>/g,
(_, id) => codeStore[parseInt(id, 10)]);
const strRegExp = /(?<indent>^[ \t]*)!!!\s*(?<type>note|question|success|info|todo|warning|attention|caution|failure|missing|danger|bug|error|example|quote|tip|abstract|memo|sheet|test)(?<title> .*)\n(?<content>(?:^\k<indent> {4}.*\n|^\n)+)/;
let admonitionRegExp = new RegExp(strRegExp, 'gmi');
if (!admonitionRegExp.test(working)) {
data.content = restoreCodeBlocks(working);
return data;
}
data.content = restoreCodeBlocks(working.replace(admonitionRegExp, (match, ...args) => {
const groups = args.pop();
const requiredIndent = groups.indent.length + 4;
const contentIndentRegex = new RegExp(`^ {${requiredIndent}}`, 'gm');
let content = groups.content.replace(contentIndentRegex, '').trim();
const titleText = groups.title.trim();
let titleHtml = '';
if (titleText !== '""' && titleText !== '') {
let displayTitleText = titleText === '""' ? groups.type : titleText.replace(/^[" ]+|[" ]+$/g, '');
let renderedTitleContent = '';
if (hexo && hexo.render && typeof hexo.render.renderSync === 'function') {
renderedTitleContent = hexo.render.renderSync({ text: displayTitleText, engine: 'markdown' }).trim();
if (renderedTitleContent.startsWith('<p>') && renderedTitleContent.endsWith('</p>')) {
renderedTitleContent = renderedTitleContent.substring(3, renderedTitleContent.length - 4).trim();
}
} else {
hexo.log.warn('Admonition: hexo.render.renderSync not available for title rendering. Title Markdown might not be processed.');
renderedTitleContent = displayTitleText;
}
titleHtml = `<p class="admonition-title">${renderedTitleContent}</p>`;
}
return `<div class="admonition ${groups.type.toLowerCase()}">${titleHtml}\n\n${content}\n\n</div>\n\n`;
}));
return data;
});
hexo.extend.filter.register('after_render:html', require('./lib/addstyle').addStyle);