-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathadd-front-matter.js
58 lines (52 loc) · 1.95 KB
/
add-front-matter.js
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
const directoryPath = 'docs/nodeapi'; // Update with your documentation directory path
const fs = require('fs');
const path = require('path');
function addFrontMatterToFiles(dir) {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
addFrontMatterToFiles(fullPath); // Recurse into subdirectories
} else if (fullPath.endsWith('.md') && path.basename(fullPath) === 'README.md') {
const content = fs.readFileSync(fullPath, 'utf8');
const frontMatter = `---
layout: default
title: NodeJS Api
nav_order: 9
has_children: true
---
`;
if(!content.startsWith('---')) {
// fs.writeFileSync(fullPath, frontMatter + content.replace(/\.md#/g, ".html#"));
fs.writeFileSync(fullPath, frontMatter + content.replace(/\.md/g, ".html"));
} else {
fs.writeFileSync(fullPath, content.replace(/\.md/g, ".html"));
}
} else if (fullPath.endsWith('.md') && path.basename(fullPath) !== 'README.md') {
const content = fs.readFileSync(fullPath, 'utf8');
const frontMatterExclude = `---
layout: page
title: ${path.basename(fullPath, '.md')}
nav_exclude: true
parent: NodeJS Api
---
`;
const frontMatter = `---
layout: page
title: ${path.basename(fullPath, '.md')}
parent: NodeJS Api
---
`;
if(!content.startsWith('---')) {
if(fullPath.indexOf('interfaces') > -1) {
fs.writeFileSync(fullPath, frontMatterExclude + content.replace(/\.md/g, ".html"));
} else {
fs.writeFileSync(fullPath, frontMatter + content.replace(/\.md/g, ".html"));
}
} else {
fs.writeFileSync(fullPath, content.replace(/\.md/g, ".html"));
}
}
});
}
// Start the process from the root directory
addFrontMatterToFiles(directoryPath);