-
Notifications
You must be signed in to change notification settings - Fork 328
feat: support global search in multiple environments and fix pipeline errors #2629
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,21 +33,25 @@ import css from 'highlight.js/lib/languages/css' | |||||||||||||||||||||||||||||
import html from 'highlight.js/lib/languages/xml' | ||||||||||||||||||||||||||||||
import docsearch from '@docsearch/js' | ||||||||||||||||||||||||||||||
import '@docsearch/css' | ||||||||||||||||||||||||||||||
import { doSearchEverySite } from './tools/docsearch' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const envTarget = import.meta.env.VITE_BUILD_TARGET || 'open' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
hljs.registerLanguage('javascript', javascript) | ||||||||||||||||||||||||||||||
hljs.registerLanguage('css', css) | ||||||||||||||||||||||||||||||
hljs.registerLanguage('html', html) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (envTarget === 'open') { | ||||||||||||||||||||||||||||||
docsearch({ | ||||||||||||||||||||||||||||||
appId: 'AGPA5UXHMH', | ||||||||||||||||||||||||||||||
apiKey: '5fa09fc20270efa61d68e2c2eb0f56df', | ||||||||||||||||||||||||||||||
indexName: 'opentiny', | ||||||||||||||||||||||||||||||
container: '.search-box', | ||||||||||||||||||||||||||||||
debug: false | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
docsearch({ | ||||||||||||||||||||||||||||||
appId: 'AGPA5UXHMH', | ||||||||||||||||||||||||||||||
apiKey: '5fa09fc20270efa61d68e2c2eb0f56df', | ||||||||||||||||||||||||||||||
indexName: 'opentiny', | ||||||||||||||||||||||||||||||
container: '.search-box', | ||||||||||||||||||||||||||||||
debug: false | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
Comment on lines
+44
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Move API credentials to environment variables The Algolia API credentials are exposed in client-side code, which poses a security risk. These should be moved to environment variables. docsearch({
- appId: 'AGPA5UXHMH',
- apiKey: '5fa09fc20270efa61d68e2c2eb0f56df',
+ appId: import.meta.env.VITE_ALGOLIA_APP_ID,
+ apiKey: import.meta.env.VITE_ALGOLIA_API_KEY,
indexName: 'opentiny',
container: '.search-box',
debug: false
}) 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Gitleaks (8.21.2)46-46: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key) |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (envTarget !== 'open') { | ||||||||||||||||||||||||||||||
// 支持本地开发和内网使用全局搜索 | ||||||||||||||||||||||||||||||
doSearchEverySite() | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// 实验后发现,先调用一次预热一下,后续再调用会有速度的提示,因此在main中预热一下。 | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||
const HIT_CLASS = 'DocSearch-Hit' | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const findUrlLink = (target) => { | ||||||||||||||||||||||||||||||||||
if (target?.nodeName?.toLocaleLowerCase?.() === 'a') { | ||||||||||||||||||||||||||||||||||
return target.getAttribute('href') | ||||||||||||||||||||||||||||||||||
} else if (target?.parentElement) { | ||||||||||||||||||||||||||||||||||
return findUrlLink(target.parentElement) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+3
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for recursive DOM traversal The -const findUrlLink = (target) => {
+const findUrlLink = (target, depth = 0) => {
+ const MAX_DEPTH = 10;
+ if (depth > MAX_DEPTH) return null;
if (target?.nodeName?.toLocaleLowerCase?.() === 'a') {
return target.getAttribute('href')
} else if (target?.parentElement) {
- return findUrlLink(target.parentElement)
+ return findUrlLink(target.parentElement, depth + 1)
}
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const isAlgoliaHitDom = (dom) => | ||||||||||||||||||||||||||||||||||
dom?.className?.includes?.(HIT_CLASS) || dom?.parentElement?.className?.includes?.(HIT_CLASS) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
export const doSearchEverySite = () => { | ||||||||||||||||||||||||||||||||||
window.addEventListener('click', (event) => { | ||||||||||||||||||||||||||||||||||
const target = event.target | ||||||||||||||||||||||||||||||||||
if (isAlgoliaHitDom(target)) { | ||||||||||||||||||||||||||||||||||
const openUrl = findUrlLink(target) | ||||||||||||||||||||||||||||||||||
if (openUrl) { | ||||||||||||||||||||||||||||||||||
const urlObj = new URL(openUrl) | ||||||||||||||||||||||||||||||||||
event.preventDefault() | ||||||||||||||||||||||||||||||||||
window.location.href = openUrl.replace(urlObj.origin, window.location.origin) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
} |
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.
💡 Codebase verification
🛠️ Refactor suggestion
The @docsearch/react dependency configuration is incorrect and should be fixed
The codebase is using
@docsearch/js
and@docsearch/css
directly, as evidenced in main.js:The
@docsearch/react
dependency is incorrectly aliased to@docsearch/css
and should be removed since:@docsearch/js
), not the React version@docsearch/css
dependency"@docsearch/js": "^3.8.0", "@docsearch/css": "^3.8.0", - "@docsearch/react": "npm:@docsearch/css"
🔗 Analysis chain
Verify @docsearch/react dependency configuration
The dependency configuration appears unusual:
This might lead to duplicate CSS imports and potential version conflicts.
Consider using the proper @docsearch/react package instead of the CSS alias:
The previous commands didn't provide enough context. Let's search more broadly for docsearch usage and check package configurations to understand the dependency setup better.
Let's examine how docsearch is actually being used in the project to better understand the dependency configuration.
Let's examine how docsearch is actually implemented in the main.js file and if there are any other relevant files to understand the dependency setup better.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 146
Script:
Length of output: 223
Script:
Length of output: 1830
Script:
Length of output: 2682