|
| 1 | +// Ported from https://github.com/vuejs/vitepress/blob/main/src/node/markdown/plugins/snippet.ts |
| 2 | + |
| 3 | +import path from 'node:path' |
| 4 | +import fs from 'fs-extra' |
| 5 | +import type { ResolvedSlidevOptions } from '../options' |
| 6 | + |
| 7 | +function dedent(text: string): string { |
| 8 | + const lines = text.split('\n') |
| 9 | + |
| 10 | + const minIndentLength = lines.reduce((acc, line) => { |
| 11 | + for (let i = 0; i < line.length; i++) { |
| 12 | + if (line[i] !== ' ' && line[i] !== '\t') |
| 13 | + return Math.min(i, acc) |
| 14 | + } |
| 15 | + return acc |
| 16 | + }, Number.POSITIVE_INFINITY) |
| 17 | + |
| 18 | + if (minIndentLength < Number.POSITIVE_INFINITY) |
| 19 | + return lines.map(x => x.slice(minIndentLength)).join('\n') |
| 20 | + |
| 21 | + return text |
| 22 | +} |
| 23 | + |
| 24 | +function testLine( |
| 25 | + line: string, |
| 26 | + regexp: RegExp, |
| 27 | + regionName: string, |
| 28 | + end: boolean = false, |
| 29 | +) { |
| 30 | + const [full, tag, name] = regexp.exec(line.trim()) || [] |
| 31 | + |
| 32 | + return ( |
| 33 | + full |
| 34 | + && tag |
| 35 | + && name === regionName |
| 36 | + && tag.match(end ? /^[Ee]nd ?[rR]egion$/ : /^[rR]egion$/) |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +function findRegion(lines: Array<string>, regionName: string) { |
| 41 | + const regionRegexps = [ |
| 42 | + /^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java |
| 43 | + /^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss |
| 44 | + /^#pragma ((?:end)?region) ([\w*-]+)$/, // C, C++ |
| 45 | + /^<!-- #?((?:end)?region) ([\w*-]+) -->$/, // HTML, markdown |
| 46 | + /^#((?:End )Region) ([\w*-]+)$/, // Visual Basic |
| 47 | + /^::#((?:end)region) ([\w*-]+)$/, // Bat |
| 48 | + /^# ?((?:end)?region) ([\w*-]+)$/, // C#, PHP, Powershell, Python, perl & misc |
| 49 | + ] |
| 50 | + |
| 51 | + let regexp = null |
| 52 | + let start = -1 |
| 53 | + |
| 54 | + for (const [lineId, line] of lines.entries()) { |
| 55 | + if (regexp === null) { |
| 56 | + for (const reg of regionRegexps) { |
| 57 | + if (testLine(line, reg, regionName)) { |
| 58 | + start = lineId + 1 |
| 59 | + regexp = reg |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + else if (testLine(line, regexp, regionName, true)) { |
| 65 | + return { start, end: lineId, regexp } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * format: ">>> /path/to/file.extension#region language meta..." |
| 74 | + * where #region, language and meta are optional |
| 75 | + * meta should starts with { |
| 76 | + * lang can contain special characters like C++, C#, F#, etc. |
| 77 | + * path can be relative to the current file or absolute |
| 78 | + * file extension is optional |
| 79 | + * path can contain spaces and dots |
| 80 | + * |
| 81 | + * captures: ['/path/to/file.extension', '#region', 'language', '{meta}'] |
| 82 | + */ |
| 83 | +export function transformSnippet(md: string, options: ResolvedSlidevOptions, id: string) { |
| 84 | + const slideId = (id as string).match(/(\d+)\.md$/)?.[1] |
| 85 | + if (!slideId) |
| 86 | + return md |
| 87 | + const data = options.data |
| 88 | + const slideInfo = data.slides[+slideId - 1] |
| 89 | + const dir = path.dirname(slideInfo.source?.filepath ?? options?.entry ?? options!.userRoot) |
| 90 | + return md.replace( |
| 91 | + /^<<< *(.+?)(#[\w-]+)? *(?: (\S+?))? *(\{.*)?$/mg, |
| 92 | + (full, filepath = '', regionName = '', lang = '', meta = '') => { |
| 93 | + const firstLine = `\`\`\`${lang || path.extname(filepath).slice(1)} ${meta}` |
| 94 | + |
| 95 | + const src = /^\@[\/]/.test(filepath) |
| 96 | + ? path.resolve(options!.userRoot, filepath.slice(2)) |
| 97 | + : path.resolve(dir, filepath) |
| 98 | + |
| 99 | + data.entries!.push(src) |
| 100 | + |
| 101 | + const isAFile = fs.statSync(src).isFile() |
| 102 | + if (!fs.existsSync(src) || !isAFile) { |
| 103 | + throw new Error(isAFile |
| 104 | + ? `Code snippet path not found: ${src}` |
| 105 | + : `Invalid code snippet option`) |
| 106 | + } |
| 107 | + |
| 108 | + let content = fs.readFileSync(src, 'utf8') |
| 109 | + |
| 110 | + slideInfo.snippetsUsed ??= {} |
| 111 | + slideInfo.snippetsUsed[src] = content |
| 112 | + |
| 113 | + if (regionName) { |
| 114 | + const lines = content.split(/\r?\n/) |
| 115 | + const region = findRegion(lines, regionName.slice(1)) |
| 116 | + |
| 117 | + if (region) { |
| 118 | + content = dedent( |
| 119 | + lines |
| 120 | + .slice(region.start, region.end) |
| 121 | + .filter(line => !region.regexp.test(line.trim())) |
| 122 | + .join('\n'), |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return `${firstLine}\n${content}\n\`\`\`` |
| 128 | + }, |
| 129 | + ) |
| 130 | +} |
0 commit comments