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 and fixed typos in the README.
  • Loading branch information
hankertrix committed Nov 20, 2024
1 parent 408922c commit ddfbc90
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
10 changes: 8 additions & 2 deletions 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,8 +104,8 @@ 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

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details.
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, "");

Check failure on line 30 in src/internal/config.js

View workflow job for this annotation

GitHub Actions / build

Property 'pagefind_url' does not exist on type '{ vite_plugin_pagefind: { assets_dir: string; build_command: string; pagefind_url: string; dev_strategy: "eager" | "lazy"; }; site: string; }'.

Check failure on line 30 in src/internal/config.js

View workflow job for this annotation

GitHub Actions / build

Property 'pagefind_url' does not exist on type '{ vite_plugin_pagefind: { assets_dir: string; build_command: string; pagefind_url: string; dev_strategy: "eager" | "lazy"; }; site: string; }'.
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;

Check failure on line 15 in src/plugins/pagefind-build.js

View workflow job for this annotation

GitHub Actions / build

Property 'pagefind_url' does not exist on type '{ vite_plugin_pagefind: { assets_dir: string; build_command: string; pagefind_url: string; dev_strategy: "eager" | "lazy"; }; site: string; }'.
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;

Check failure on line 23 in src/plugins/pagefind-dev.js

View workflow job for this annotation

GitHub Actions / build

Property 'pagefind_url' does not exist on type '{ vite_plugin_pagefind: { assets_dir: string; build_command: string; pagefind_url: string; dev_strategy: "eager" | "lazy"; }; site: string; }'.
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 ddfbc90

Please sign in to comment.