-
Notifications
You must be signed in to change notification settings - Fork 4
/
pagefind.mjs
50 lines (46 loc) · 1.2 KB
/
pagefind.mjs
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
// Astro pagefind integration.
// Combination of shishkin/astro-pagefind (outdated) and withastro/starlight.
import { spawn } from "node:child_process";
import { dirname, relative } from "node:path";
import { fileURLToPath } from "node:url";
import sirv from "sirv";
export default function pagefind() {
/** @type {string} */
let outDir;
/** @type {string} */
let cwd;
/** @type {import('astro').AstroIntegration} */
const integration = {
name: "Pagefind",
hooks: {
"astro:config:setup": ({ config }) => {
const targetDir = fileURLToPath(config.outDir);
cwd = dirname(fileURLToPath(import.meta.url));
outDir = relative(cwd, targetDir);
},
"astro:server:setup": ({ server }) => {
const serve = sirv(outDir, {
dev: true,
etag: true,
});
server.middlewares.use((req, res, next) => {
if (req.url?.startsWith("/pagefind/")) {
serve(req, res, next);
} else {
next();
}
});
},
"astro:build:done": () => {
return new Promise((resolve) => {
spawn("npx", ["-y", "pagefind", "--site", outDir], {
stdio: "inherit",
shell: true,
cwd,
}).on("close", () => resolve());
});
},
},
};
return integration;
}