Skip to content

Commit

Permalink
Add support for the pagefind highlight script
Browse files Browse the repository at this point in the history
Also added the ability to customise the URL that pagefind is being
imported from, instead of relying on the default import of
/pagefind/pagefind.js.

Updated the documentation to reflect this additional configuration
option.
  • Loading branch information
hankertrix committed Nov 20, 2024
1 parent 408922c commit da683d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ The command to build and index the project.

_default_: 'npm run build'

### pagefind_url

The URL you use to import the pagefind script on your site. For example, if you use `/pagefind/pagefind.js`, the `pagefind_url` is `pagefind`. If you use `/search/static/pagefind/pagefind.js`, the `pagefind_url` is `search/static/pagefind`

_default_: 'pagefind'

### dev_strategy

The indexing strategy used during development:
Expand All @@ -98,7 +104,7 @@ const pagefind: Pagefind = await import("/pagefind/pagefind.js");

## Pagefind

For further questions regarding Pagefind itself you can refer to [the offical docs](https://pagefind.app/).
For further questions regarding Pagefind itself you can refer to [the official docs](https://pagefind.app/).

## License

Expand Down
5 changes: 4 additions & 1 deletion src/internal/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const PagefindConfigSchema = v.object({
v.object({
assets_dir: v.optional(v.string(), "public"),
build_command: v.optional(v.string(), "npm run build"),
pagefind_url: v.optional(v.string(), "pagefind"),
dev_strategy: v.optional(v.picklist(["eager", "lazy"]), "lazy"),
}),
{},
Expand All @@ -25,5 +26,7 @@ export async function get_pagefind_config(cwd) {
"utf-8",
);
const pagefind_parsed = JSON.parse(pagefind_raw);
return v.parse(PagefindConfigSchema, pagefind_parsed);
const config = v.parse(PagefindConfigSchema, pagefind_parsed);
config.pagefind_url = config.pagefind_url.replace(/^\/+|\/+$/g, "");
return config;
}
8 changes: 6 additions & 2 deletions src/plugins/pagefind-build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { get_pagefind_config } from "../internal/config.js";
import { PACKAGE_NAME } from "../internal/constants.js";

/**
Expand All @@ -8,11 +9,14 @@ export default function build() {
return {
name: `${PACKAGE_NAME}-build`,
apply: "build",
config() {
async config() {
const cwd = process.cwd();
const pagefind_config = await get_pagefind_config(cwd);
const pagefind_url = pagefind_config.pagefind_url;
return {
build: {
rollupOptions: {
external: "/pagefind/pagefind.js",
external: [`/${pagefind_url}/pagefind.js`, `/${pagefind_url}/pagefind-highlight.js`],
},
},
};
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/pagefind-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ export default function dev() {
apply: "serve",
enforce: "post",
async config() {
const cwd = process.cwd();
const pagefind_config = await get_pagefind_config(cwd);
const pagefind_url = pagefind_config.pagefind_url;
return {
assetsInclude: "**/pagefind.js",
assetsInclude: ["**/pagefind.js", "**/pagefind-highlight.js"],
build: {
rollupOptions: {
external: "/pagefind/pagefind.js",
external: [`/${pagefind_url}/pagefind.js`, `/${pagefind_url}/pagefind-highlight.js`],
},
},
};
Expand Down

0 comments on commit da683d5

Please sign in to comment.