-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
utils.ts
32 lines (29 loc) · 960 Bytes
/
utils.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
export function normalizeRangeStr(rangeStr = '') {
return !rangeStr.trim() ? [] : rangeStr.trim().split(/\|/g).map(i => i.trim())
}
export function getCodeBlocks(md: string) {
const codeblocks = Array
.from(md.matchAll(/^```[\s\S]*?^```/mg))
.map((m) => {
const start = m.index!
const end = m.index! + m[0].length
const startLine = md.slice(0, start).match(/\n/g)?.length || 0
const endLine = md.slice(0, end).match(/\n/g)?.length || 0
return [start, end, startLine, endLine]
})
return {
codeblocks,
isInsideCodeblocks(idx: number) {
return codeblocks.some(([s, e]) => s <= idx && idx <= e)
},
isLineInsideCodeblocks(line: number) {
return codeblocks.some(([, , s, e]) => s <= line && line <= e)
},
}
}
/**
* Escape `{{` in code block to prevent Vue interpret it, #99, #1316
*/
export function escapeVueInCode(md: string) {
return md.replace(/{{/g, '{{')
}