From c92625510e270b8b161615b06ac9de62ae4112a5 Mon Sep 17 00:00:00 2001 From: gerteck Date: Tue, 14 Jan 2025 19:25:02 +0800 Subject: [PATCH] Add PagefindSearchbar vue components Add custom pagefind searchbar and searchbarResultItem vue components, that make use of pagefind API. Updated plugin to work with vue components Added defaultUI option to use default Pagefind UI Updated indexing logic --- packages/core/src/Site/index.ts | 4 +- packages/core/src/plugins/pageFind.ts | 41 ++- .../vue-components/src/PagefindSearchbar.vue | 264 ++++++++++++++++++ .../src/PagefindSearchbarResultItem.vue | 127 +++++++++ packages/vue-components/src/Searchbar.vue | 5 - packages/vue-components/src/index.js | 2 + 6 files changed, 427 insertions(+), 16 deletions(-) create mode 100644 packages/vue-components/src/PagefindSearchbar.vue create mode 100644 packages/vue-components/src/PagefindSearchbarResultItem.vue diff --git a/packages/core/src/Site/index.ts b/packages/core/src/Site/index.ts index 4d9f0c1b52..ab13e24ddc 100644 --- a/packages/core/src/Site/index.ts +++ b/packages/core/src/Site/index.ts @@ -585,7 +585,9 @@ export class Site { }); const { index } = newIndex; if (index) { - await index.addDirectory({ path: this.outputPath }); + const { errors, page_count } = await index.addDirectory({ path: this.outputPath }); + errors.forEach(error => logger.error(error)); + logger.info(`Pagefind indexed ${page_count} pages`); await index.writeFiles({ outputPath: `${this.outputPath}/pagefind` }); } await close(); diff --git a/packages/core/src/plugins/pageFind.ts b/packages/core/src/plugins/pageFind.ts index 1993c9925b..786ee14293 100644 --- a/packages/core/src/plugins/pageFind.ts +++ b/packages/core/src/plugins/pageFind.ts @@ -1,17 +1,14 @@ -// import cheerio from 'cheerio'; -// import { PluginContext, FrontMatter } from './Plugin'; -// todo: find a way to import the UI and Script from the working directory. -// const DEFAULT_UI = 'https://cdn.jsdelivr.net/npm/@pagefind/default-ui@1.0.4/+esm'; - +// todo: Find way to bypass MB's auto changing of the path +// (find a way to import the UI and Script from the working directory.) const JS_FILE_NAME = 'pageFindAssets/pagefind-ui.min.js'; const CSS_FILE_NAME = 'pageFindAssets/pagefind-ui.min.css'; -const ADDTIONAL_CSS_FILE_NAME = 'pageFindAssets/pagefind-ui-additional.css'; + const PAGEFIND_INPUT_SELECTOR = '#pagefind-search-input'; /** - * Generates the script to initialize the PageFind UI + * Generates the script to initialize the Default Pagefind UI */ -function genScript() { +function initalizeDefaultPagefindUIScript() { return ` `; } +/** + * Attaches the pagefind search API to the window object + */ +function attachPagefindObjectScript() { + return ` + `; +} + export = { tagConfig: { pagefind: { isSpecial: true, }, }, - getScripts: () => [``, genScript()], + getScripts: () => [ + ``, + attachPagefindObjectScript(), + initalizeDefaultPagefindUIScript(), + ], getLinks: () => [ ``, - ``, + // ``, ], }; diff --git a/packages/vue-components/src/PagefindSearchbar.vue b/packages/vue-components/src/PagefindSearchbar.vue new file mode 100644 index 0000000000..f870a2044f --- /dev/null +++ b/packages/vue-components/src/PagefindSearchbar.vue @@ -0,0 +1,264 @@ + + + + + + + diff --git a/packages/vue-components/src/PagefindSearchbarResultItem.vue b/packages/vue-components/src/PagefindSearchbarResultItem.vue new file mode 100644 index 0000000000..248cefbded --- /dev/null +++ b/packages/vue-components/src/PagefindSearchbarResultItem.vue @@ -0,0 +1,127 @@ + + + + + + diff --git a/packages/vue-components/src/Searchbar.vue b/packages/vue-components/src/Searchbar.vue index 2223afddad..54d755277a 100644 --- a/packages/vue-components/src/Searchbar.vue +++ b/packages/vue-components/src/Searchbar.vue @@ -1,7 +1,6 @@