Skip to content

Commit ba839e0

Browse files
authored
feat: support global search in multiple environments and fix pipeline errors (#2629)
* feat: support global search in multiple environments and fix pipeline errors * feat: support global search in multiple environments and fix pipeline errors
1 parent baaa366 commit ba839e0

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

examples/sites/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opentiny/vue-docs",
3-
"version": "3.20.3",
3+
"version": "3.20.5",
44
"license": "MIT",
55
"scripts": {
66
"start": "vite",
@@ -49,7 +49,8 @@
4949
"vue-i18n": "^9.1.10",
5050
"vue-router": "4.1.5",
5151
"@docsearch/js": "^3.8.0",
52-
"@docsearch/css": "^3.8.0"
52+
"@docsearch/css": "^3.8.0",
53+
"@docsearch/react": "npm:@docsearch/css"
5354
},
5455
"devDependencies": {
5556
"@opentiny-internal/unplugin-virtual-template": "workspace:~",

examples/sites/src/main.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,25 @@ import css from 'highlight.js/lib/languages/css'
3333
import html from 'highlight.js/lib/languages/xml'
3434
import docsearch from '@docsearch/js'
3535
import '@docsearch/css'
36+
import { doSearchEverySite } from './tools/docsearch'
3637

3738
const envTarget = import.meta.env.VITE_BUILD_TARGET || 'open'
3839

3940
hljs.registerLanguage('javascript', javascript)
4041
hljs.registerLanguage('css', css)
4142
hljs.registerLanguage('html', html)
4243

43-
if (envTarget === 'open') {
44-
docsearch({
45-
appId: 'AGPA5UXHMH',
46-
apiKey: '5fa09fc20270efa61d68e2c2eb0f56df',
47-
indexName: 'opentiny',
48-
container: '.search-box',
49-
debug: false
50-
})
44+
docsearch({
45+
appId: 'AGPA5UXHMH',
46+
apiKey: '5fa09fc20270efa61d68e2c2eb0f56df',
47+
indexName: 'opentiny',
48+
container: '.search-box',
49+
debug: false
50+
})
51+
52+
if (envTarget !== 'open') {
53+
// 支持本地开发和内网使用全局搜索
54+
doSearchEverySite()
5155
}
5256

5357
// 实验后发现,先调用一次预热一下,后续再调用会有速度的提示,因此在main中预热一下。

examples/sites/src/tools/docsearch.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const HIT_CLASS = 'DocSearch-Hit'
2+
3+
const findUrlLink = (target) => {
4+
if (target?.nodeName?.toLocaleLowerCase?.() === 'a') {
5+
return target.getAttribute('href')
6+
} else if (target?.parentElement) {
7+
return findUrlLink(target.parentElement)
8+
}
9+
}
10+
11+
const isAlgoliaHitDom = (dom) =>
12+
dom?.className?.includes?.(HIT_CLASS) || dom?.parentElement?.className?.includes?.(HIT_CLASS)
13+
14+
export const doSearchEverySite = () => {
15+
window.addEventListener('click', (event) => {
16+
const target = event.target
17+
if (isAlgoliaHitDom(target)) {
18+
const openUrl = findUrlLink(target)
19+
if (openUrl) {
20+
const urlObj = new URL(openUrl)
21+
event.preventDefault()
22+
window.location.href = openUrl.replace(urlObj.origin, window.location.origin)
23+
}
24+
}
25+
})
26+
}

0 commit comments

Comments
 (0)