-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathblock.ts
More file actions
175 lines (147 loc) · 4.72 KB
/
Copy pathblock.ts
File metadata and controls
175 lines (147 loc) · 4.72 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type MarkdownIt from 'markdown-it/lib/index.js';
import type StateBlock from 'markdown-it/lib/rules_block/state_block.js';
import type Token from 'markdown-it/lib/token.js';
/** Parse MyST targets (``(name)=``), blockquotes (``% comment``) and block breaks (``+++``).
*
* Adapted from: mdit_py_plugins/myst_blocks/index.py
*/
export function blockPlugin(md: MarkdownIt): void {
md.block.ruler.before('blockquote', 'myst_line_comment', parse_line_comment, {
alt: ['paragraph', 'reference', 'blockquote', 'list', 'footnote_def'],
});
md.block.ruler.before('hr', 'myst_block_break', parse_block_break, {
alt: ['paragraph', 'reference', 'blockquote', 'list', 'footnote_def'],
});
md.block.ruler.before('hr', 'myst_target', parse_target, {
alt: ['paragraph', 'reference', 'blockquote', 'list', 'footnote_def'],
});
md.renderer.rules.myst_line_comment = render_myst_line_comment;
md.renderer.rules.myst_target = render_myst_target;
}
function parse_line_comment(
state: StateBlock,
startLine: number,
endLine: number,
silent: boolean,
): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine];
let maximum = state.eMarks[startLine];
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) {
return false;
}
if (state.src[pos] !== '%') {
return false;
}
if (silent) {
return true;
}
const token = state.push('myst_line_comment', '', 0);
token.attrSet('class', 'myst-line-comment');
token.content = state.src.slice(pos + 1, maximum).replace(/\s+$/gm, ''); // rstrip
token.markup = '%';
// search end of block while appending lines to `token.content`
let nextLine: number;
for (nextLine = startLine + 1; nextLine < endLine; nextLine++) {
pos = state.bMarks[nextLine] + state.tShift[nextLine];
maximum = state.eMarks[nextLine];
if (state.src[pos] !== '%') {
break;
}
token.content += '\n' + state.src.slice(pos + 1, maximum).replace(/\s+$/gm, ''); // rstrip
}
state.line = nextLine;
token.map = [startLine, nextLine];
return true;
}
function parse_block_break(
state: StateBlock,
startLine: number,
endLine: number,
silent: boolean,
): boolean {
let pos = state.bMarks[startLine] + state.tShift[startLine];
const maximum = state.eMarks[startLine];
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) {
return false;
}
const marker = state.src.charCodeAt(pos);
pos += 1;
// Check block marker /* + */
if (marker !== 0x2b) {
return false;
}
// markers can be mixed with spaces, but there should be at least 3 of them
let cnt = 1;
while (pos < maximum) {
const ch = state.src.charCodeAt(pos);
if (ch !== marker && !state.md.utils.isSpace(ch)) {
break;
}
if (ch === marker) {
cnt += 1;
}
pos += 1;
}
if (cnt < 3) {
return false;
}
if (silent) {
return true;
}
state.line = startLine + 1;
const token = state.push('myst_block_break', 'hr', 0);
token.attrSet('class', 'myst-block');
token.content = state.src.slice(pos, maximum).trim();
token.map = [startLine, state.line];
token.markup = state.md.utils.fromCodePoint(marker).repeat(cnt);
return true;
}
const TARGET_PATTERN = /^\((?<label>[a-zA-Z0-9|@<>*./_\-+:]{1,100})\)=\s*$/;
function parse_target(
state: StateBlock,
startLine: number,
endLine: number,
silent: boolean,
): boolean {
const pos = state.bMarks[startLine] + state.tShift[startLine];
const maximum = state.eMarks[startLine];
// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) {
return false;
}
const match = TARGET_PATTERN.exec(state.src.slice(pos, maximum));
if (!match) {
return false;
}
if (silent) {
return true;
}
state.line = startLine + 1;
const token = state.push('myst_target', '', 0);
token.attrSet('class', 'myst-target');
token.content = match && match.groups ? match.groups['label'] : '';
token.map = [startLine, state.line];
return true;
}
function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function render_myst_line_comment(tokens: Token[], idx: number): string {
const token = tokens[idx];
const content = token.content;
return `<!-- ${escapeHtml(content).trim()} -->`;
}
function render_myst_target(tokens: Token[], idx: number) {
const token = tokens[idx];
const className = 'myst-target';
const label = token.content;
const target = `<a href="#${label}">(${label})=</a>`;
return `<div class="${className}">${target}</div>`;
}