Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the pagefind highlight script #54

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/proud-peaches-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"vite-plugin-pagefind": patch
---

Add support for the pagefind highlight script,
as well as a new configuration option to support
having the pagefind bundle placed in other places
other than the default `/pagefind/pagefind.js`.
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_dir

The URL directory you use to import the pagefind script on your site. For example, if you use `/pagefind/pagefind.js`, the `pagefind_dir` is `pagefind`. If you use `/search/static/pagefind/pagefind.js`, the `pagefind_dir` 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.
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@
"url": "https://github.com/Hugos68/vite-plugin-pagefind"
},
"homepage": "https://github.com/Hugos68/vite-plugin-pagefind#vite-plugin-pagefind",
"keywords": [
"vite",
"vite-plugin",
"pagefind"
],
"keywords": ["vite", "vite-plugin", "pagefind"],
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"files": ["dist"],
"main": "./dist/plugins/pagefind.js",
"exports": {
".": {
Expand Down
9 changes: 8 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_dir: v.optional(v.string(), "pagefind"),
dev_strategy: v.optional(v.picklist(["eager", "lazy"]), "lazy"),
}),
{},
Expand All @@ -25,5 +26,11 @@ 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);
const pagefind_dir = config.vite_plugin_pagefind.pagefind_dir;
config.vite_plugin_pagefind.pagefind_dir = pagefind_dir.replace(
/^\/+|\/+$/g,
"",
);
return config;
}
11 changes: 9 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,17 @@ 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_dir = pagefind_config.vite_plugin_pagefind.pagefind_dir;
return {
build: {
rollupOptions: {
external: "/pagefind/pagefind.js",
external: [
`/${pagefind_dir}/pagefind.js`,
`/${pagefind_dir}/pagefind-highlight.js`,
],
},
},
};
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/pagefind-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ export default function dev() {
apply: "serve",
enforce: "post",
async config() {
const cwd = process.cwd();
const pagefind_config = await get_pagefind_config(cwd);
const pagefind_dir = pagefind_config.vite_plugin_pagefind.pagefind_dir;
return {
assetsInclude: "**/pagefind.js",
assetsInclude: ["**/pagefind.js", "**/pagefind-highlight.js"],
build: {
rollupOptions: {
external: "/pagefind/pagefind.js",
external: [
`/${pagefind_dir}/pagefind.js`,
`/${pagefind_dir}/pagefind-highlight.js`,
],
},
},
};
Expand Down