-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagefind.js
More file actions
68 lines (54 loc) · 1.86 KB
/
pagefind.js
File metadata and controls
68 lines (54 loc) · 1.86 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
56
57
58
59
60
61
62
63
64
65
66
67
68
import path from "path";
import fs from "fs";
import frontmatter from "front-matter";
import * as pagefind from "pagefind";
const { index } = await pagefind.createIndex();
const recurse = async (dir) => {
const files = await fs.promises.readdir(dir);
for (const file of files) {
if (file.startsWith(".")) continue;
const stat = await fs.promises.stat(path.join(dir, file));
if (stat.isDirectory()) {
await recurse(path.join(dir, file));
continue;
}
const content = frontmatter(
fs.readFileSync(path.join(dir, file), "utf8"),
);
const { errors } = await index.addCustomRecord({
url: path
.join(dir, file)
.replace("src/content/docs/", "/docs/")
.replace(".mdx", "")
.replace(".md", ""),
meta: {
title: content.attributes.title,
description: content.attributes.description,
published: content.attributes.date,
},
content: content.body
.replace(/#|`/g, "")
.replace(/[<>]/g, " ")
.replace(/\[(.+?)\]\(.+?\)/g, "$1")
.replace(/\/\/ \[!.+?\]/g, ""), // Remove mdx syntax
language: "en",
});
if (errors.length) {
console.error(errors);
}
}
};
await recurse("src/content/docs");
const { errors: writeErrors } = await index.writeFiles({
outputPath: ".vercel/output/static/pagefind",
});
console.log(writeErrors.length ? writeErrors : "Index written to disk");
if (
process.env.npm_lifecycle_event === "dev" ||
process.argv.includes("--dev")
) {
const { errors } = await index.writeFiles({
outputPath: "public/pagefind",
});
console.log(errors.length ? errors : "Dev index written to disk");
}