/
index.ts
131 lines (116 loc) · 3.48 KB
/
index.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable */
import type {Transformer} from 'unified';
import visit from 'unist-util-visit';
const NEWLINE = '\n';
const config = {
tag: ':::',
keywords: [
'secondary',
'info',
'success',
'danger',
'note',
'tip',
'warning',
'important',
'caution',
],
};
function escapeRegExp(s: string): string {
return s.replace(new RegExp(`[-[\\]{}()*+?.\\\\^$|/]`, 'g'), '\\$&');
}
export default function plugin(this: any): Transformer {
const keywords = Object.values(config.keywords).map(escapeRegExp).join('|');
const tag = escapeRegExp(config.tag);
const regex = new RegExp(`${tag}(${keywords})(?: *(.*))?\n`);
const escapeTag = new RegExp(escapeRegExp(`\\${config.tag}`), 'g');
// the tokenizer is called on blocks to determine if there is an admonition present and create tags for it
function blockTokenizer(this: any, eat: any, value: string, silent: boolean) {
// stop if no match or match does not start at beginning of line
const match = regex.exec(value);
if (!match || match.index !== 0) return false;
// if silent return the match
if (silent) return true;
const now = eat.now();
const [opening, keyword, title] = match;
const food = [];
const content = [];
// consume lines until a closing tag
let idx = 0;
while ((idx = value.indexOf(NEWLINE)) !== -1) {
// grab this line and eat it
const next = value.indexOf(NEWLINE, idx + 1);
const line =
next !== -1 ? value.slice(idx + 1, next) : value.slice(idx + 1);
food.push(line);
value = value.slice(idx + 1);
// the closing tag is NOT part of the content
if (line.startsWith(config.tag)) break;
content.push(line);
}
// consume the processed tag and replace escape sequences
const contentString = content.join(NEWLINE).replace(escapeTag, config.tag);
const add = eat(opening + food.join(NEWLINE));
// parse the content in block mode
const exit = this.enterBlock();
const contentNodes = this.tokenizeBlock(contentString, now);
exit();
const element = {
type: 'admonitionHTML',
data: {
hName: 'admonition',
hProperties: {
title,
type: keyword,
},
},
children: contentNodes,
};
return add(element);
}
// add tokenizer to parser after fenced code blocks
const Parser = (this as any).Parser.prototype;
Parser.blockTokenizers.admonition = blockTokenizer;
Parser.blockMethods.splice(
Parser.blockMethods.indexOf('fencedCode') + 1,
0,
'admonition',
);
Parser.interruptParagraph.splice(
Parser.interruptParagraph.indexOf('fencedCode') + 1,
0,
['admonition'],
);
Parser.interruptList.splice(
Parser.interruptList.indexOf('fencedCode') + 1,
0,
['admonition'],
);
Parser.interruptBlockquote.splice(
Parser.interruptBlockquote.indexOf('fencedCode') + 1,
0,
['admonition'],
);
return function transformer(tree) {
// escape everything except admonitionHTML nodes
// @ts-ignore FIXME
visit(
tree,
(node: any) => {
return node.type !== 'admonitionHTML';
},
function visitor(node: any) {
if (node.value) {
node.value = node.value.replace(escapeTag, config.tag);
}
return node;
},
);
};
}