-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathindex-docs.ts
More file actions
55 lines (46 loc) · 1.37 KB
/
Copy pathindex-docs.ts
File metadata and controls
55 lines (46 loc) · 1.37 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
import fs from "node:fs";
import path from "node:path";
import GithubSlugger from "github-slugger";
import { COMPONENTS_PATH } from "./constants";
const slugger = new GithubSlugger();
const SEARCH_INDEX_PATH = "./src/SEARCH_INDEX.json";
const H2_DELMIMITER = "## ";
type SearchDocument = {
id: string;
text: string;
description: string;
href: string;
isComponent: boolean;
};
const files = fs.readdirSync(COMPONENTS_PATH);
const documents: SearchDocument[] = [];
for (const file of files) {
slugger.reset();
const [componentName] = file.split(".");
if (!componentName) continue;
const filePath = path.join(COMPONENTS_PATH, file);
const fileContent = fs.readFileSync(filePath, "utf8");
const lines = fileContent.split("\n");
documents.push({
id: `${componentName}-page`,
text: componentName,
description: componentName,
href: `/components/${componentName}`,
isComponent: true,
});
for (const line of lines) {
if (line.startsWith(H2_DELMIMITER)) {
const [, h2] = line.split(H2_DELMIMITER);
if (!h2) continue;
const hash = slugger.slug(h2);
documents.push({
id: componentName + hash,
text: componentName,
description: h2,
href: `/components/${componentName}#${hash}`,
isComponent: false,
});
}
}
}
fs.writeFileSync(SEARCH_INDEX_PATH, JSON.stringify(documents, null, 2));