-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpagefind.ts
More file actions
95 lines (90 loc) · 3.13 KB
/
pagefind.ts
File metadata and controls
95 lines (90 loc) · 3.13 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import type { AstroIntegration } from "astro";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { createIndex, type PagefindServiceConfig } from "pagefind";
import sirv from "sirv";
/**
* Pagefind Astro integration options.
*/
export interface PagefindOptions {
/**
* `PagefindServiceConfig` passed to pagefind's `createIndex`
*/
indexConfig?: PagefindServiceConfig;
}
export default function pagefind({ indexConfig }: PagefindOptions = {}): AstroIntegration {
let outDir: string;
return {
name: "pagefind",
hooks: {
"astro:config:setup": ({ config, logger }) => {
if (config.output === "server") {
logger.warn(
"Output type `server` does not produce static *.html pages in its output and thus will not work with astro-pagefind integration.",
);
return;
}
if (config.adapter?.name.startsWith("@astrojs/vercel")) {
outDir = fileURLToPath(new URL(".vercel/output/static/", config.root));
} else if (config.adapter?.name === "@astrojs/cloudflare") {
outDir = fileURLToPath(new URL(config.base?.replace(/^\//, ""), config.outDir));
} else if (config.adapter?.name === "@astrojs/node") {
outDir = fileURLToPath(config.build.client);
} else {
outDir = fileURLToPath(config.outDir);
}
},
"astro:server:setup": ({ server, logger }) => {
if (!outDir) {
logger.warn(
"astro-pagefind couldn't reliably determine the output directory. Search assets will not be served.",
);
return;
}
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": async ({ logger }) => {
if (!outDir) {
logger.warn(
"astro-pagefind couldn't reliably determine the output directory. Search index will not be built.",
);
return;
}
const { index, errors: createErrors } = await createIndex(indexConfig);
if (!index) {
logger.error("Pagefind failed to create index");
createErrors.forEach((e) => logger.error(e));
return;
}
const { page_count, errors: addErrors } = await index.addDirectory({ path: outDir });
if (addErrors.length) {
logger.error("Pagefind failed to index files");
addErrors.forEach((e) => logger.error(e));
return;
} else {
logger.info(`Pagefind indexed ${page_count} pages`);
}
const { outputPath, errors: writeErrors } = await index.writeFiles({
outputPath: path.join(outDir, "pagefind"),
});
if (writeErrors.length) {
logger.error("Pagefind failed to write index");
writeErrors.forEach((e) => logger.error(e));
return;
} else {
logger.info(`Pagefind wrote index to ${outputPath}`);
}
},
},
};
}