Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat:automatically update the hash value of searchIndex #1723

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
Loading