Skip to content

Commit

Permalink
feat: 优化搜索插件默认配置
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jan 5, 2024
1 parent 12b1770 commit a3fa5e4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
13 changes: 7 additions & 6 deletions theme/src/node/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import autoFrontmatter from './autoFrontmatter.js'
import { resolveLocaleOptions } from './resolveLocaleOptions.js'
import { pathJoin } from './utils.js'
import { resolveNotesList } from './resolveNotesList.js'
import { resolvedDocsearchOption, resolvedSearchOptions } from './searchPluginOptions.js'

export function setupPlugins(
app: App,
Expand Down Expand Up @@ -129,16 +130,16 @@ export function setupPlugins(
}))
}

if (options.search !== false)
plugins.push(searchPlugin(options.search))

if (options.docsearch !== false && !options.search) {
if (options.docsearch?.appId && options.docsearch?.apiKey)
plugins.push(docsearchPlugin(options.docsearch))
if (options.docsearch) {
if (options.docsearch.appId && options.docsearch.apiKey)
plugins.push(docsearchPlugin(resolvedDocsearchOption(app, options.docsearch)))

else
console.error('docsearch plugin: appId and apiKey are both required')
}
else if (options.search !== false) {
plugins.push(searchPlugin(resolvedSearchOptions(app, options.search)))
}

if (options.shikiji !== false) {
plugins.push(shikijiPlugin({
Expand Down
88 changes: 88 additions & 0 deletions theme/src/node/searchPluginOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { DocsearchPluginOptions } from '@vuepress/plugin-docsearch'
import type { SearchPluginOptions } from '@vuepress/plugin-search'
import type { App } from '@vuepress/core'
import { deepMerge } from '@pengzhanbo/utils'

// `en-US` is used by default
const defaultDocsearchLocales: NonNullable<DocsearchPluginOptions['locales']> = {
'zh-CN': {
placeholder: '搜索文档',
translations: {
button: {
buttonText: '搜索文档',
buttonAriaLabel: '搜索文档',
},
modal: {
searchBox: {
resetButtonTitle: '清除查询条件',
resetButtonAriaLabel: '清除查询条件',
cancelButtonText: '取消',
cancelButtonAriaLabel: '取消',
},
startScreen: {
recentSearchesTitle: '搜索历史',
noRecentSearchesText: '没有搜索历史',
saveRecentSearchButtonTitle: '保存至搜索历史',
removeRecentSearchButtonTitle: '从搜索历史中移除',
favoriteSearchesTitle: '收藏',
removeFavoriteSearchButtonTitle: '从收藏中移除',
},
errorScreen: {
titleText: '无法获取结果',
helpText: '你可能需要检查你的网络连接',
},
footer: {
selectText: '选择',
navigateText: '切换',
closeText: '关闭',
searchByText: '搜索提供者',
},
noResultsScreen: {
noResultsText: '无法找到相关结果',
suggestedQueryText: '你可以尝试查询',
reportMissingResultsText: '你认为该查询应该有结果?',
reportMissingResultsLinkText: '点击反馈',
},
},
},
},
}

const defaultSearchLocales: NonNullable<SearchPluginOptions['locales']> = {
'zh-CN': { placeholder: '搜索' },
'en-US': { placeholder: 'Search' },
}

export function resolvedDocsearchOption(app: App, options: DocsearchPluginOptions): DocsearchPluginOptions {
options.locales ??= {}

Object.keys(app.siteData.locales || {}).forEach((locale) => {
const lang = app.siteData.locales![locale]?.lang || 'en-US'
if (defaultDocsearchLocales[lang]) {
options.locales![locale] = deepMerge(
{},
defaultDocsearchLocales[lang],
options.locales![locale] || {},
)
}
})

return options
}

export function resolvedSearchOptions(app: App, options: SearchPluginOptions = {}): SearchPluginOptions {
options.locales ??= {}

Object.keys(app.siteData.locales || {}).forEach((locale) => {
const lang = app.siteData.locales![locale]?.lang || 'en-US'
if (defaultSearchLocales[lang]) {
options.locales![locale] = deepMerge(
{},
defaultSearchLocales[lang],
options.locales![locale] || {},
)
}
})

return options
}

0 comments on commit a3fa5e4

Please sign in to comment.