Skip to content

Commit eda31a8

Browse files
committed
Enhance build-docs.js to extract headers from markdown files and include them in metadata
1 parent 3ab838a commit eda31a8

File tree

2 files changed

+189
-128
lines changed

2 files changed

+189
-128
lines changed

build-docs.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
//get all *.md files in ./docs, reccursively, and store them in an array, with their properties in the first block, they're from obsidian
21
const fs = require('fs');
32
const path = require('path');
43

4+
function extractHeaders(content) {
5+
const headerRegex = /^(#{1,6})\s+(.+)$/gm;
6+
const headers = [];
7+
let match;
8+
9+
while ((match = headerRegex.exec(content)) !== null) {
10+
// Don't include h1 headers as they're typically the title
11+
if (match[1].length > 1) {
12+
headers.push(match[2].trim());
13+
}
14+
}
15+
16+
return headers;
17+
}
18+
519
function parseFrontMatter(content) {
620
const lines = content.trim().split('\n');
721
let metadata = {};
@@ -20,9 +34,15 @@ function parseFrontMatter(content) {
2034
}
2135
}
2236

37+
const contentText = lines.slice(contentStart).join('\n').trim();
38+
const headers = extractHeaders(contentText);
39+
if (headers.length > 0) {
40+
metadata.headers = headers;
41+
}
42+
2343
return {
2444
metadata,
25-
content: lines.slice(contentStart).join('\n').trim()
45+
content: contentText
2646
};
2747
}
2848

@@ -124,7 +144,6 @@ function loadIndexTemplate() {
124144
const indexPath = path.join(__dirname, 'index.json');
125145
try {
126146
const indexData = JSON.parse(fs.readFileSync(indexPath, 'utf-8'));
127-
// Strip out documents array, keeping all other properties
128147
delete indexData.documents;
129148
return indexData;
130149
} catch (error) {
@@ -156,12 +175,10 @@ function combineIndexes(dir) {
156175
const indexData = loadIndexTemplate();
157176
const documents = readAllMarkdownFiles(dir);
158177

159-
// Add the documents array with new content
160178
indexData.documents = documents;
161179
writeIndexFile(indexData);
162180
}
163181

164-
// Replace console.log with combineIndexes call
165182
combineIndexes(path.join(__dirname, 'docs'));
166183

167184
module.exports = readAllMarkdownFiles;

0 commit comments

Comments
 (0)