forked from alexeykadochnik0v/js--pro
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadd-metadata.js
More file actions
41 lines (32 loc) · 1.3 KB
/
add-metadata.js
File metadata and controls
41 lines (32 loc) · 1.3 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
const fs = require("fs");
const path = require("path");
const lessonsDir = path.join(__dirname, "lessons");
const lessonDirs = fs
.readdirSync(lessonsDir)
.filter((dir) => dir.startsWith("lesson") && dir !== "lesson01"); // Пропускаем lesson01, так как уже обработали
lessonDirs.forEach((lessonDir) => {
const lessonPath = path.join(lessonsDir, lessonDir, "lesson.md");
if (fs.existsSync(lessonPath)) {
let content = fs.readFileSync(lessonPath, "utf8");
// Извлекаем номер урока из имени директории
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(`Added metadata to ${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 added to all lesson files");