File tree Expand file tree Collapse file tree 3 files changed +41
-10
lines changed
Expand file tree Collapse file tree 3 files changed +41
-10
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " @opentiny/vue-docs" ,
3- "version" : " 3.20.3 " ,
3+ "version" : " 3.20.5 " ,
44 "license" : " MIT" ,
55 "scripts" : {
66 "start" : " vite" ,
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:~" ,
Original file line number Diff line number Diff line change @@ -33,21 +33,25 @@ import css from 'highlight.js/lib/languages/css'
3333import html from 'highlight.js/lib/languages/xml'
3434import docsearch from '@docsearch/js'
3535import '@docsearch/css'
36+ import { doSearchEverySite } from './tools/docsearch'
3637
3738const envTarget = import . meta. env . VITE_BUILD_TARGET || 'open'
3839
3940hljs . registerLanguage ( 'javascript' , javascript )
4041hljs . registerLanguage ( 'css' , css )
4142hljs . 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中预热一下。
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments