/
markdown.ts
276 lines (250 loc) · 6.13 KB
/
markdown.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Sanitizer } from '@jupyterlab/apputils';
import { IMarkdownParser, renderMarkdown } from '@jupyterlab/rendermime';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { TableOfContents } from '../tokens';
/**
* Markdown heading
*/
export interface IMarkdownHeading extends TableOfContents.IHeading {
/**
* Heading line
*/
line: number;
/**
* Raw string containing the heading
*/
raw: string;
}
/**
* Build the heading html id.
*
* @param raw Raw markdown heading
* @param level Heading level
* @param sanitizer HTML sanitizer
*/
export async function getHeadingId(
markdownParser: IMarkdownParser,
raw: string,
level: number,
sanitizer?: IRenderMime.ISanitizer
): Promise<string | null> {
try {
const host = document.createElement('div');
await renderMarkdown({
markdownParser,
host,
source: raw,
trusted: false,
sanitizer: sanitizer ?? new Sanitizer(),
shouldTypeset: false,
resolver: null,
linkHandler: null,
latexTypesetter: null
});
const header = host.querySelector(`h${level}`);
if (!header) {
return null;
}
return header.id;
} catch (reason) {
console.error('Failed to parse a heading.', reason);
}
return null;
}
/**
* Parses the provided string and returns a list of headings.
*
* @param text - Input text
* @returns List of headings
*/
export function getHeadings(text: string): IMarkdownHeading[] {
// Split the text into lines:
const lines = text.split('\n');
// Iterate over the lines to get the header level and text for each line:
const headings = new Array<IMarkdownHeading>();
let isCodeBlock;
let lineIdx = 0;
// Don't check for Markdown headings if in a YAML frontmatter block.
// We can only start a frontmatter block on the first line of the file.
// At other positions in a markdown file, '---' represents a horizontal rule.
if (lines[lineIdx] === '---') {
// Search for another '---' and treat that as the end of the frontmatter.
// If we don't find one, treat the file as containing no frontmatter.
for (
let frontmatterEndLineIdx = lineIdx + 1;
frontmatterEndLineIdx < lines.length;
frontmatterEndLineIdx++
) {
if (lines[frontmatterEndLineIdx] === '---') {
lineIdx = frontmatterEndLineIdx + 1;
break;
}
}
}
for (; lineIdx < lines.length; lineIdx++) {
const line = lines[lineIdx];
if (line === '') {
// Bail early
continue;
}
// Don't check for Markdown headings if in a code block
if (line.startsWith('```')) {
isCodeBlock = !isCodeBlock;
}
if (isCodeBlock) {
continue;
}
const heading = parseHeading(line, lines[lineIdx + 1]); // append the next line to capture alternative style Markdown headings
if (heading) {
headings.push({
...heading,
line: lineIdx
});
}
}
return headings;
}
const MARKDOWN_MIME_TYPE = [
'text/x-ipythongfm',
'text/x-markdown',
'text/x-gfm',
'text/markdown'
];
/**
* Returns whether a MIME type corresponds to a Markdown flavor.
*
* @param mime - MIME type string
* @returns boolean indicating whether a provided MIME type corresponds to a Markdown flavor
*
* @example
* const bool = isMarkdown('text/markdown');
* // returns true
*
* @example
* const bool = isMarkdown('text/plain');
* // returns false
*/
export function isMarkdown(mime: string): boolean {
return MARKDOWN_MIME_TYPE.includes(mime);
}
/**
* Interface describing a parsed heading result.
*
* @private
*/
interface IHeader {
/**
* Heading text.
*/
text: string;
/**
* Heading level.
*/
level: number;
/**
* Raw string containing the heading
*/
raw: string;
/**
* Whether the heading is marked to skip or not
*/
skip: boolean;
}
/**
* Parses a heading, if one exists, from a provided string.
*
* ## Notes
*
* - Heading examples:
*
* - Markdown heading:
*
* ```
* # Foo
* ```
*
* - Markdown heading (alternative style):
*
* ```
* Foo
* ===
* ```
*
* ```
* Foo
* ---
* ```
*
* - HTML heading:
*
* ```
* <h3>Foo</h3>
* ```
*
* @private
* @param line - Line to parse
* @param nextLine - The line after the one to parse
* @returns heading info
*
* @example
* const out = parseHeading('### Foo\n');
* // returns {'text': 'Foo', 'level': 3}
*
* @example
* const out = parseHeading('Foo\n===\n');
* // returns {'text': 'Foo', 'level': 1}
*
* @example
* const out = parseHeading('<h4>Foo</h4>\n');
* // returns {'text': 'Foo', 'level': 4}
*
* @example
* const out = parseHeading('Foo');
* // returns null
*/
function parseHeading(line: string, nextLine?: string): IHeader | null {
// Case: Markdown heading
let match = line.match(/^([#]{1,6}) (.*)/);
if (match) {
return {
text: cleanTitle(match[2]),
level: match[1].length,
raw: line,
skip: skipHeading.test(match[0])
};
}
// Case: Markdown heading (alternative style)
if (nextLine) {
match = nextLine.match(/^ {0,3}([=]{2,}|[-]{2,})\s*$/);
if (match) {
return {
text: cleanTitle(line),
level: match[1][0] === '=' ? 1 : 2,
raw: [line, nextLine].join('\n'),
skip: skipHeading.test(line)
};
}
}
// Case: HTML heading (WARNING: this is not particularly robust, as HTML headings can span multiple lines)
match = line.match(/<h([1-6]).*>(.*)<\/h\1>/i);
if (match) {
return {
text: match[2],
level: parseInt(match[1], 10),
skip: skipHeading.test(match[0]),
raw: line
};
}
return null;
}
function cleanTitle(heading: string): string {
// take special care to parse Markdown links into raw text
return heading.replace(/\[(.+)\]\(.+\)/g, '$1');
}
/**
* Ignore title with html tag with a class name equal to `jp-toc-ignore` or `tocSkip`
*/
const skipHeading =
/<\w+\s(.*?\s)?class="(.*?\s)?(jp-toc-ignore|tocSkip)(\s.*?)?"(\s.*?)?>/;