Skip to content

Commit

Permalink
Merge pull request #406 from CloudCannon/feat/sub-results
Browse files Browse the repository at this point in the history
Sub results / anchor links
  • Loading branch information
bglw authored Sep 5, 2023
2 parents f33bb65 + 0a06adf commit 315054b
Show file tree
Hide file tree
Showing 15 changed files with 605 additions and 647 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ public
.hugo_build.lock
_gen
pagefind_ui/default/_dev_files/_pagefind/ui.*
pagefind_ui/default/_dev_files/_pagefind/pagefind.js
pagefind_ui/default/_dev_files/pagefind/ui.*
pagefind_ui/default/_dev_files/pagefind/pagefind.js
pagefind_ui/default/css
pagefind_ui/modular/_dev_files/_pagefind/modular.*
pagefind_ui/modular/_dev_files/_pagefind/pagefind.js
pagefind_ui/modular/_dev_files/pagefind/modular.*
pagefind_ui/modular/_dev_files/pagefind/pagefind.js

npm_dist

Expand All @@ -29,4 +33,4 @@ pkg/
vendor

# Node
node_modules
node_modules
2 changes: 1 addition & 1 deletion docs/layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ <h1>{{ .Params.title }}</h1>

<script>
window.addEventListener('DOMContentLoaded', (event) => {
new PagefindUI({ element: "#search" });
new PagefindUI({ element: "#search", showSubResults: true });
});
</script>

Expand Down
24 changes: 24 additions & 0 deletions pagefind/features/anchors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Feature: Anchors
<p>Some text nested under the divs</p>
</div>
"""
Given I have a "public/repr/index.html" file with the body:
"""
<div data-pagefind-body>
<h1 id="repr-heading">My <span data-pagefind-ignore>Redacted</span> Heading about Symbiosis</h1>
</div>
"""
When I run my program
Then I should see "Running Pagefind" in stdout
When I serve the "public" directory
Expand Down Expand Up @@ -149,3 +155,21 @@ Feature: Anchors
Then There should be no logs
Then The selector "[data-search]>ul>li:nth-of-type(1)" should contain "/dog/#h1: PageTwo, from Pagefind / 'text nested under the h1. Words in spans should be <mark>extracted.</mark> Some text nested under the p with spans.'"
Then The selector "[data-search]>ul>li:nth-of-type(2)" should contain "/dog/#h2_hrefs: Links should be extracted / 'the h2. Text that is bold or italic should be <mark>extracted</mark> Some text nested under the span. Text containing nested IDs should <mark>extract</mark> both. Some text nested under the p'"

Scenario: Pagefind respects data-pagefind-ignore inside anchors
When I evaluate:
"""
async function() {
let pagefind = await import("/pagefind/pagefind.js");
let search = await pagefind.search("symbiosis");
let searchdata = await search.results[0].data();
document.querySelector('[data-search]').innerHTML = `
<ul>
${searchdata.sub_results.map(r => `<li>${r.url}: ${r.title} / '${r.excerpt}'</li>`).join('')}
</ul>
`;
}
"""
Then There should be no logs
Then The selector "[data-search]>ul>li:nth-of-type(1)" should contain "/repr/#repr-heading: My Heading about Symbiosis / 'My Heading about <mark>Symbiosis.</mark>'"
4 changes: 3 additions & 1 deletion pagefind/src/fossick/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,9 @@ impl<'a> DomParser<'a> {
let element_text = el.as_str();
node.current_value.push_str(element_text);

if node.anchor_ids.is_some() {
let node_is_ignored = node.status == NodeStatus::Ignored || node.status == NodeStatus::Excluded;

if !node_is_ignored && node.anchor_ids.is_some() {
let anchor_ids = node.anchor_ids.clone().unwrap();
drop(node);
for anchor_id in anchor_ids {
Expand Down
231 changes: 0 additions & 231 deletions pagefind_ui/default/_dev_files/_pagefind/pagefind.js

This file was deleted.

9 changes: 7 additions & 2 deletions pagefind_ui/default/_dev_files/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
</head>
<body style="max-width: 1000px; padding: 80px; margin: 0 auto;">
<h1>Hello World from the local dev suite</h1>
<p>Searches for words ending in <kbd>y</kbd> will return no results</p>
<details>
<summary>Shortcuts</summary>
<p>Searches for words ending in <kbd>y</kbd> will return no results</p>
<p>Searches containing <kbd>r</kbd> will have a sub-result for the page itself</p>
<p>Searches containing <kbd>d</kbd> will have sub-results for anchors</p>
</details>
<div id="search"></div>
<script src="/pagefind/ui.js"></script>
<script>
new PagefindUI({ element: "#search" });
new PagefindUI({ element: "#search", showSubResults: true });
</script>
</body>
</html>
Loading

0 comments on commit 315054b

Please sign in to comment.