This should be highlighted
+This should not be highlighted
+diff --git a/docs/content/docs/_index.md b/docs/content/docs/_index.md
index 274c81fc..027c0c46 100644
--- a/docs/content/docs/_index.md
+++ b/docs/content/docs/_index.md
@@ -53,6 +53,19 @@ We can see that a bunch of content was indexed, and Pagefind will be running a p
Loading this in your browser, you should see a search input on your page. Try searching for some content and you will see results appear from your site.
+## Highlighting
+
+To highlight the search terms on results page, add the following snippet on every page that has been indexed
+
+```html
+
+
+
+ """
+ Given I have a "public/single-body/index.html" file with the body:
+ """
+ This should be highlighted This should not be highlighted
This should not be highlighted
+ + """ + Given I have a "public/multiple-bodies/index.html" file with the body: + """ +This should be highlighted
+This should not be highlighted
+This should not be highlighted
+This should be highlighted
+This should not be highlighted
+This should be highlighted
+This should not be highlighted
+This should not be highlighted
+This should be highlighted
+This should not be highlighted
+This should not be highlighted
+Hello world! How are you
+ """ + When I run my program + Then I should see "Running Pagefind" in stdout + Then I should see the file "public/pagefind/pagefind.js" + When I serve the "public" directory + When I load "/" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("world"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='?pagefind-highlight=world']" should contain "hello world" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("hello world"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='?pagefind-highlight=hello&pagefind-highlight=world']" should contain "hello world" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("hello world!"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='?pagefind-highlight=hello&pagefind-highlight=world%21']" should contain "hello world" + + Scenario: Pagefind UI does not add highlight query params + Given I have a "public/index.html" file with the body: + """ + + + + + """ + Given I have a "public/cat/index.html" file with the body: + """ +Hello world! How are you
+ """ + When I run my program + Then I should see "Running Pagefind" in stdout + Then I should see the file "public/pagefind/pagefind.js" + When I serve the "public" directory + When I load "/" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("world"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='/']" should contain "hello world" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("hello world"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='/']" should contain "hello world" + + Scenario: Pagefind UI uses custom highlight query param name + Given I have a "public/index.html" file with the body: + """ + + + + + """ + Given I have a "public/cat/index.html" file with the body: + """ +Hello world! How are you
+ """ + When I run my program + Then I should see "Running Pagefind" in stdout + Then I should see the file "public/pagefind/pagefind.js" + When I serve the "public" directory + When I load "/" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("world"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='?custom-param=world']" should contain "hello world" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("hello world"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='?custom-param=hello&custom-param=world']" should contain "hello world" + When I evaluate: + """ + async function() { + window.pui.triggerSearch("hello world!"); + await new Promise(r => setTimeout(r, 1500)); // TODO: await el in humane + } + """ + Then There should be no logs + Then The selector ".pagefind-ui__result-link[href$='?custom-param=hello&custom-param=world%21']" should contain "hello world" diff --git a/pagefind/features/ui/ui_hooks.feature b/pagefind/features/ui/ui_hooks.feature index f0451a05..3c645c4a 100644 --- a/pagefind/features/ui/ui_hooks.feature +++ b/pagefind/features/ui/ui_hooks.feature @@ -14,6 +14,7 @@ Feature: UI Hooks window.pui = new PagefindUI({ element: "#search", processTerm: (t) => t.replace("word", "search"), + highlightQueryParamName: null }); """ @@ -31,7 +32,7 @@ Feature: UI Hooks // TODO: Add more web test steps to humane instead of throwing js let el = document.querySelector(".pagefind-ui__result-link"); if (el.getAttribute("href") !== "/") { - throw new Error("Search term should have been normalized by processTerm"); + throw new Error(`Search term should have been normalized by processTerm. href: ${el.getAttribute("href")}`); } } """ diff --git a/pagefind/src/output/mod.rs b/pagefind/src/output/mod.rs index c2003f9f..9ab7e37e 100644 --- a/pagefind/src/output/mod.rs +++ b/pagefind/src/output/mod.rs @@ -62,6 +62,12 @@ const SEARCH_JS: &str = include_str!(concat!( env!("CARGO_PKG_VERSION"), ".js" )); +const HIGHLIGHT_JS: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/vendor/pagefind_highlight.", + env!("CARGO_PKG_VERSION"), + ".js" +)); pub struct LanguageMeta { pub page_count: usize, @@ -125,6 +131,12 @@ async fn write_common( Compress::None, write_behavior, ), + write( + outdir.join("pagefind-highlight.js"), + vec![HIGHLIGHT_JS], + Compress::None, + write_behavior, + ), write( outdir.join("pagefind-ui.js"), vec![WEB_UI_JS], diff --git a/pagefind_ui/default/svelte/result.svelte b/pagefind_ui/default/svelte/result.svelte index e1fca739..86639611 100644 --- a/pagefind_ui/default/svelte/result.svelte +++ b/pagefind_ui/default/svelte/result.svelte @@ -1,162 +1,155 @@{@html data.excerpt}
- {#if meta.length} - - {/if} -- {placeholder(30)} -
-- {placeholder(40)} -
-{@html data.excerpt}
+ {#if meta.length} + + {/if} ++ {placeholder(30)} +
++ {placeholder(40)} +
+- {data.meta?.title}{@html data.meta?.title}
{#if has_root_sub_result} @@ -70,8 +77,11 @@ {#each non_root_sub_results as subres}- {subres.title}{@html subres.title}
{@html subres.excerpt}
@@ -82,7 +92,7 @@ diff --git a/pagefind_ui/default/svelte/ui.svelte b/pagefind_ui/default/svelte/ui.svelte index 3946be2e..ff285dfa 100644 --- a/pagefind_ui/default/svelte/ui.svelte +++ b/pagefind_ui/default/svelte/ui.svelte @@ -1,452 +1,477 @@