Skip to content
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

Merged
merged 2 commits into from
Oct 28, 2024
Merged

Search improvements #4393

merged 2 commits into from
Oct 28, 2024

Conversation

clydebarrow
Copy link
Contributor

Description:

  • In search, clicking on excerpt text goes to the corresponding anchor, if there is one.
  • Fix image paths in search
  • Bump pagefind version

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.

…r, if there is one.

* Fix image paths in search
* Bump pagefind version
Copy link

netlify bot commented Oct 28, 2024

Deploy Preview for esphome ready!

Name Link
🔨 Latest commit c3c204d
🔍 Latest deploy log https://app.netlify.com/sites/esphome/deploys/671efdf8920bba0008287bb0
😎 Deploy Preview https://deploy-preview-4393--esphome.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Walkthrough

The changes in this pull request involve updates to the Makefile, _templates/searchbox.html, and pagefind.yml. The Makefile has been modified to update the version of the PAGEFIND tool from 1.1.0 to 1.1.1, affecting the download URL for the pagefind binary. In _templates/searchbox.html, a new function getLink is introduced, and several variable declarations are updated to improve scoping and immutability. Additionally, modifications are made to enhance the handling of click events and maintain focus on the input field. The pagefind.yml file now includes an exclusion selector for the page indexing process.

Changes

File Change Summary
Makefile Updated PAGEFIND_VERSION from 1.1.0 to 1.1.1. Affects the download URL for the pagefind binary.
_templates/searchbox.html Added function getLink(location, anchors, url). Updated variable declarations for mobileWidth, leftPos, topPos, maxWidth, wrapper, thumb, inner, title, and excerpt to use const or let. Added input_field variable. Modified clickCallback function for event handling and added focus maintenance on input field.
pagefind.yml Added exclusion selector "pre" to exclude_selectors in the configuration file.

Suggested reviewers

  • jesserockz
  • nagyrobi

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Files that changed from the base of the PR and between bac5e2a and 2156491.

📒 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 declaration

Good improvement using const for the immutable mobileWidth value.


86-111: LGTM: Improved variable declarations and image handling

Good 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 choices

Good choice of let for position variables that are reassigned and const for the immutable maxWidth.


187-187: LGTM: Improved click event handling

Good improvement using composedPath() for more reliable event handling, especially with shadow DOM.

Comment on lines 77 to 83
function getLink(location, anchors, url) {
anchor = anchors.filter(a => a.location === location)[0];
if (anchor) {
return url + "#" + anchor.id;
}
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential issues in getLink function

Several improvements needed:

  1. The anchor variable is implicitly declared
  2. Missing null check for the anchors parameter
  3. 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.

Suggested change
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;
}

Comment on lines 200 to 208
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);
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve input field focus handling

Several concerns with the current implementation:

  1. input_field is implicitly declared global
  2. The setTimeout delay is arbitrary
  3. 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.

Suggested change
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});
});
}
});
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Files that changed from the base of the PR and between 2156491 and c3c204d.

📒 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 diagrams

pagefind.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:

  1. Are users expected to search for code examples?
  2. 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 reassignment
  • const for maxWidth that remains constant

Comment on lines +92 to +95
let image = result?.meta?.image;
if (image) {
if (image.includes("/_images/"))
image = image.substring(image.indexOf("/_images/"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Enhance image path handling robustness.

The current image path manipulation could be more robust:

  1. The substring operation assumes "/_images/" exists
  2. 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.

Suggested change
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);
}

Comment on lines +203 to +211
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});
});
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve focus management accessibility.

While using requestAnimationFrame is better than setTimeout, the current implementation could be improved for better accessibility and user experience:

  1. Users can't intentionally move focus away
  2. 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.

Suggested change
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});
});
}
})
}

@nagyrobi nagyrobi enabled auto-merge (squash) October 28, 2024 03:33
@nagyrobi nagyrobi merged commit 7ce07b9 into esphome:current Oct 28, 2024
3 checks passed
@clydebarrow clydebarrow deleted the pagefind branch October 28, 2024 05:40
@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants