Skip to content

Commit

Permalink
pagefind: new ui.globalVariable option
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 21, 2024
1 parent f47ec6d commit 41ba341
Show file tree
Hide file tree
Showing 4 changed files with 3,098 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
- `google_fonts`: Added `subset` option to filter character ranges. [#692]
- `prism` plugin: Added `cssFile` and `placeholder` option to themes.
- `code_highlight` plugin: Added `cssFile` and `placeholder` option to themes.
- `pagefind` plugin: New `ui.globalVariable` option.

### Changed
- Files with extension `.d.ts` are ignored by default [#707].
Expand Down
16 changes: 12 additions & 4 deletions plugins/pagefind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ export interface UIOptions {
* Configure the query parameter name.
*/
highlightParam?: string;

/**
* The global variable name to use for the Pagefind UI instance.
* This is useful if you need to interact with the UI programmatically.
*/
globalVariable?: string;
}

export interface Options {
Expand Down Expand Up @@ -196,7 +202,7 @@ export function pagefind(userOptions?: Options) {
});

if (options.ui) {
const { containerId, ...ui } = options.ui;
const { containerId, globalVariable, ...ui } = options.ui;

site.process([".html"], (pages) => {
for (const page of pages) {
Expand Down Expand Up @@ -249,12 +255,14 @@ export function pagefind(userOptions?: Options) {
? ui.processResult.toString()
: undefined,
};
let code = `new PagefindUI(${JSON.stringify(uiSettings)});`;
if (globalVariable) {
code = `window["${globalVariable}"] = ${code}`;
}
const init = document.createElement("script");
init.setAttribute("type", "text/javascript");
init.innerHTML =
`window.addEventListener('DOMContentLoaded',()=>{new PagefindUI(${
JSON.stringify(uiSettings)
});});`;
`window.addEventListener('DOMContentLoaded',()=>{${code}});`;
document.head.append(init);

if (ui.highlightParam) {
Expand Down
Loading

0 comments on commit 41ba341

Please sign in to comment.