generated from mintlify/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadScripts.js
More file actions
68 lines (54 loc) · 1.9 KB
/
loadScripts.js
File metadata and controls
68 lines (54 loc) · 1.9 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
const fs = require("fs");
const path = require("path");
const chokidar = require("chokidar");
const MDX_DIR = path.resolve("./docs"); // Folder with your .mdx files
const SNIPPET_DIR = path.resolve("./snippets"); // Folder with code to load
const CODE_BLOCK_REGEX =
/```(\w+)([^\n]*)\{CODE_LOAD::(.+?)\}([^\n]*)\n([\s\S]*?)```/g;
function updateCodeBlocksInFile(filePath) {
if (!fs.existsSync(filePath)) return;
const ext = path.extname(filePath);
if (ext !== ".mdx") return;
const fileContent = fs.readFileSync(filePath, "utf8");
const updatedContent = fileContent.replace(
CODE_BLOCK_REGEX,
(match, lang, metaBefore, loadPath, metaAfter) => {
const fullMeta = (metaBefore + metaAfter).trim();
const snippetFullPath = path.resolve(path.dirname(filePath), loadPath);
if (!fs.existsSync(snippetFullPath)) {
console.warn(`❌ Snippet not found: ${snippetFullPath}`);
return match;
}
const loadedCode = fs.readFileSync(snippetFullPath, "utf8").trimEnd();
return `\`\`\`${lang} ${fullMeta} {CODE_LOAD::${loadPath}}\n${loadedCode}\n\`\`\``;
}
);
fs.writeFileSync(filePath, updatedContent, "utf8");
console.log(`✅ Updated: ${filePath}`);
}
function updateAllMdxFiles() {
fs.readdirSync(MDX_DIR)
.filter((file) => file.endsWith(".mdx"))
.forEach((file) => {
updateCodeBlocksInFile(path.join(MDX_DIR, file));
});
}
function startWatcher() {
console.log("👀 Watching for changes...");
chokidar
.watch([MDX_DIR, SNIPPET_DIR], {
ignoreInitial: false,
persistent: true,
})
.on("change", (filePath) => {
console.log(`✏️ File changed: ${filePath}`);
if (filePath.endsWith(".mdx")) {
updateCodeBlocksInFile(filePath);
} else {
console.log("🔁 Updating all .mdx files (snippet changed)...");
updateAllMdxFiles();
}
});
}
updateAllMdxFiles();
startWatcher();