Skip to content

Commit e9cd66e

Browse files
author
niuweili
committed
feat: add replace md
1 parent dd58704 commit e9cd66e

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

vite.config.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
import { resolve } from "path";
2+
import { readFileSync } from "fs";
23
import { defineConfig, mergeConfig } from "vite";
34

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+
435
function transformCodePlugin(mode) {
536
return {
637
name: "transform-code-plugin",
@@ -60,10 +91,10 @@ const iifeConfig = {
6091

6192
// 根据命令行参数选择配置
6293
export default defineConfig(({ command, mode }) => {
94+
const plugins = [formatMarkdownPlugin()];
6395
if (mode === "iife") {
64-
return mergeConfig(iifeConfig, {
65-
plugins: [transformCodePlugin(mode)],
66-
});
96+
plugins.push(transformCodePlugin(mode));
97+
return mergeConfig(iifeConfig, { plugins });
6798
}
68-
return esmConfig;
99+
return mergeConfig(esmConfig, { plugins });
69100
});

0 commit comments

Comments
 (0)