返回 slidev
drag.ts
根目录 / packages / slidev / node / syntax / drag.ts
1 import type MagicString from 'magic-string-stack'
2 import type MarkdownExit from 'markdown-exit'
3 import { SourceMapConsumer } from 'source-map-js'
4
5 type Token = ReturnType<MarkdownExit['parseInline']>[number]
6
7 const dragComponentRegex = /<(v-?drag-?\w*)([\s>])/i
8 const dragDirectiveRegex = /(?<![</\w])v-drag(=".*?")?/i
9
10 export default function MarkdownItVDrag(md: MarkdownExit, markdownTransformMap: Map<string, MagicString>) {
11 const visited = new WeakSet()
12 const sourceMapConsumers = new WeakMap<MagicString, SourceMapConsumer>()
13
14 function getSourceMapConsumer(id: string | undefined) {
15 const s = id && markdownTransformMap.get(id)
16 if (!s)
17 return undefined
18 let smc = sourceMapConsumers.get(s)
19 if (smc)
20 return smc
21 const sourceMap = s.generateMap()
22 smc = new SourceMapConsumer({
23 ...sourceMap,
24 version: sourceMap.version.toString(),
25 sourcesContent: sourceMap.sourcesContent?.map(content => content ?? ''),
26 })
27 sourceMapConsumers.set(s, smc)
28 return smc
29 }
30
31 const _parse = md.parse
32 md.parse = function (src, env) {
33 const smc = getSourceMapConsumer(env?.id)
34 // A generated line past the file (the exclusive end of a block that ends
35 // the slide) is unmapped. `null - 1` is -1, and `Math.max(start + 1, -1)`
36 // then shrinks the range to a single line, so the write-back misses `v-drag`.
37 const toOriginalPos = (line: number) => {
38 if (!smc)
39 return line
40 const mapped = smc.originalPositionFor({ line: line + 1, column: 0 }).line
41 return mapped == null || mapped <= 0
42 ? null
43 : mapped - 1
44 }
45 function toMarkdownSource(map: [number, number], idx: number) {
46 const start = toOriginalPos(map[0]) ?? map[0]
47 let end = toOriginalPos(map[1])
48 if (end == null) {
49 end = start + 1
50 for (let line = map[1] - 1; line > map[0]; line--) {
51 const mapped = toOriginalPos(line)
52 if (mapped != null) {
53 end = mapped + 1
54 break
55 }
56 }
57 }
58 return `[${start},${Math.max(start + 1, end)},${idx}]`
59 }
60
61 function replaceChildren(token: Token, regex: RegExp, replacement: string) {
62 for (const child of token.children ?? []) {
63 if (child.type === 'html_block' || child.type === 'html_inline') {
64 child.content = child.content.replace(regex, replacement)
65 }
66 replaceChildren(child, regex, replacement)
67 }
68 }
69
70 return _parse.call(this, src, env)
71 .map((token) => {
72 if (!['html_block', 'html_inline', 'inline'].includes(token.type) || !token.content.includes('drag') || visited.has(token))
73 return token
74
75 // Iterates all html tokens and replaces <v-drag> with <v-drag :markdownSource="..."> to pass the markdown source to the component
76 token.content = token.content
77 .replace(dragComponentRegex, (_, tag, space, idx) => {
78 const replacement = `<${tag} :markdownSource="${toMarkdownSource(token.map!, idx)}"${space}`
79 replaceChildren(token, dragComponentRegex, replacement)
80 return replacement
81 })
82 .replace(dragDirectiveRegex, (_, value, idx) => {
83 const replacement = `v-drag${value ?? ''} :markdownSource="${toMarkdownSource(token.map!, idx)}"`
84 replaceChildren(token, dragDirectiveRegex, replacement)
85 return replacement
86 })
87
88 visited.add(token)
89 return token
90 })
91 }
92 }
93
93 lines TYPESCRIPT