-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-editor.js
More file actions
334 lines (282 loc) · 9.05 KB
/
content-editor.js
File metadata and controls
334 lines (282 loc) · 9.05 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @file content-editor.js
*
* Content editor server for managing text variables extracted from templates
* Provides API endpoints for reading and updating content files
*/
const express = require('express');
const fs = require('fs');
const path = require('path');
const yaml = require('yaml');
const app = express();
app.use(express.json());
app.use(express.static('public'));
class ContentManager {
constructor(projectPath) {
this.projectPath = projectPath;
this.contentPath = path.join(projectPath, 'content');
this.settingsPath = path.join(projectPath, 'settings');
this.i18nPath = path.join(projectPath, '_i18n');
}
/**
* Get all content files for a specific page
*/
getPageContent(pageName) {
const pageContentPath = path.join(this.contentPath, pageName);
const pageSettingsPath = path.join(this.settingsPath, pageName);
if (!fs.existsSync(pageContentPath)) {
throw new Error(`Page '${pageName}' not found`);
}
const contentFiles = fs.readdirSync(pageContentPath).filter(file =>
file.endsWith('.md') || file.endsWith('.mdx')
);
const content = {};
contentFiles.forEach(file => {
const fileName = path.parse(file).name;
const filePath = path.join(pageContentPath, file);
const settingsFile = path.join(pageSettingsPath, fileName + '.json');
content[fileName] = {
text: fs.readFileSync(filePath, 'utf8'),
settings: fs.existsSync(settingsFile) ?
JSON.parse(fs.readFileSync(settingsFile, 'utf8')) : {}
};
});
return content;
}
/**
* Get i18n variables and their translations
*/
getI18nVariables() {
if (!fs.existsSync(this.i18nPath)) {
return {};
}
const languages = {};
const i18nFiles = fs.readdirSync(this.i18nPath).filter(file =>
file.endsWith('.yml') || file.endsWith('.yaml')
);
i18nFiles.forEach(file => {
const lang = path.parse(file).name;
const filePath = path.join(this.i18nPath, file);
const content = fs.readFileSync(filePath, 'utf8');
languages[lang] = yaml.parse(content);
});
return languages;
}
/**
* Update content file
*/
updateContent(pageName, sectionName, newText) {
const contentFile = path.join(this.contentPath, pageName, sectionName + '.md');
if (!fs.existsSync(contentFile)) {
throw new Error(`Content file '${sectionName}' not found for page '${pageName}'`);
}
fs.writeFileSync(contentFile, newText, 'utf8');
return true;
}
/**
* Update i18n translation
*/
updateI18nTranslation(language, key, value) {
const i18nFile = path.join(this.i18nPath, language + '.yml');
let translations = {};
if (fs.existsSync(i18nFile)) {
const content = fs.readFileSync(i18nFile, 'utf8');
translations = yaml.parse(content) || {};
}
// Set nested key
this.setNestedKey(translations, key, value);
const yamlContent = yaml.stringify(translations);
fs.writeFileSync(i18nFile, yamlContent, 'utf8');
return true;
}
/**
* Helper to set nested object key
*/
setNestedKey(obj, key, value) {
const keys = key.split('.');
let current = obj;
for (let i = 0; i < keys.length - 1; i++) {
if (!current[keys[i]]) {
current[keys[i]] = {};
}
current = current[keys[i]];
}
current[keys[keys.length - 1]] = value;
}
/**
* Get all available projects/pages
*/
getProjects() {
if (!fs.existsSync(this.contentPath)) {
return [];
}
return fs.readdirSync(this.contentPath).filter(item => {
const itemPath = path.join(this.contentPath, item);
return fs.statSync(itemPath).isDirectory();
});
}
/**
* Get JSON files for a specific page
*/
getJsonFiles(pageName) {
const jsonFiles = {};
const contentFiles = ['menu.json', 'footer.json', 'settings.json'];
contentFiles.forEach(fileName => {
const filePath = path.join(this.contentPath, fileName);
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf8');
try {
jsonFiles[path.parse(fileName).name] = JSON.parse(content);
} catch (error) {
console.error(`Error parsing JSON file ${fileName}:`, error);
}
}
});
return jsonFiles;
}
/**
* Get specific JSON file content
*/
getJsonFile(fileName) {
const filePath = path.join(this.contentPath, fileName + '.json');
if (!fs.existsSync(filePath)) {
throw new Error(`JSON file '${fileName}.json' not found`);
}
const content = fs.readFileSync(filePath, 'utf8');
return JSON.parse(content);
}
/**
* Update JSON file content
*/
updateJsonFile(fileName, content) {
const filePath = path.join(this.contentPath, fileName + '.json');
if (!fs.existsSync(filePath)) {
throw new Error(`JSON file '${fileName}.json' not found`);
}
const jsonContent = JSON.stringify(content, null, 2);
fs.writeFileSync(filePath, jsonContent, 'utf8');
return true;
}
}
// Initialize with command line argument or default
const projectPath = process.argv[2] || './client';
const contentManager = new ContentManager(projectPath);
// API Routes
/**
* Get all available projects
*/
app.get('/api/projects', (req, res) => {
try {
const projects = contentManager.getProjects();
res.json({ projects });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* Get content for a specific page
*/
app.get('/api/content/:pageName', (req, res) => {
try {
const content = contentManager.getPageContent(req.params.pageName);
res.json(content);
} catch (error) {
res.status(404).json({ error: error.message });
}
});
/**
* Get i18n variables
*/
app.get('/api/i18n', (req, res) => {
try {
const i18n = contentManager.getI18nVariables();
res.json(i18n);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* Update content
*/
app.post('/api/content/:pageName/:sectionName', (req, res) => {
try {
const { text } = req.body;
if (!text && text !== '') {
return res.status(400).json({ error: 'Text content is required' });
}
const success = contentManager.updateContent(
req.params.pageName,
req.params.sectionName,
text
);
res.json({ success, message: 'Content updated successfully' });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* Update i18n translation
*/
app.post('/api/i18n/:language', (req, res) => {
try {
const { key, value } = req.body;
if (!key || (!value && value !== '')) {
return res.status(400).json({ error: 'Key and value are required' });
}
const success = contentManager.updateI18nTranslation(
req.params.language,
key,
value
);
res.json({ success, message: 'Translation updated successfully' });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* Get JSON files for a specific page
*/
app.get('/api/json/:pageName', (req, res) => {
try {
const jsonFiles = contentManager.getJsonFiles(req.params.pageName);
res.json(jsonFiles);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* Get specific JSON file content
*/
app.get('/api/json/:pageName/:fileName', (req, res) => {
try {
const content = contentManager.getJsonFile(req.params.fileName);
res.json(content);
} catch (error) {
res.status(404).json({ error: error.message });
}
});
/**
* Update JSON file content
*/
app.post('/api/json/:pageName/:fileName', (req, res) => {
try {
const { content } = req.body;
if (!content) {
return res.status(400).json({ error: 'Content is required' });
}
const success = contentManager.updateJsonFile(req.params.fileName, content);
res.json({ success, message: 'JSON file updated successfully' });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Serve the editor interface
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Content Editor running on http://localhost:${PORT}`);
console.log(`Managing content in: ${projectPath}`);
});
module.exports = { ContentManager };