Skip to content

Commit b8a8991

Browse files
Refactor/pagefind search (#2879)
* Add pagefind search result utility files * Remvove pagefind-ui.css & load pagefind.js dynamically No longer a need for pagefind-ui.css as we will be referencing our own custom CSS instead. Likewise, we no longern eed PagefindUI's js. Dynamically imports pagefind by lazy loading it each time the search opens. * Fix import path in page.njk * Replace PagefindUI with pagefind.js API in Search.vue Removed deep() overrides with custom css Update keyboard navigation using selectedIndex state Remove MutationObserver and innerHTML hacky solution Implemented custom result rendering * Update styling for Search.vue * Update pagefind search utils to process subresult status Included an additional field subResult fields in FormattedSearchResult to detect whether the result if a subResult and if it is the last subResult. Updated Search.vue results to indicate if a result if a main result or a search result with svgs. Svgs were taken from algolia's design on the cs2103 website. * Fix bug first index sub result and Clean search.css The occured as the first sub result would be essentially the same as the main result. This is because our page names and their first heading are usually the same contributing to this error. For example, makingTheSiteSearchable.html is the same as makingTheSiteSearchable.html#making-the-site-searchable. Hence, by checking of the sub result's title is the same as the main result's title, we can identify which sub result to not include when displaying the data. Also search.css by removing accidentally duplicated code and restructuring the order of the class selectors in a more logical manner. Also included more comments explaining and breaking down the classes into different sections for easier understandability. Word lol * Update styling for mark & font sizes * Fix mark component not appearing in excerpt bug Some of the marked components were not appearing as they were being cut off within the elipsis due to the length of the excerpt displayed. Hence, instead, it now processes the excerpt length to display a maximum of 15 characters before it to ensure that the marked components are always included. * Add nowrap for result titles styling This was mainly discovered because I found a bug where it seems that pagefind is indexing the headers generated within generated <question></question>. As a result, since these titles are extremely long, it ended up completely hiding the excerpt part of the search-result-item. Hence, this warrants a change by first ensuring that nowrap on the title to ensure that it only displays the first part of the header. And also, I need to raise a issue for the bug as I don't think we should be letting pagefind index these generated headers. * Add merging of consecutive marked terms This will ensure that they are displayed as one segment instead of 2 distinct segements when the search terms are next to each other within the excerpt. * Fix bug storing only duplicated sub-results not unique Previous it was adding duplicated sub-results instead of unqiue ones. E.g.when searching "refactoring" in 2103 site, it was would store 10 (the count limt) copies of the "What" subresult instead of the other sub-sections (How, When, etc). This happened because it was iterating through each weighted location (i.e. the search term position). And for each, it would select the sub-result with the most context, after which it would push the it into the sub array without checking for duplicates. Hence, using the 2103 site example from before, there were >=10 instances of "Refactoring" in "What" section, it was interating through these instances until it reached the 10 count limit. After which, the filters to remove the duplicated entries would result in only mention of the "What" section being displayed, which meant that it would only display "Refactoring" with 1 sub result -> "What" section. Hence, to fix this, I added a duplicate check before pushing subs array. Now we will only store the FIRST instance of each sub-result. Hence "What" is stored once, and it will allow the correct storing of the other sections as well. This ensures that we can display the actual maximum of 10 sub-results. * Add more DocSearch styling * Use score instead of weight when sorting * Create testcases for searchUtils * Rehaul tests for Search.vue * Update functional tests * Update functional test (again) * Export as types & used numeric check * Resolve Search.vue code quality issues * Create pagefind-lazloader.js & update functional tests * Fix assets manager bug * Update functional tests * Fix weight order issue
1 parent cb0edbc commit b8a8991

136 files changed

Lines changed: 1951 additions & 678 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ packages/core/src/lib/markdown-it/highlight/*.js
2222
packages/core/src/lib/markdown-it/plugins/**/*.js
2323
packages/core/src/lib/markdown-it/utils/*.js
2424
packages/core/src/Page/*.js
25+
packages/core/src/Pagefind/*.js
2526
packages/core/src/patches/**/*.js
2627
packages/core/src/plugins/**/*.js
2728
packages/core/src/Site/*.js

.eslintrc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
/* eslint quotes: ["error", "double"] */
33

44
module.exports = {
5-
"ignorePatterns": ["docs/_site/**", "**/dist/**", "**/node_modules/**"],
5+
"ignorePatterns": [
6+
"docs/_site/**",
7+
"**/dist/**", "**/node_modules/**",
8+
"packages/core-web/asset/js/pagefind-lazyloader.js",
9+
],
610
"env": {
711
"node": true,
812
"es6": true,

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ packages/core/src/lib/markdown-it/highlight/*.js
9898
packages/core/src/lib/markdown-it/plugins/**/*.js
9999
packages/core/src/lib/markdown-it/utils/*.js
100100
packages/core/src/Page/*.js
101+
packages/core/src/Pagefind/*.js
101102
packages/core/src/plugins/**/*.js
102103
packages/core/src/Site/*.js
103104
packages/core/src/utils/*.js

packages/cli/test/functional/test_site/expected/bugs/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<link rel="stylesheet" href="/test_site/markbind/glyphicons/css/bootstrap-glyphicons.min.css">
1717
<link rel="stylesheet" href="/test_site/markbind/css/codeblock-dark.min.css">
1818
<link rel="stylesheet" href="/test_site/markbind/css/markbind.min.css">
19-
<link rel="stylesheet" href="/test_site/markbind/pagefind/pagefind-ui.css">
2019
<link rel="stylesheet" href="/test_site/plugins/testMarkbindPlugin/testMarkbindPluginStylesheet.css">
2120
<link rel="stylesheet" href="/test_site/plugins/web3Form/web-3-form.css">
2221
<link rel="stylesheet" href="/test_site/plugins/markbind-plugin-anchors/markbind-plugin-anchors.css">
@@ -360,6 +359,6 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
360359
});
361360

362361
</script>
363-
<script src="/test_site/markbind/pagefind/pagefind-ui.js"></script>
362+
<script src="/test_site/markbind/js/pagefind-lazyloader.js"></script>
364363

365364
</html>

packages/cli/test/functional/test_site/expected/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<link rel="stylesheet" href="/test_site/markbind/glyphicons/css/bootstrap-glyphicons.min.css">
1717
<link rel="stylesheet" href="/test_site/markbind/css/codeblock-dark.min.css">
1818
<link rel="stylesheet" href="/test_site/markbind/css/markbind.min.css">
19-
<link rel="stylesheet" href="/test_site/markbind/pagefind/pagefind-ui.css">
2019
<link rel="stylesheet" href="/test_site/plugins/testMarkbindPlugin/testMarkbindPluginStylesheet.css">
2120
<link rel="stylesheet" href="/test_site/plugins/web3Form/web-3-form.css">
2221
<link rel="stylesheet" href="/test_site/plugins/markbind-plugin-anchors/markbind-plugin-anchors.css">
@@ -1026,6 +1025,6 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
10261025
});
10271026

10281027
</script>
1029-
<script src="/test_site/markbind/pagefind/pagefind-ui.js"></script>
1028+
<script src="/test_site/markbind/js/pagefind-lazyloader.js"></script>
10301029

10311030
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Lazy loader for Pagefind search functionality.
3+
* This script dynamically imports pagefind.js only when needed (when user opens search).
4+
* This avoids loading the search library on initial page load.
5+
*/
6+
window.__pagefind__ = null;
7+
8+
window.loadPagefind = async () => {
9+
if (!window.__pagefind__) {
10+
// baseUrl is defined in page.njk script tag
11+
const baseUrl = window.baseUrl || '';
12+
const module = await import(`${baseUrl}/markbind/pagefind/pagefind.js`);
13+
window.__pagefind__ = module;
14+
module.init();
15+
}
16+
return window.__pagefind__;
17+
};

packages/cli/test/functional/test_site/expected/sub_site/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<link rel="stylesheet" href="/test_site/markbind/glyphicons/css/bootstrap-glyphicons.min.css">
1616
<link rel="stylesheet" href="/test_site/markbind/css/codeblock-dark.min.css">
1717
<link rel="stylesheet" href="/test_site/markbind/css/markbind.min.css">
18-
<link rel="stylesheet" href="/test_site/markbind/pagefind/pagefind-ui.css">
1918
<link rel="stylesheet" href="/test_site/plugins/testMarkbindPlugin/testMarkbindPluginStylesheet.css">
2019
<link rel="stylesheet" href="/test_site/plugins/web3Form/web-3-form.css">
2120
<link rel="stylesheet" href="/test_site/plugins/markbind-plugin-anchors/markbind-plugin-anchors.css">
@@ -367,6 +366,6 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
367366
});
368367

369368
</script>
370-
<script src="/test_site/markbind/pagefind/pagefind-ui.js"></script>
369+
<script src="/test_site/markbind/js/pagefind-lazyloader.js"></script>
371370

372371
</html>

packages/cli/test/functional/test_site/expected/sub_site/nested_sub_site/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<link rel="stylesheet" href="/test_site/markbind/glyphicons/css/bootstrap-glyphicons.min.css">
1616
<link rel="stylesheet" href="/test_site/markbind/css/codeblock-dark.min.css">
1717
<link rel="stylesheet" href="/test_site/markbind/css/markbind.min.css">
18-
<link rel="stylesheet" href="/test_site/markbind/pagefind/pagefind-ui.css">
1918
<link rel="stylesheet" href="/test_site/plugins/testMarkbindPlugin/testMarkbindPluginStylesheet.css">
2019
<link rel="stylesheet" href="/test_site/plugins/web3Form/web-3-form.css">
2120
<link rel="stylesheet" href="/test_site/plugins/markbind-plugin-anchors/markbind-plugin-anchors.css">
@@ -358,6 +357,6 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
358357
});
359358

