-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat:automatically update the hash value of searchIndex (#1723)
Co-authored-by: liyang <liyang@selectdb.com>
- Loading branch information
1 parent
dbfd61e
commit 6db337b
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |