move escape regexp to shared

pull/3403/head
Divyansh Singh 2 years ago
parent 3e06baee1b
commit 8f896d387c

@ -28,6 +28,7 @@ import {
} from 'vue' } from 'vue'
import type { ModalTranslations } from '../../../../types/local-search' import type { ModalTranslations } from '../../../../types/local-search'
import { pathToFile } from '../../app/utils' import { pathToFile } from '../../app/utils'
import { escapeRegExp } from '../../shared'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { LRUCache } from '../support/lru' import { LRUCache } from '../support/lru'
import { createSearchTranslate } from '../support/translation' import { createSearchTranslate } from '../support/translation'
@ -146,8 +147,8 @@ const cache = new LRUCache<string, Map<string, string>>(16) // 16 files
debouncedWatch( debouncedWatch(
() => [searchIndex.value, filterText.value, showDetailedList.value] as const, () => [searchIndex.value, filterText.value, showDetailedList.value] as const,
async ([index, filterTextValue, showDetailedListValue], old, onCleanup) => { async ([index, filterTextValue, showDetailedListValue], old, onCleanup) => {
if (old?.[0] !== index) {
if (old?.[0] !== index) { // in case of hmr // in case of hmr
cache.clear() cache.clear()
} }
@ -396,11 +397,7 @@ function formMarkRegex(terms: Set<string>) {
return new RegExp( return new RegExp(
[...terms] [...terms]
.sort((a, b) => b.length - a.length) .sort((a, b) => b.length - a.length)
.map((term) => { .map((term) => `(${escapeRegExp(term)})`)
return `(${term
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d')})`
})
.join('|'), .join('|'),
'gi' 'gi'
) )

@ -11,7 +11,7 @@ import {
import { APP_PATH } from '../alias' import { APP_PATH } from '../alias'
import type { SiteConfig } from '../config' import type { SiteConfig } from '../config'
import { createVitePressPlugin } from '../plugin' import { createVitePressPlugin } from '../plugin'
import { sanitizeFileName, slash } from '../shared' import { escapeRegExp, sanitizeFileName, slash } from '../shared'
import { task } from '../utils/task' import { task } from '../utils/task'
import { buildMPAClient } from './buildMPAClient' import { buildMPAClient } from './buildMPAClient'
@ -257,7 +257,3 @@ function staticImportedByEntry(
cache.set(id, someImporterIs) cache.set(id, someImporterIs)
return someImporterIs return someImporterIs
} }
function escapeRegExp(str: string) {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}

@ -195,3 +195,8 @@ export function treatAsHtml(filename: string): boolean {
return ext == null || !KNOWN_EXTENSIONS.has(ext.toLowerCase()) return ext == null || !KNOWN_EXTENSIONS.has(ext.toLowerCase())
} }
// https://github.com/sindresorhus/escape-string-regexp/blob/ba9a4473850cb367936417e97f1f2191b7cc67dd/index.js
export function escapeRegExp(str: string) {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}

Loading…
Cancel
Save