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

Fix search term match and highlight for Unicode chars #402

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/assets/javascripts/sphinx_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ export async function getResults(query: string): Promise<SearchResultStream> {
// The search term made be made up of multiple "words" separated
// by special characters like [-._]. Split them up and treat each
// as a separate search term.
for (const wordMatch of lowerTerm.matchAll(/\w+/g)) {
for (const wordMatch of lowerTerm.matchAll(/\p{L}+/gu)) {
const subTerm = wordMatch[0]
if (stopwords.indexOf(subTerm) !== -1) {
// skip this "word"
Expand Down Expand Up @@ -794,16 +794,16 @@ export async function getResults(query: string): Promise<SearchResultStream> {
}

const pattern = new RegExp(
`\\b(?:${ hlterms.map(escapeRegExp).join("|") })`,
"img"
`(?:${ hlterms.map(escapeRegExp).join("|") })`,
"imgu"
)
const highlight = (s: unknown) => {
return `<mark data-md-highlight>${s}</mark>`
}
const highlightTerms = (text: string) => {
return escapeHTML(text)
.replace(pattern, highlight)
.replace(/<\/mark>(\s+)<mark[^>]*>/gim, "$1")
.replace(/<\/mark>(\s+)<mark[^>]*>/gimu, "$1")
}

return {
Expand Down