-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcodeModal.ts
41 lines (33 loc) · 1.03 KB
/
codeModal.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
// markdown-it plugin for generating line numbers.
// It depends on preWrapper plugin.
import type MarkdownIt from 'markdown-it'
import type { MarkdownOptions } from '../markdown'
export const codeModalPlugin = (
md: MarkdownIt,
enable = false,
options: MarkdownOptions = {}
) => {
const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => {
const rawCode = fence(...args)
const [tokens, idx] = args
const info = tokens[idx].info
if (
(!enable && !/:modal($| |=)/.test(info)) ||
(enable && /:no-modal($| )/.test(info))
) {
return rawCode
}
let end = rawCode.lastIndexOf('</div>')
let innerCode =
rawCode.substring(0, end) +
`<button title="${options.codeModalButtonTitle}" class="close"></button>` +
'</div>'
const modal =
`<button title="${options.codeModalButtonTitle}" class="modal"></button>` +
'<div class="modal-container">' +
innerCode +
'</div>'
return rawCode.substring(0, end) + modal + '</div>'
}
}