|
1 | 1 | import { resolve } from "path"; |
| 2 | +import { readFileSync } from "fs"; |
2 | 3 | import { defineConfig, mergeConfig } from "vite"; |
3 | 4 |
|
| 5 | +function formatMd(content) { |
| 6 | + let result = content; |
| 7 | + result = result.replace(/^---[\s\S]*?---\n*/, ""); |
| 8 | + result = result.replace( |
| 9 | + /^\s*:{3,}\s*\w[^\n]*\n([\s\S]*?)^\s*:{3,}\s*$/gm, |
| 10 | + (_, inner) => { |
| 11 | + return inner |
| 12 | + .trimEnd() |
| 13 | + .split("\n") |
| 14 | + .map((line) => `> ${line}`) |
| 15 | + .join("\n"); |
| 16 | + } |
| 17 | + ); |
| 18 | + return result; |
| 19 | +} |
| 20 | + |
| 21 | +function formatMarkdownPlugin() { |
| 22 | + return { |
| 23 | + name: "format-markdown-raw", |
| 24 | + enforce: "pre", |
| 25 | + load(id) { |
| 26 | + if (/\.md\?raw$/.test(id)) { |
| 27 | + const filePath = id.replace(/\?raw$/, ""); |
| 28 | + const content = readFileSync(filePath, "utf-8"); |
| 29 | + return `export default ${JSON.stringify(formatMd(content))}`; |
| 30 | + } |
| 31 | + }, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
4 | 35 | function transformCodePlugin(mode) { |
5 | 36 | return { |
6 | 37 | name: "transform-code-plugin", |
@@ -60,10 +91,10 @@ const iifeConfig = { |
60 | 91 |
|
61 | 92 | // 根据命令行参数选择配置 |
62 | 93 | export default defineConfig(({ command, mode }) => { |
| 94 | + const plugins = [formatMarkdownPlugin()]; |
63 | 95 | if (mode === "iife") { |
64 | | - return mergeConfig(iifeConfig, { |
65 | | - plugins: [transformCodePlugin(mode)], |
66 | | - }); |
| 96 | + plugins.push(transformCodePlugin(mode)); |
| 97 | + return mergeConfig(iifeConfig, { plugins }); |
67 | 98 | } |
68 | | - return esmConfig; |
| 99 | + return mergeConfig(esmConfig, { plugins }); |
69 | 100 | }); |
0 commit comments