diff --git a/examples/sites/package.json b/examples/sites/package.json index 2512878f2b..3acd5705be 100644 --- a/examples/sites/package.json +++ b/examples/sites/package.json @@ -1,6 +1,6 @@ { "name": "@opentiny/vue-docs", - "version": "3.20.5", + "version": "3.20.7", "license": "MIT", "scripts": { "start": "vite", diff --git a/examples/sites/src/tools/docsearch.js b/examples/sites/src/tools/docsearch.js index d08bb4715e..eded441790 100644 --- a/examples/sites/src/tools/docsearch.js +++ b/examples/sites/src/tools/docsearch.js @@ -1,26 +1,13 @@ -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) - } -} - -const isAlgoliaHitDom = (dom) => - dom?.className?.includes?.(HIT_CLASS) || dom?.parentElement?.className?.includes?.(HIT_CLASS) +const OPEN_TINY_URL = 'https://opentiny.design' 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) + window.handleGlobalSearchData = (resolve) => { + return (data) => { + if (typeof data.content === 'string') { + data.content = data.content.replaceAll(OPEN_TINY_URL, window.location.origin) } + + resolve(data) } - }) + } } diff --git a/examples/sites/vite-dosearch-plugin.js b/examples/sites/vite-dosearch-plugin.js new file mode 100644 index 0000000000..6bc2fe5df4 --- /dev/null +++ b/examples/sites/vite-dosearch-plugin.js @@ -0,0 +1,18 @@ +export default function viteDosearchPlugin() { + return { + name: 'vite-dosearch-plugin', + enforce: 'pre', + transform(code, id) { + if (id.includes('@docsearch')) { + const newCode = code.replace( + 'Promise((function(t){var n=new XMLHttpRequest;', + 'Promise((function(t){t=window.handleGlobalSearchData(t);var n=new XMLHttpRequest;' + ) + return { + code: newCode, + map: null + } + } + } + } +} diff --git a/examples/sites/vite.config.ts b/examples/sites/vite.config.ts index 469fb28e06..92e5210165 100644 --- a/examples/sites/vite.config.ts +++ b/examples/sites/vite.config.ts @@ -1,5 +1,6 @@ import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' +import viteDosearchPlugin from './vite-dosearch-plugin' import Unocss from 'unocss/vite' import path from 'node:path' import UnoCssConfig from './uno.config' @@ -42,6 +43,7 @@ export default defineConfig((config) => { const env = loadEnv(config.mode, process.cwd() + '/env', '') const isSaas = env.VITE_TINY_THEME === 'saas' const isPlus = env.VITE_APP_MODE === 'plus' + const isInner = env.VITE_BUILD_TARGET === 'inner' const demosPath = isPlus ? '../plusdocs/pc' : `./demos/${env.VITE_APP_MODE}` const apisPath = isPlus ? '../plusdocs/apis' : './demos/apis' const menuPath = isSaas ? path.resolve('./demos/saas') : path.resolve(demosPath) @@ -115,7 +117,8 @@ export default defineConfig((config) => { viteStaticCopy({ targets: copyTarget }), - delStatic() + delStatic(), + isInner ? viteDosearchPlugin() : null ], optimizeDeps: getOptimizeDeps(3), build: {