-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #425 from Jothsa/highlight-script
Highlight Support
- Loading branch information
Showing
16 changed files
with
1,127 additions
and
572 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: "Highlight API Config" | ||
nav_title: "Highlight API Config" | ||
nav_section: References | ||
weight: 65 | ||
|
||
--- | ||
|
||
`PagefindHighlight` accepts an object with any of the following options: | ||
|
||
### markContext | ||
|
||
```js | ||
new PagefindHighlight({ markContext: "[data-pagefind-body]" }) | ||
``` | ||
|
||
The area in which to highlight text. Defaults to `[data-pagefind-body]` if any `[data-pagefind-body]` elements can be found, otherwise `document.body`. This can be a CSS selector string, a DOM element, an array of DOM elements, or a NodeList. | ||
|
||
### pagefindQueryParamName | ||
|
||
```js | ||
new PagefindHighlight({ pagefindQueryParamName: "pagefind-highlight" }) | ||
``` | ||
|
||
The name of the query parameter that Pagefind uses to determine which terms to highlight. Defaults to `pagefind-highlight`. If the name is changed here, it *must* be changed in the [`PagefindUI` object](/docs/ui/#highlight-query-param-name) as well. | ||
|
||
### markOptions | ||
|
||
An object with the same shape as the [Mark.js options](https://markjs.io/#mark) object, except that the `separateWordSearch` option is not supported. | ||
|
||
This option defaults to: | ||
|
||
```js | ||
{ | ||
className: "pagefind-highlight", | ||
exclude: ["[data-pagefind-ignore]", "[data-pagefind-ignore] *"], | ||
} | ||
``` | ||
|
||
If either `className` or exclude are not specified they will default to the above. To disable the default `exclude` behavior, pass an empty array to `exclude`. To not add a class to the highlight elements, pass an empty string to `className`. | ||
|
||
### addStyles | ||
|
||
This is a boolean that determines whether or not Pagefind will add minimal styles to the highlighted elements. If set to `false`, Pagefind will not add any styles to the page. This option defaults to `true`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: "Highlighting search terms" | ||
nav_title: "Highlighting search terms" | ||
nav_section: Searching | ||
weight: 10 | ||
--- | ||
|
||
Pagefind allows you to highlight search terms on the result page. To enable this feature, import `/pagefind/pagefind-highlight.js` on the result page and create a new `PagefindHighlight` object. | ||
|
||
```html | ||
<script src="/pagefind/pagefind-highlight.js"></script> | ||
<script> | ||
new PagefindHighlight(); | ||
<script> | ||
``` | ||
To see the options available to PagefindHighlight, see [Highlight Config](/docs/highlight-config). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
pagefind/features/highlighting/highlighting_base.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
Feature: Highlighting Tests | ||
|
||
Background: | ||
Given I have the environment variables: | ||
| PAGEFIND_SITE | public | | ||
Given I have a "public/words/index.html" file with the body: | ||
""" | ||
<p id="has-highlight">Is this highlighted? It should be!</p> | ||
<p data-pagefind-ignore>This should not be highlighted</p> | ||
<p data-pagefind-ignore><span>This</span> should not be highlighted</p> | ||
<script type="module"> | ||
await import('/pagefind/pagefind-highlight.js'); | ||
new PagefindHighlight(); | ||
</script> | ||
""" | ||
Given I have a "public/single-body/index.html" file with the body: | ||
""" | ||
<main data-pagefind-body> | ||
<p id="has-highlight">This should be highlighted</p> | ||
<p data-pagefind-ignore>This should not be highlighted</p> | ||
</main> | ||
<p id="no-highlight">This should not be highlighted</p> | ||
<script type="module"> | ||
await import('/pagefind/pagefind-highlight.js'); | ||
new PagefindHighlight(); | ||
</script> | ||
""" | ||
Given I have a "public/multiple-bodies/index.html" file with the body: | ||
""" | ||
<main data-pagefind-body> | ||
<p id="has-highlight">This should be highlighted</p> | ||
<p data-pagefind-ignore>This should not be highlighted</p> | ||
</main> | ||
<p id="no-highlight">This should not be highlighted</p> | ||
<div data-pagefind-body> | ||
<p id="has-highlight">This should be highlighted</p> | ||
<p data-pagefind-ignore>This should not be highlighted</p> | ||
</div> | ||
<script type="module"> | ||
await import('/pagefind/pagefind-highlight.js'); | ||
new PagefindHighlight(); | ||
</script> | ||
""" | ||
Given I have a "public/options/index.html" file with the body: | ||
""" | ||
<main data-pagefind-body> | ||
<p id="has-highlight">This should be highlighted</p> | ||
<p data-pagefind-ignore>This should not be highlighted</p> | ||
</main> | ||
<p id="no-highlight">This should not be highlighted</p> | ||
<div data-pagefind-body> | ||
<p id="has-highlight">This should be highlighted</p> | ||
<p class="ignore">This should not be highlighted</p> | ||
<p data-pagefind-ignore>This should not be highlighted</p> | ||
</div> | ||
<script type="module"> | ||
await import('/pagefind/pagefind-highlight.js'); | ||
new PagefindHighlight({ | ||
pagefindQueryParamName: 'custom-name', | ||
markOptions: { | ||
className: 'custom-class', | ||
exclude: [ | ||
"[data-pagefind-ignore]", | ||
"[data-pagefind-ignore] *", | ||
".ignore" | ||
] | ||
} | ||
}); | ||
</script> | ||
""" | ||
When I run my program | ||
Then I should see "Running Pagefind" in stdout | ||
When I serve the "public" directory | ||
|
||
Scenario: Highlight script is loaded | ||
When I load "/words/" | ||
Then I should see the file "public/pagefind/pagefind-highlight.js" | ||
Then There should be no logs | ||
|
||
Scenario: Highlight script marks correctly | ||
When I load "/words/?pagefind-highlight=this" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark" should contain "this" | ||
Then The selector "#has-highlight mark.pagefind-highlight" should contain "this" | ||
Then The selector "p[data-pagefind-ignore]:not(:has(span))" should contain "This should not be highlighted" | ||
Then The selector "p[data-pagefind-ignore]:has(span)" should contain "<span>This</span> should not be highlighted" | ||
When I load "/words/?pagefind-highlight=this&pagefind-highlight=should" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark:first-of-type" should contain "this" | ||
Then The selector "#has-highlight mark:nth-of-type(2)" should contain "should" | ||
When I load "/words/?pagefind-highlight=is+this" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark" should contain "Is this" | ||
Then The selector "p[data-pagefind-ignore]" should contain "This should not be highlighted" | ||
When I load "/words/?pagefind-highlight=highlighted%3F" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark" should contain "highlighted?" | ||
When I load "/words/?pagefind-highlight=this+highlighted%3F" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark:first-of-type" should contain "this highlighted?" | ||
|
||
Scenario: Highlight script stays within pagefind-body | ||
When I load "/single-body/?pagefind-highlight=this" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark" should contain "This" | ||
Then The selector "p[data-pagefind-ignore]" should contain "This should not be highlighted" | ||
Then The selector "#no-highlight" should contain "This should not be highlighted" | ||
When I load "/multiple-bodies/?pagefind-highlight=this" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark" should contain "This" | ||
Then The selector "p[data-pagefind-ignore]" should contain "This should not be highlighted" | ||
Then The selector "#no-highlight" should contain "This should not be highlighted" | ||
|
||
Scenario: Highlight script options work | ||
When I load "/options/?custom-name=this" | ||
Then There should be no logs | ||
Then The selector "#has-highlight mark" should contain "This" | ||
Then The selector "#has-highlight mark.custom-class" should contain "This" | ||
Then The selector "p[data-pagefind-ignore]" should contain "This should not be highlighted" | ||
Then The selector "p.ignore" should contain "This should not be highlighted" | ||
Then The selector "#no-highlight" should contain "This should not be highlighted" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
Feature: Base UI Tests | ||
Background: | ||
Given I have the environment variables: | ||
| PAGEFIND_SITE | public | | ||
|
||
# in this senario I use the css attribute selector to make sure the link has the query param as the end | ||
# if the link doesn't exist, the check will fail | ||
# see https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors#syntax:~:text=%5Battr%24%3Dvalue%5D,by%20value. | ||
|
||
Scenario: Pagefind UI adds highlight query params | ||
Given I have a "public/index.html" file with the body: | ||
""" | ||
<div id="search"></div> | ||
<script src="/pagefind/pagefind-ui.js"></script> | ||
<script> | ||
window.pui = new PagefindUI({ element: "#search" }); | ||
</script> | ||
""" | ||
Given I have a "public/cat/index.html" file with the body: | ||
""" | ||
<h1>hello world</h1> | ||
<p>Hello world! How are you</p> | ||
""" | ||
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: | ||
""" | ||
<div id="search"></div> | ||
<script src="/pagefind/pagefind-ui.js"></script> | ||
<script> | ||
window.pui = new PagefindUI({ element: "#search", highlightQueryParamName: null }); | ||
</script> | ||
""" | ||
Given I have a "public/cat/index.html" file with the body: | ||
""" | ||
<h1>hello world</h1> | ||
<p>Hello world! How are you</p> | ||
""" | ||
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: | ||
""" | ||
<div id="search"></div> | ||
<script src="/pagefind/pagefind-ui.js"></script> | ||
<script> | ||
window.pui = new PagefindUI({ element: "#search", highlightQueryParamName: 'custom-param' }); | ||
</script> | ||
""" | ||
Given I have a "public/cat/index.html" file with the body: | ||
""" | ||
<h1>hello world</h1> | ||
<p>Hello world! How are you</p> | ||
""" | ||
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.