-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Search improvements #4393
Search improvements #4393
Conversation
…r, if there is one. * Fix image paths in search * Bump pagefind version
✅ Deploy Preview for esphome ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
WalkthroughThe changes in this pull request involve updates to the Changes
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
Makefile (1)
3-3
: Consider documenting the version bump reason.It would be helpful to add a comment explaining why this specific version was chosen and what improvements it brings to the search functionality.
Example:
+# Updated to 1.1.1 for improved search excerpt handling PAGEFIND_VERSION=1.1.1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- Makefile (1 hunks)
- _templates/searchbox.html (5 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
Makefile (1)
Pattern
**
: - Do not generate or add any sequence diagrams_templates/searchbox.html (1)
Pattern
**
: - Do not generate or add any sequence diagrams
🔇 Additional comments (5)
Makefile (1)
3-3
: LGTM: Safe version bump of pagefind.The update from 1.1.0 to 1.1.1 is a patch version change, indicating bug fixes or minor improvements that align with the PR's search enhancement objectives.
_templates/searchbox.html (4)
63-63
: LGTM: Improved variable declarationGood improvement using
const
for the immutablemobileWidth
value.
86-111
: LGTM: Improved variable declarations and image handlingGood improvements:
- Using
const
for immutable references- Proper handling of image paths
- Appropriate use of optional chaining
- Clean integration with the new
getLink
function
118-120
: LGTM: Appropriate variable declaration choicesGood choice of
let
for position variables that are reassigned andconst
for the immutablemaxWidth
.
187-187
: LGTM: Improved click event handlingGood improvement using
composedPath()
for more reliable event handling, especially with shadow DOM.
_templates/searchbox.html
Outdated
function getLink(location, anchors, url) { | ||
anchor = anchors.filter(a => a.location === location)[0]; | ||
if (anchor) { | ||
return url + "#" + anchor.id; | ||
} | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix potential issues in getLink function
Several improvements needed:
- The
anchor
variable is implicitly declared - Missing null check for the
anchors
parameter - The filter callback could be simplified
Apply these improvements:
function getLink(location, anchors, url) {
- anchor = anchors.filter(a => a.location === location)[0];
- if (anchor) {
- return url + "#" + anchor.id;
- }
- return null;
+ if (!anchors?.length) return null;
+ const anchor = anchors.find(a => a.location === location);
+ return anchor ? `${url}#${anchor.id}` : null;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function getLink(location, anchors, url) { | |
anchor = anchors.filter(a => a.location === location)[0]; | |
if (anchor) { | |
return url + "#" + anchor.id; | |
} | |
return null; | |
} | |
function getLink(location, anchors, url) { | |
if (!anchors?.length) return null; | |
const anchor = anchors.find(a => a.location === location); | |
return anchor ? `${url}#${anchor.id}` : null; | |
} |
_templates/searchbox.html
Outdated
input_field = document.querySelector("#pfmod-input-0"); | ||
if (input_field) { | ||
input_field.focus({preventScroll: true}); | ||
input_field.addEventListener("blur", () => { | ||
setTimeout(() => { | ||
input_field.focus({preventScroll: true}); | ||
}, 100); | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve input field focus handling
Several concerns with the current implementation:
input_field
is implicitly declared global- The setTimeout delay is arbitrary
- Could create an infinite focus loop if user intentionally wants to blur
Consider this improved implementation:
- input_field = document.querySelector("#pfmod-input-0");
- if (input_field) {
- input_field.focus({preventScroll: true});
- input_field.addEventListener("blur", () => {
- setTimeout(() => {
- input_field.focus({preventScroll: true});
- }, 100);
- })
- }
+ const inputField = document.querySelector("#pfmod-input-0");
+ if (inputField) {
+ inputField.focus({preventScroll: true});
+ let shouldRefocus = true;
+
+ // Allow escape key to disable auto-focus
+ inputField.addEventListener("keydown", (e) => {
+ if (e.key === "Escape") {
+ shouldRefocus = false;
+ }
+ });
+
+ inputField.addEventListener("blur", () => {
+ if (shouldRefocus) {
+ // Use requestAnimationFrame for better timing
+ requestAnimationFrame(() => {
+ inputField.focus({preventScroll: true});
+ });
+ }
+ });
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
input_field = document.querySelector("#pfmod-input-0"); | |
if (input_field) { | |
input_field.focus({preventScroll: true}); | |
input_field.addEventListener("blur", () => { | |
setTimeout(() => { | |
input_field.focus({preventScroll: true}); | |
}, 100); | |
}) | |
} | |
const inputField = document.querySelector("#pfmod-input-0"); | |
if (inputField) { | |
inputField.focus({preventScroll: true}); | |
let shouldRefocus = true; | |
// Allow escape key to disable auto-focus | |
inputField.addEventListener("keydown", (e) => { | |
if (e.key === "Escape") { | |
shouldRefocus = false; | |
} | |
}); | |
inputField.addEventListener("blur", () => { | |
if (shouldRefocus) { | |
// Use requestAnimationFrame for better timing | |
requestAnimationFrame(() => { | |
inputField.focus({preventScroll: true}); | |
}); | |
} | |
}); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- _templates/searchbox.html (5 hunks)
- pagefind.yml (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
_templates/searchbox.html (1)
Pattern
**
: - Do not generate or add any sequence diagramspagefind.yml (1)
Pattern
**
: - Do not generate or add any sequence diagrams
🔇 Additional comments (5)
pagefind.yml (2)
8-8
: LGTM! The YAML syntax and indentation are correct.The addition of "pre" to exclude_selectors follows the existing pattern and is properly formatted.
8-8
: Verify the impact on code example searchability.Adding "pre" to exclude_selectors will prevent code blocks from being indexed in the search. While this might reduce noise in search results, it could also make it harder for users to find specific code examples in the documentation.
Please confirm if this is the intended behavior:
- Are users expected to search for code examples?
- Have you tested how this affects the search experience?
Let's check the current usage of
<pre>
tags in the documentation:_templates/searchbox.html (3)
63-63
: LGTM! Good use of const for immutable value.The change from var to const for mobileWidth improves code safety by preventing accidental reassignment.
77-86
: LGTM! Robust anchor link handling implementation.The implementation correctly:
- Handles null/empty anchors
- Finds the closest anchor at or before the target location
- Constructs proper URL with anchor
121-123
: LGTM! Appropriate use of let/const declarations.Good choice of:
let
for position variables that need reassignmentconst
for maxWidth that remains constant
let image = result?.meta?.image; | ||
if (image) { | ||
if (image.includes("/_images/")) | ||
image = image.substring(image.indexOf("/_images/")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhance image path handling robustness.
The current image path manipulation could be more robust:
- The substring operation assumes "/_images/" exists
- No validation of the image path format
Consider this safer approach:
- let image = result?.meta?.image;
- if (image) {
- if (image.includes("/_images/"))
- image = image.substring(image.indexOf("/_images/"));
+ let image = result?.meta?.image;
+ if (image) {
+ const imagePathIndex = image.indexOf("/_images/");
+ if (imagePathIndex !== -1) {
+ image = image.substring(imagePathIndex);
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
let image = result?.meta?.image; | |
if (image) { | |
if (image.includes("/_images/")) | |
image = image.substring(image.indexOf("/_images/")); | |
let image = result?.meta?.image; | |
if (image) { | |
const imagePathIndex = image.indexOf("/_images/"); | |
if (imagePathIndex !== -1) { | |
image = image.substring(imagePathIndex); | |
} |
const input_field = document.getElementById("pfmod-input-0"); | ||
if (input_field) { | ||
input_field.focus({preventScroll: true}); | ||
input_field.addEventListener("blur", () => { | ||
requestAnimationFrame(() => { | ||
input_field.focus({preventScroll: true}); | ||
}); | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve focus management accessibility.
While using requestAnimationFrame
is better than setTimeout
, the current implementation could be improved for better accessibility and user experience:
- Users can't intentionally move focus away
- Could interfere with keyboard navigation
Consider this more accessible approach:
const input_field = document.getElementById("pfmod-input-0");
if (input_field) {
input_field.focus({preventScroll: true});
+ let shouldMaintainFocus = true;
+
+ // Allow users to opt-out of focus lock
+ input_field.addEventListener("keydown", (e) => {
+ if (e.key === "Escape" || e.key === "Tab") {
+ shouldMaintainFocus = false;
+ }
+ });
+
input_field.addEventListener("blur", () => {
- requestAnimationFrame(() => {
- input_field.focus({preventScroll: true});
- });
+ if (shouldMaintainFocus) {
+ requestAnimationFrame(() => {
+ input_field.focus({preventScroll: true});
+ });
+ }
})
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const input_field = document.getElementById("pfmod-input-0"); | |
if (input_field) { | |
input_field.focus({preventScroll: true}); | |
input_field.addEventListener("blur", () => { | |
requestAnimationFrame(() => { | |
input_field.focus({preventScroll: true}); | |
}); | |
}) | |
} | |
const input_field = document.getElementById("pfmod-input-0"); | |
if (input_field) { | |
input_field.focus({preventScroll: true}); | |
let shouldMaintainFocus = true; | |
// Allow users to opt-out of focus lock | |
input_field.addEventListener("keydown", (e) => { | |
if (e.key === "Escape" || e.key === "Tab") { | |
shouldMaintainFocus = false; | |
} | |
}); | |
input_field.addEventListener("blur", () => { | |
if (shouldMaintainFocus) { | |
requestAnimationFrame(() => { | |
input_field.focus({preventScroll: true}); | |
}); | |
} | |
}) | |
} |
Description:
Related issue (if applicable): fixes
Pull request in esphome with YAML changes (if applicable): esphome/esphome#
Checklist:
I am merging into
next
because this is new documentation that has a matching pull-request in esphome as linked above.or
I am merging into
current
because this is a fix, change and/or adjustment in the current documentation and is not for a new component or feature.Link added in
/index.rst
when creating new documents for new components or cookbook.