-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.markdown-preview.ts
More file actions
85 lines (68 loc) · 2.68 KB
/
Copy pathvite.markdown-preview.ts
File metadata and controls
85 lines (68 loc) · 2.68 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { readFile } from 'node:fs/promises';
import { transform } from 'esbuild';
import { normalizePath, type Plugin } from 'vite';
const previewFence = /```(jsx|tsx)\s+mdx:preview[^\n]*\n([\s\S]*?)```/g;
const previewModulePrefix = '\0markdown-preview:';
const publicPreviewModulePrefix = 'virtual:markdown-preview:';
const escapeTemplate = (value: string) => value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${');
const quoteString = (value: string) => JSON.stringify(value);
const toFragmentPreview = (code: string) => `
import React from 'react';
export default function MarkdownPreviewExample() {
return (
<>${code}</>
);
}
`;
const hasDefaultExport = (code: string) => /export\s+default\s+/.test(code);
export function markdownPreviewPlugin(): Plugin {
const examples = new Map<string, string>();
return {
name: 'markdown-preview',
enforce: 'pre',
resolveId(id) {
if (id.startsWith(publicPreviewModulePrefix)) {
return `${previewModulePrefix}${id.slice(publicPreviewModulePrefix.length)}`;
}
return null;
},
async load(id) {
if (id.startsWith(previewModulePrefix)) {
const key = id.slice(previewModulePrefix.length);
const code = examples.get(key);
if (!code) return null;
const result = await transform(hasDefaultExport(code) ? code : toFragmentPreview(code), {
loader: 'tsx',
jsx: 'automatic',
format: 'esm'
});
return result.code;
}
const [filepath, query = ''] = id.split('?');
if (query !== 'preview' || !filepath.endsWith('.md')) return null;
const markdown = await readFile(filepath, 'utf8');
const previewImports: string[] = [];
const previewExports: string[] = [];
const previewCodes: string[] = [];
const normalizedPath = normalizePath(filepath);
const previewMarkdown = markdown.replace(previewFence, (_block: string, language: string, code: string) => {
const index = previewImports.length;
const source = code.trim();
const key = `${normalizedPath}:${index}`;
examples.set(key, source);
previewImports.push(`import Preview${index} from '${publicPreviewModulePrefix}${key}';`);
previewExports.push(`Preview${index}`);
previewCodes.push(source);
return `\`\`\`${language} mdx:preview=${index}\n${code}\`\`\``;
});
return `
${previewImports.join('\n')}
const markdown = \`${escapeTemplate(previewMarkdown)}\`;
const previews = [${previewExports.join(', ')}];
const codes = [${previewCodes.map(quoteString).join(', ')}];
export { markdown, previews, codes };
export default { markdown, previews, codes };
`;
}
};
}