fix: docs search

This commit is contained in:
07akioni 2024-01-01 15:45:11 +08:00
parent 33a612c770
commit 356f1636ae
2 changed files with 33 additions and 9 deletions

View File

@ -328,9 +328,17 @@ export default defineComponent({
const searchableOptionsRef = useFlattenedDocOptions()
const searchPatternRef = ref('')
const searchOptionsRef = computed(() => {
function getLabel (item) {
// function getLabel(item) {
// if (item.label) {
// return typeof item.extra === 'function'
// ? () => [item.label, ' ', item.extra()]
// : item.label + (item.extra ? ' ' + item.extra : '')
// }
// return item.key
// }
function getSearchableContent (item) {
if (item.label) {
return item.label + (item.extra ? ' ' + item.extra : '')
return item.label + (item.extraString ? ' ' + item.extraString : '')
}
return item.key
}
@ -342,11 +350,13 @@ export default defineComponent({
.toLowerCase()
.replace(replaceRegex, '')
.slice(0, 20)
const label = getLabel(item).toLowerCase().replace(replaceRegex, '')
const label = getSearchableContent(item)
.toLowerCase()
.replace(replaceRegex, '')
return match(pattern, label)
})
.map((item) => ({
label: getLabel(item),
label: getSearchableContent(item),
value: item.path
}))
})

View File

@ -17,19 +17,32 @@ export const renderMenuLabel = (option) => {
)
}
const renderNewTag = () =>
h(NTag, { type: 'success', size: 'small' }, { default: () => 'New' })
const renderNewTag = (isZh) =>
h(
NTag,
{ type: 'success', size: 'small', round: true, bordered: false },
{ default: isZh ? () => '新' : () => 'New' }
)
const renderItemExtra = (rawItem, isZh) => {
if (!rawItem.enSuffix || !isZh) { return rawItem.isNew ? renderNewTag : undefined }
if (!rawItem.enSuffix || !isZh) {
return rawItem.isNew ? renderNewTag : undefined
}
const renderEn = () =>
h(
NSpace,
{ inline: true, size: 6 },
{ default: () => [rawItem.en, renderNewTag()] }
{ inline: true, size: 6, wrapItem: false, align: 'center' },
{ default: () => [rawItem.en, renderNewTag(isZh)] }
)
return rawItem.isNew ? renderEn : rawItem.en
}
const getItemExtraString = (rawItem, isZh) => {
if (!rawItem.enSuffix || !isZh) {
return ''
} else {
return rawItem.en
}
}
const appendCounts = (item) => {
if (!item.children) {
@ -56,6 +69,7 @@ function createItems (lang, theme, prefix, items) {
key: rawItem.en,
label: rawItem[langKey] || rawItem.en,
extra: renderItemExtra(rawItem, isZh),
extraString: getItemExtraString(rawItem, isZh),
path: rawItem.path
? `/${lang}/${theme}` + prefix + rawItem.path
: undefined