-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen_search_indexes_node.js
50 lines (39 loc) · 1.31 KB
/
gen_search_indexes_node.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
import { createConsola } from "consola";
import fg from "fast-glob";
import matter from "gray-matter";
import { readFile, writeFile } from "node:fs/promises";
import { stripHtml } from "string-strip-html";
import RemoveMarkdown from "remove-markdown";
const log = createConsola({
formatOptions: {
date: true,
},
});
const posts = [];
const matchingFiles = fg.stream("**/+page.svx", { dot: true });
// read all routes
for await (const file of matchingFiles) {
// ignore the error page
if (file.includes("+error.svx")) {
log.warn("Skipping error page");
continue;
}
const rawContent = await readFile(file);
log.info("Transforming", file);
const frontmatter = matter(rawContent); // parse markdown front matter
const filePath = file.slice(0, -9).slice(2); // remove the file name and extension and leading slash
// add to posts
const contentNoHtml = stripHtml(frontmatter.content).result;
const strippedMarkdown = RemoveMarkdown(contentNoHtml)
.replace(/:::.*/, "")
.replace(/:::/, "") // remove admonitions
posts.push({
title: frontmatter.data.title || "MissingNo.",
content: strippedMarkdown,
url: "/" + filePath,
});
}
// write to file
log.start("Writing to file...");
await writeFile("./src/routes/search.json/meta.json", JSON.stringify(posts));
log.success("Done!");