Skip to content

Commit

Permalink
Feat:automatically update the hash value of searchIndex (#1723)
Browse files Browse the repository at this point in the history
Co-authored-by: liyang <liyang@selectdb.com>
  • Loading branch information
yang1666204 and liyang authored Jan 7, 2025
1 parent dbfd61e commit 6db337b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cron-deploy-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
42 changes: 42 additions & 0 deletions scripts/update_search_hash.js
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 6db337b

Please sign in to comment.