-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
slot-sugar.ts
34 lines (29 loc) · 988 Bytes
/
slot-sugar.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
import type { MarkdownTransformContext } from '@slidev/types'
import { getCodeBlocks } from './utils'
export function transformSlotSugar(
ctx: MarkdownTransformContext,
) {
const linesWithNewline = ctx.s.original.split(/(\r?\n)/g)
const codeBlocks = getCodeBlocks(ctx.s.original)
const lines: string[] = []
for (let i = 0; i < linesWithNewline.length; i += 2) {
const line = linesWithNewline[i]
const newline = linesWithNewline[i + 1] || ''
lines.push(line + newline)
}
let prevSlot = false
let offset = 0
lines.forEach((line) => {
const start = offset
offset += line.length
if (codeBlocks.isInsideCodeblocks(offset))
return
const match = line.match(/^::\s*([\w\.\-\:]+)\s*::(\s*)?$/)
if (match) {
ctx.s.overwrite(start, offset - match[2].length, `${prevSlot ? '\n\n</template>\n' : '\n'}<template v-slot:${match[1]}="slotProps">\n`)
prevSlot = true
}
})
if (prevSlot)
ctx.s.append('\n\n</template>')
}