360359
</script>
361-
<script src="/test_site/markbind/pagefind/pagefind-ui.js"></script>
360+
<script src="/test_site/markbind/js/pagefind-lazyloader.js"></script>
362361

363362
</html>

packages/cli/test/functional/test_site/expected/sub_site/nested_sub_site/testNunjucksPathResolving.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<link rel="stylesheet" href="/test_site/markbind/glyphicons/css/bootstrap-glyphicons.min.css">
1717
<link rel="stylesheet" href="/test_site/markbind/css/codeblock-dark.min.css">
1818
<link rel="stylesheet" href="/test_site/markbind/css/markbind.min.css">
19-
<link rel="stylesheet" href="/test_site/markbind/pagefind/pagefind-ui.css">
2019
<link rel="stylesheet" href="/test_site/plugins/testMarkbindPlugin/testMarkbindPluginStylesheet.css">
2120
<link rel="stylesheet" href="/test_site/plugins/web3Form/web-3-form.css">
2221
<link rel="stylesheet" href="/test_site/plugins/markbind-plugin-anchors/markbind-plugin-anchors.css">
@@ -364,6 +363,6 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
364363
});
365364

366365
</script>
367-
<script src="/test_site/markbind/pagefind/pagefind-ui.js"></script>
366+
<script src="/test_site/markbind/js/pagefind-lazyloader.js"></script>
368367

369368
</html>

packages/cli/test/functional/test_site/expected/sub_site/testNunjucksPathResolving.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<link rel="stylesheet" href="/test_site/markbind/glyphicons/css/bootstrap-glyphicons.min.css">
1717
<link rel="stylesheet" href="/test_site/markbind/css/codeblock-dark.min.css">
1818
<link rel="stylesheet" href="/test_site/markbind/css/markbind.min.css">
19-
<link rel="stylesheet" href="/test_site/markbind/pagefind/pagefind-ui.css">
2019
<link rel="stylesheet" href="/test_site/plugins/testMarkbindPlugin/testMarkbindPluginStylesheet.css">
2120
<link rel="stylesheet" href="/test_site/plugins/web3Form/web-3-form.css">
2221
<link rel="stylesheet" href="/test_site/plugins/markbind-plugin-anchors/markbind-plugin-anchors.css">
@@ -364,6 +363,6 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
364363
});
365364

366365
</script>
367-
<script src="/test_site/markbind/pagefind/pagefind-ui.js"></script>
366+
<script src="/test_site/markbind/js/pagefind-lazyloader.js"></script>
368367

369368
</html>

0 commit comments

Comments
 (0)