-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync_content.js
More file actions
166 lines (144 loc) · 4.89 KB
/
sync_content.js
File metadata and controls
166 lines (144 loc) · 4.89 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
160
161
162
163
164
165
166
#!/usr/bin/env node
/**
* Synchronizes content from the CMS-friendly structure to Docusaurus i18n structure
* using symlinks, so that Docusaurus hot reload works during development.
*
* CMS structure (multiple_folders - Sveltia CMS compatible):
* content/docs/en/...
* content/docs/es/...
* content/tutorials/en/...
* content/tutorials/es/...
*
* Docusaurus structure (what Docusaurus expects):
* docs/ -> content/docs/en
* i18n/es/docusaurus-plugin-content-docs/current/ -> content/docs/es
* tutorials/ -> content/tutorials/en
* i18n/es/docusaurus-plugin-content-docs-tutorials/current/ -> content/tutorials/es
* changelog/ -> content/changelog/en
* i18n/es/docusaurus-plugin-content-blog-changelog/ -> content/changelog/es
*
* Run this before starting the site.
*/
const fs = require("fs");
const path = require("path");
const ROOT = __dirname;
// Mapping: source (CMS) -> destination (Docusaurus)
const SYNC_MAP = [
{
source: "content/docs/en",
dest: "docs",
description: "English docs",
},
{
source: "content/docs/es",
dest: "i18n/es/docusaurus-plugin-content-docs/current",
description: "Spanish docs",
},
{
source: "content/tutorials/en",
dest: "tutorials",
description: "English tutorials",
},
{
source: "content/tutorials/es",
dest: "i18n/es/docusaurus-plugin-content-docs-tutorials/current",
description: "Spanish tutorials",
},
{
source: "content/changelog/en",
dest: "changelog",
description: "English changelog",
},
{
source: "content/changelog/es",
dest: "i18n/es/docusaurus-plugin-content-blog-changelog",
description: "Spanish changelog",
},
];
/**
* Create a symlink from dest pointing to src.
* Removes any existing file, directory, or symlink at dest first.
*/
function createSymlink(src, dest) {
const srcAbs = path.resolve(ROOT, src);
const destAbs = path.resolve(ROOT, dest);
if (!fs.existsSync(srcAbs)) {
console.log(` ⏭️ Skipping (source not found): ${src}`);
return;
}
// Remove existing dest (dir, file, or broken symlink)
try {
fs.lstatSync(destAbs); // throws if nothing exists
fs.rmSync(destAbs, { recursive: true, force: true });
} catch (_) {
// dest doesn't exist, nothing to remove
}
// Ensure parent directory exists
fs.mkdirSync(path.dirname(destAbs), { recursive: true });
fs.symlinkSync(srcAbs, destAbs, "dir");
console.log(` ✓ ${dest} → ${src}`);
}
/**
* Sync tutorial images from content/tutorials/[slug]/ to static/img/tutorials/[slug]/
* This handles images uploaded by the CMS which creates folders like content/tutorials/install-keda/
*/
function syncTutorialImages() {
const tutorialsDir = path.join(ROOT, "content/tutorials");
const staticImgDir = path.join(ROOT, "static/img/tutorials");
if (!fs.existsSync(tutorialsDir)) {
return { count: 0, synced: [] };
}
const entries = fs.readdirSync(tutorialsDir, { withFileTypes: true });
let count = 0;
const synced = [];
for (const entry of entries) {
// Skip language folders (en, es) and non-directories
if (!entry.isDirectory() || ["en", "es"].includes(entry.name)) {
continue;
}
const srcDir = path.join(tutorialsDir, entry.name);
const destDir = path.join(staticImgDir, entry.name);
// Check if the source folder contains any image files
const srcContents = fs.readdirSync(srcDir, { withFileTypes: true });
const imageFiles = srcContents.filter(
(f) => f.isFile() && /\.(png|jpg|jpeg|gif|webp|svg)$/i.test(f.name),
);
if (imageFiles.length > 0) {
// Create destination directory
fs.mkdirSync(destDir, { recursive: true });
// Copy image files
for (const imgFile of imageFiles) {
const srcPath = path.join(srcDir, imgFile.name);
const destPath = path.join(destDir, imgFile.name);
fs.copyFileSync(srcPath, destPath);
count++;
synced.push({
from: path.relative(ROOT, srcPath),
to: path.relative(ROOT, destPath),
});
}
}
}
return { count, synced };
}
function main() {
console.log(
"🔗 Linking content from CMS structure to Docusaurus structure...\n",
);
for (const mapping of SYNC_MAP) {
createSymlink(mapping.source, mapping.dest);
}
// Sync tutorial images from CMS folders to static/img/tutorials
console.log("\n🖼️ Syncing tutorial images...");
const imgStats = syncTutorialImages();
if (imgStats.count > 0) {
console.log(` ✓ Synced ${imgStats.count} tutorial image(s):`);
for (const item of imgStats.synced) {
console.log(` ${item.from} → ${item.to}`);
}
} else {
console.log(" ℹ️ No new tutorial images to sync");
}
console.log("\n✅ Content sync complete!");
}
main();