Fix safari issue (#2666)

This commit is contained in:
Bruce Berrios 2022-03-09 17:36:18 -05:00 committed by GitHub
parent 95a8a32008
commit 746fcbe934
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -13,6 +13,7 @@ import {
TextExtractor,
} from "../utils/search-service";
import { debounce } from "../utils/debounce";
import { splitByAndKeep } from "../utils/strings";
export let selectedId = 1;
@ -41,9 +42,7 @@ function highlightMatches(text: string, matchedText: string[]): string {
if (matchedText.length === 0) {
return text;
}
const words = text.split(
/(?=[.,"/#!$%^&*;:{}=\-_`~()\s])|(?<=[.,"/#!$%^&*;:{}=\-_`~()\s])/g
);
const words = splitByAndKeep(text, `.,"/#!$%^&*;:{}=-_\`~() `.split(""));
const normalizedWords = words.map((word) => {
const shouldHighlight = matchedText.find((match) => {

View file

@ -0,0 +1,28 @@
/**
*
* @param text String to split
* @param delimiters Single character delimiters.
*/
export function splitByAndKeep(text: string, delimiters: string[]): string[] {
const splitString: string[] = [];
let currentToken: string[] = [];
const delimiterSet = new Set<string>(delimiters);
for (const char of text) {
if (delimiterSet.has(char)) {
if (currentToken.length > 0) {
splitString.push(currentToken.join(""));
}
splitString.push(char);
currentToken = [];
} else {
currentToken.push(char);
}
}
if (currentToken.length > 0) {
splitString.push(currentToken.join(""));
}
return splitString;
}