-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsync-managed-content.mjs
More file actions
159 lines (136 loc) · 4.24 KB
/
Copy pathsync-managed-content.mjs
File metadata and controls
159 lines (136 loc) · 4.24 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { cp, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
import { basename, dirname, extname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const runtimeRoot = resolve(fileURLToPath(new URL("..", import.meta.url)));
const repoRoot = resolve(runtimeRoot, "../..");
const authoredRoots = [
{ source: resolve(repoRoot, "docs"), target: resolve(runtimeRoot, "app/docs") },
{ source: resolve(repoRoot, "api-reference"), target: resolve(runtimeRoot, "app/docs/api") },
];
const ignoredDirectoryNames = new Set([
"node_modules",
".next",
".turbo",
".vercel",
"dist",
"build",
"coverage",
]);
const ignoredFileNames = new Set([
"bun.lock",
"jsconfig.json",
"package-lock.json",
"package.json",
"pnpm-lock.yaml",
"tsconfig.json",
"yarn.lock",
]);
const staticAssetExtensions = new Set([
".avif",
".bmp",
".csv",
".gif",
".ico",
".jpeg",
".jpg",
".json",
".mp4",
".pdf",
".png",
".svg",
".txt",
".webm",
".webp",
".zip",
]);
const codeFenceLanguageAliases = new Map([
["dotenv", "bash"],
["env", "bash"],
["shell", "bash"],
]);
function isMarkdownFile(path) {
return [".md", ".mdx"].includes(extname(path).toLowerCase());
}
function isStaticAssetFile(path) {
return staticAssetExtensions.has(extname(path).toLowerCase());
}
function shouldSkipDirectory(name) {
return name.startsWith(".") || ignoredDirectoryNames.has(name);
}
function shouldSkipFile(name) {
return name.startsWith(".") || ignoredFileNames.has(name);
}
function normalizeMarkdownContent(content) {
const fenceMarker = String.fromCharCode(96);
const codeFencePattern = new RegExp(
"(^|\\n)(" + fenceMarker + "{3,})([A-Za-z0-9_+.-]+)([^\\n" + fenceMarker + "]*)",
"g",
);
return content.replace(codeFencePattern, (match, prefix, fence, language, rest = "") => {
const normalizedLanguage = codeFenceLanguageAliases.get(language.toLowerCase());
if (!normalizedLanguage) {
return match;
}
return prefix + fence + normalizedLanguage + rest;
});
}
function targetPagePath(targetRoot, relativePath) {
const withoutExtension = relativePath.replace(/\.mdx?$/i, "");
const routeFileName = basename(withoutExtension).toLowerCase();
const isIndexPage = routeFileName === "index" || routeFileName === "page";
const targetDirectory = isIndexPage ? dirname(withoutExtension) : withoutExtension;
return join(targetRoot, targetDirectory === "." ? "" : targetDirectory, "page.mdx");
}
async function fileExists(path) {
try {
await readdir(path);
return true;
} catch {
try {
await readFile(path, "utf8");
return true;
} catch {
return false;
}
}
}
async function syncAuthoredRoot(sourceRoot, targetRoot) {
if (!(await fileExists(sourceRoot))) {
return;
}
const visit = async (currentSourceDirectory, relativeDirectory = "") => {
const entries = await readdir(currentSourceDirectory, { withFileTypes: true });
for (const entry of entries) {
if (entry.name.startsWith(".")) {
continue;
}
const sourcePath = join(currentSourceDirectory, entry.name);
const relativePath = relativeDirectory ? join(relativeDirectory, entry.name) : entry.name;
if (entry.isDirectory()) {
if (shouldSkipDirectory(entry.name)) {
continue;
}
await visit(sourcePath, relativePath);
continue;
}
if (!isMarkdownFile(entry.name)) {
if (shouldSkipFile(entry.name) || !isStaticAssetFile(entry.name)) {
continue;
}
const targetPath = join(targetRoot, relativePath);
await mkdir(dirname(targetPath), { recursive: true });
await cp(sourcePath, targetPath, { force: true });
continue;
}
const targetPath = targetPagePath(targetRoot, relativePath);
await mkdir(dirname(targetPath), { recursive: true });
await writeFile(targetPath, normalizeMarkdownContent(await readFile(sourcePath, "utf8")), "utf8");
}
};
await visit(sourceRoot);
}
await rm(resolve(runtimeRoot, "app/docs"), { recursive: true, force: true });
await mkdir(resolve(runtimeRoot, "app/docs"), { recursive: true });
for (const authoredRoot of authoredRoots) {
await syncAuthoredRoot(authoredRoot.source, authoredRoot.target);
}