From ddfbc90d612fba468045473ece6cdeb0746cb72e Mon Sep 17 00:00:00 2001 From: hankertrix <91734413+hankertrix@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:41:43 +0800 Subject: [PATCH] Add support for the pagefind highlight script 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. --- README.md | 10 ++++++++-- src/internal/config.js | 5 ++++- src/plugins/pagefind-build.js | 8 ++++++-- src/plugins/pagefind-dev.js | 7 +++++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c6b9f51..777b4ef 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. diff --git a/src/internal/config.js b/src/internal/config.js index 458cd56..b0771f3 100644 --- a/src/internal/config.js +++ b/src/internal/config.js @@ -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"), }), {}, @@ -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; } diff --git a/src/plugins/pagefind-build.js b/src/plugins/pagefind-build.js index 287f735..a04cfd5 100644 --- a/src/plugins/pagefind-build.js +++ b/src/plugins/pagefind-build.js @@ -1,3 +1,4 @@ +import { get_pagefind_config } from "../internal/config.js"; import { PACKAGE_NAME } from "../internal/constants.js"; /** @@ -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`], }, }, }; diff --git a/src/plugins/pagefind-dev.js b/src/plugins/pagefind-dev.js index d0cab50..05be026 100644 --- a/src/plugins/pagefind-dev.js +++ b/src/plugins/pagefind-dev.js @@ -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`], }, }, };