-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (70 loc) · 2.23 KB
/
script.js
File metadata and controls
90 lines (70 loc) · 2.23 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
const fs = require("fs");
const path = require("path");
function extractHeading1FromMarkdown(filePath) {
try {
const content = fs.readFileSync(filePath, "utf8");
const headingMatch = content.match(/^# (.+)$/m);
if (headingMatch && headingMatch[1]) {
return headingMatch[1].trim();
}
} catch (error) {
console.error(`${filePath} 파일 읽기 오류:`, error);
}
return path.basename(filePath, path.extname(filePath));
}
function scanDirectory(rootDir, depth = 0) {
const result = {};
try {
const items = fs.readdirSync(rootDir);
for (const item of items) {
const itemPath = path.join(rootDir, item);
const stats = fs.statSync(itemPath);
if (stats.isDirectory() && depth === 0) {
result[item] = [];
const subItems = fs.readdirSync(itemPath);
for (const subItem of subItems) {
const subItemPath = path.join(itemPath, subItem);
const subStats = fs.statSync(subItemPath);
if (
subStats.isFile() &&
path.extname(subItem).toLowerCase() === ".md"
) {
const title = extractHeading1FromMarkdown(subItemPath);
result[item].push({
filename: subItem,
title: title,
});
}
}
}
}
return result;
} catch (error) {
console.error("디렉토리 스캔 중 오류 발생:", error);
return {};
}
}
function generateMarkdown(structure) {
let markdown = `# ✏️ Today I Learned
> 언젠가 사라지는 주기억장치를 위한 보조기억장치
`;
for (const [directory, files] of Object.entries(structure)) {
if (files.length === 0) continue;
markdown += `## ${directory}\n`;
for (const file of files) {
markdown += `- [${file.title}](./${directory}/${file.filename})\n`;
}
markdown += "\n";
}
return markdown;
}
const rootDir = "./";
const outputFile = "README.md";
const structure = scanDirectory(rootDir);
const markdown = generateMarkdown(structure);
try {
fs.writeFileSync(outputFile, markdown);
console.log(`마크다운이 성공적으로 생성되었습니다: ${outputFile}`);
} catch (error) {
console.error("마크다운 파일 저장 중 오류 발생:", error);
}