From c4a9d78f293b5f030b1440b6d59f16b1383aa16c Mon Sep 17 00:00:00 2001 From: liyang Date: Tue, 7 Jan 2025 15:01:41 +0800 Subject: [PATCH] Feat:automatically update the hash value of searchIndex --- .github/workflows/cron-deploy-website.yml | 2 ++ scripts/update_search_hash.js | 42 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 scripts/update_search_hash.js diff --git a/.github/workflows/cron-deploy-website.yml b/.github/workflows/cron-deploy-website.yml index 90394beaa4d4a..fa7b4b7b5db50 100644 --- a/.github/workflows/cron-deploy-website.yml +++ b/.github/workflows/cron-deploy-website.yml @@ -38,6 +38,8 @@ jobs: yarn cache clean export NODE_OPTIONS=--max-old-space-size=8192 yarn && yarn build + node ./scripts/update_search_hash.js + cp worker.js ./build/ touch build/.dummy cp .asf-site.yaml ./build/.asf.yaml cp versions.json ./build/ diff --git a/scripts/update_search_hash.js b/scripts/update_search_hash.js new file mode 100644 index 0000000000000..3e500661858d8 --- /dev/null +++ b/scripts/update_search_hash.js @@ -0,0 +1,42 @@ +const fs = require('fs'); +const path = require('path'); + +const { getIndexHash } = require('@easyops-cn/docusaurus-search-local/dist/server/server/utils/getIndexHash.js'); + +function ensureArray(object, key) { + if (!Array.isArray(object[key])) { + object[key] = [object[key]]; + } +} + +const searchConfig = { + hashed: true, + language: ['en', 'zh'], + highlightSearchTermsOnTargetPage: true, + docsDir: ['docs', 'versioned_docs', 'i18n'], + blogDir: 'blog', + // indexPages: true, + indexDocs: true, + // docsRouteBasePath: '/docs', + indexBlog: false, + explicitSearchResultPath: true, + searchBarShortcut: true, + searchBarShortcutHint: true, + searchResultLimits: 100, +}; +ensureArray(searchConfig, 'docsDir'); +ensureArray(searchConfig, 'blogDir'); +searchConfig.docsDir = searchConfig.docsDir.map(dir => path.join(__dirname, `../${dir}`)); +searchConfig.blogDir = searchConfig.blogDir.map(dir => path.join(__dirname, `../${dir}`)); +const searchHash = getIndexHash(searchConfig); +if (searchHash) { + const workerJSPath = path.join(__dirname, '../worker.js'); + const workerJs = fs.readFileSync(workerJSPath, 'utf-8'); + const targetRegex = /const searchIndexUrl = "search-index\{dir\}\.json\?_=[^"]+"/; + const replacement = `const searchIndexUrl = "search-index{dir}.json?_=${searchHash}"`; + const newWorkjs = workerJs.replace(targetRegex, replacement); + fs.writeFileSync(workerJSPath, newWorkjs, 'utf-8'); + console.log('successful,searchHash:',searchHash) +}else{ + console.log('searchHash is null'); +}