-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgen_search_indexes.js
59 lines (47 loc) · 1.56 KB
/
gen_search_indexes.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
59
import { file as defineFile, Glob, write } from "bun";
import matter from "gray-matter";
import { stripHtml } from "string-strip-html";
import { createConsola } from "consola";
import RemoveMarkdown from "remove-markdown";
const log = createConsola({
formatOptions: {
date: true,
},
});
// Requires Bun to be installed
// Sorry!
const posts = [];
const fileGlob = new Glob("./**/+page.svx");
const matchingFiles = fileGlob.scan("./src/routes");
// read all routes
for await (const file of matchingFiles) {
// ignore the error page
if (file.includes("+error.svx")) {
log.warn("Skipping error page");
continue;
}
log.info("Transforming", file);
const rawContent = await defineFile(`./src/routes/${file}`).text();
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
const tags = frontmatter.data.tags || "";
posts.push({
title: frontmatter.data.title || "MissingNo.",
content: strippedMarkdown,
description: frontmatter.data.description || null,
url: "/" + filePath,
tags: tags
.split(",")
.map((el) => el.trim())
.filter(String),
});
}
// write to file
log.start("Writing to file...");
await write("./src/routes/search.json/meta.json", JSON.stringify(posts));
log.success("Done!");