-
Notifications
You must be signed in to change notification settings - Fork 273
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
feat: support global search shortcut keys to search multiple websites #2637
Conversation
WalkthroughThe pull request includes updates to several files within the Changes
Possibly related PRs
Suggested labels
Poem
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 (
|
[e2e-test-warn] The title of the Pull request should look like "fix(vue-renderless): [action-menu, alert] fix xxx bug". Please make sure you've read our contributing guide |
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 (2)
examples/sites/src/tools/docsearch.js (1)
1-1
: Consider making OPEN_TINY_URL configurable via environment variablesThe hardcoded URL might need to change based on different environments or deployments.
-const OPEN_TINY_URL = 'https://opentiny.design' +const OPEN_TINY_URL = import.meta.env.VITE_OPEN_TINY_URL || 'https://opentiny.design'examples/sites/vite.config.ts (1)
3-3
: Document the purpose of VITE_BUILD_TARGET and inner modePlease add documentation explaining:
- The purpose of the 'inner' build target
- When and why the docsearch plugin should be enabled
- The implications of enabling/disabling this feature
Consider adding a comment block:
/** * VITE_BUILD_TARGET='inner' enables the docsearch plugin which: * 1. Transforms @docsearch library code * 2. Enables global search functionality across multiple sites * 3. <add more details about when to use this mode> */Also applies to: 46-46, 120-121
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
examples/sites/package.json
(1 hunks)examples/sites/src/tools/docsearch.js
(1 hunks)examples/sites/vite-dosearch-plugin.js
(1 hunks)examples/sites/vite.config.ts
(3 hunks)
✅ Files skipped from review due to trivial changes (1)
- examples/sites/package.json
window.handleGlobalSearchData = (resolve) => { | ||
return (data) => { | ||
if (typeof data.content === 'string') { | ||
data.content = data.content.replaceAll(OPEN_TINY_URL, window.location.origin) | ||
} | ||
|
||
resolve(data) | ||
} | ||
}) | ||
} |
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.
Security concern: Exposing function on window object
Exposing functions on the window object could lead to potential security vulnerabilities as it becomes globally accessible. Consider:
- Using a more scoped approach
- Adding input validation
- Implementing proper error handling
- window.handleGlobalSearchData = (resolve) => {
+ const handleGlobalSearchData = (resolve) => {
return (data) => {
+ if (!data || typeof data !== 'object') {
+ console.error('Invalid data format received');
+ return;
+ }
if (typeof data.content === 'string') {
data.content = data.content.replaceAll(OPEN_TINY_URL, window.location.origin)
}
resolve(data)
}
}
+ // Expose through a controlled interface if necessary
+ if (typeof window !== 'undefined') {
+ Object.defineProperty(window, 'handleGlobalSearchData', {
+ value: handleGlobalSearchData,
+ writable: false,
+ configurable: false
+ });
+ }
📝 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.
window.handleGlobalSearchData = (resolve) => { | |
return (data) => { | |
if (typeof data.content === 'string') { | |
data.content = data.content.replaceAll(OPEN_TINY_URL, window.location.origin) | |
} | |
resolve(data) | |
} | |
}) | |
} | |
const handleGlobalSearchData = (resolve) => { | |
return (data) => { | |
if (!data || typeof data !== 'object') { | |
console.error('Invalid data format received'); | |
return; | |
} | |
if (typeof data.content === 'string') { | |
data.content = data.content.replaceAll(OPEN_TINY_URL, window.location.origin) | |
} | |
resolve(data) | |
} | |
} | |
// Expose through a controlled interface if necessary | |
if (typeof window !== 'undefined') { | |
Object.defineProperty(window, 'handleGlobalSearchData', { | |
value: handleGlobalSearchData, | |
writable: false, | |
configurable: false | |
}); | |
} |
const newCode = code.replace( | ||
'Promise((function(t){var n=new XMLHttpRequest;', | ||
'Promise((function(t){t=window.handleGlobalSearchData(t);var n=new XMLHttpRequest;' | ||
) |
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.
Fragile code transformation pattern
The current string replacement approach is brittle and might break with future updates of the @docsearch library. Consider:
- Using AST transformation instead of string replacement
- Adding version checks for @docsearch
- Implementing error handling for failed transformations
- const newCode = code.replace(
- 'Promise((function(t){var n=new XMLHttpRequest;',
- 'Promise((function(t){t=window.handleGlobalSearchData(t);var n=new XMLHttpRequest;'
- )
+ try {
+ const parser = require('@babel/parser');
+ const traverse = require('@babel/traverse').default;
+ const generate = require('@babel/generator').default;
+
+ const ast = parser.parse(code);
+
+ traverse(ast, {
+ NewExpression(path) {
+ if (path.node.callee.name === 'Promise' &&
+ path.parent.type === 'CallExpression') {
+ // Insert the transformation here using AST
+ }
+ }
+ });
+
+ const newCode = generate(ast).code;
+ return {
+ code: newCode,
+ map: null
+ }
+ } catch (error) {
+ console.error('Failed to transform @docsearch code:', error);
+ return null;
+ }
Committable suggestion skipped: line range outside the PR's diff.
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Bug Fixes
@opentiny/vue-docs
project.Refactor