forked from alexeykadochnik0v/js--pro
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfix-metadata.js
More file actions
44 lines (34 loc) · 1.35 KB
/
fix-metadata.js
File metadata and controls
44 lines (34 loc) · 1.35 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
const fs = require("fs");
const path = require("path");
const lessonsDir = path.join(__dirname, "lessons");
const lessonDirs = fs
.readdirSync(lessonsDir)
.filter((dir) => dir.startsWith("lesson"));
lessonDirs.forEach((lessonDir) => {
const lessonPath = path.join(lessonsDir, lessonDir, "lesson.md");
if (fs.existsSync(lessonPath)) {
let content = fs.readFileSync(lessonPath, "utf8");
// Удаляем существующие метаданные, если они есть
content = content.replace(/^---[\s\S]*?---\n\n/, "");
// Извлекаем номер урока из имени директории
const lessonNumber = lessonDir.replace("lesson", "");
// Извлекаем заголовок из первой строки
const titleMatch = content.match(/^# (.*)/);
if (titleMatch) {
const title = titleMatch[1];
// Добавляем метаданные в начало файла
const newContent = `---
title: Урок ${lessonNumber}
description: ${title}
---
${content}`;
fs.writeFileSync(lessonPath, newContent);
console.log(`Fixed metadata in ${lessonDir}/lesson.md`);
} else {
console.log(`Could not find title in ${lessonDir}/lesson.md`);
}
} else {
console.log(`File ${lessonPath} does not exist`);
}
});
console.log("Metadata fixed in all lesson files");