Skip to content

Commit

Permalink
fix(search): better highlighting in detailed view (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Apr 16, 2023
1 parent 40f1d1b commit be83524
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -93,6 +93,7 @@
"@vue/devtools-api": "^6.5.0",
"@vueuse/core": "^10.0.2",
"body-scroll-lock": "4.0.0-beta.0",
"mark.js": "^8.11.1",
"minisearch": "^6.0.1",
"shiki": "^0.14.1",
"vite": "^4.2.1",
Expand Down Expand Up @@ -120,6 +121,7 @@
"@types/fs-extra": "^11.0.1",
"@types/koa": "^2.13.6",
"@types/koa-static": "^4.0.2",
"@types/mark.js": "^8.11.8",
"@types/markdown-it": "^12.2.3",
"@types/markdown-it-attrs": "^4.1.0",
"@types/markdown-it-container": "^2.0.5",
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions src/client/theme-default/components/VPLocalSearchBox.vue
Expand Up @@ -8,19 +8,20 @@ import {
useLocalStorage,
useSessionStorage
} from '@vueuse/core'
import Mark from 'mark.js'
import MiniSearch, { type SearchResult } from 'minisearch'
import { useRouter } from 'vitepress'
import {
computed,
createApp,
markRaw,
nextTick,
onMounted,
ref,
shallowRef,
watch,
type Ref,
computed,
watchEffect
watchEffect,
type Ref
} from 'vue'
import type { ModalTranslations } from '../../../../types/local-search'
import { pathToFile, withBase } from '../../app/utils'
Expand Down Expand Up @@ -177,9 +178,6 @@ debouncedWatch(
.map((t) => t?.replace(reg, `<mark>$&</mark>`))
.filter(Boolean)
}
if (showDetailedListValue && match.includes('text')) {
text = text.replace(reg, `<mark>$&</mark>`)
}
}
return { ...r, title, titles, text }
Expand All @@ -188,10 +186,11 @@ debouncedWatch(
await nextTick()
const excerpts = el.value?.querySelectorAll('.result .excerpt') ?? []
let i = 0
for (const excerpt of excerpts) {
excerpt.querySelector('mark')?.scrollIntoView({
block: 'center'
})
new Mark(excerpt as HTMLElement).mark(Object.keys(results.value[i].match))
excerpt.querySelector('mark')?.scrollIntoView({ block: 'center' })
i += 1
}
},
{ debounce: 200, immediate: true }
Expand Down Expand Up @@ -526,7 +525,7 @@ useEventListener('popstate', (event) => {
</Teleport>
</template>

<style scoped lang="postcss">
<style scoped>
.VPLocalSearchBox {
position: fixed;
z-index: 100;
Expand Down

0 comments on commit be83524

Please sign in to comment.