Skip to content

Commit 4454fa9

Browse files
committed
fix: use regexp.escape
1 parent fd3a597 commit 4454fa9

File tree

6 files changed

+19
-9
lines changed

6 files changed

+19
-9
lines changed

app/composables/useMarkdown.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ function stripAndEscapeHtml(text: string, packageName?: string): string {
4545
stripped = stripped.trim()
4646
// Collapse multiple whitespace into single space
4747
stripped = stripped.replace(/\s+/g, ' ')
48-
// Escape special regex characters in package name
49-
const escapedName = packageName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
5048
// Match package name at the start, optionally followed by: space, dash, colon, hyphen, or just space
51-
const namePattern = new RegExp(`^${escapedName}\\s*[-:—]?\\s*`, 'i')
49+
const namePattern = new RegExp(`^${RegExp.escape(packageName)}\\s*[-:—]?\\s*`, 'i')
5250
stripped = stripped.replace(namePattern, '').trim()
5351
}
5452

app/composables/useStructuredFilters.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@ export function useStructuredFilters(options: UseStructuredFiltersOptions) {
412412

413413
function removeKeyword(keyword: string) {
414414
filters.value.keywords = filters.value.keywords.filter(k => k !== keyword)
415-
const newQ = searchQuery.value.replace(new RegExp(`keyword:${keyword}($| )`, 'g'), '').trim()
415+
const newQ = searchQuery.value
416+
.replace(new RegExp(`keyword:${RegExp.escape(keyword)}($| )`, 'g'), '')
417+
.trim()
416418
router.replace({ query: { ...route.query, q: newQ || undefined } })
417419
if (searchQueryModel) searchQueryModel.value = newQ
418420
}

global.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare global {
2+
interface RegExpConstructor {
3+
escape(str: string): string
4+
}
5+
}
6+
7+
// required for the global type to work
8+
// oxlint-disable-next-line eslint-plugin-unicorn(require-module-specifiers)
9+
export {}

knip.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const config: KnipConfig = {
5555
'h3-next',
5656
],
5757
ignoreUnresolved: ['#components', '#oauth/config'],
58+
ignoreFiles: ['global.d.ts'],
5859
},
5960
'cli': {
6061
project: ['src/**/*.ts!', '!src/mock-*.ts'],

nuxt.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export default defineNuxtConfig({
203203
},
204204
typescript: {
205205
tsConfig: {
206-
include: ['../test/unit/server/**/*.ts'],
206+
include: ['../test/unit/server/**/*.ts', '../global.d.ts'],
207207
},
208208
},
209209
},
@@ -287,10 +287,10 @@ export default defineNuxtConfig({
287287
'#cli/*': ['../cli/src/*'],
288288
},
289289
},
290-
include: ['../test/unit/app/**/*.ts'],
290+
include: ['../test/unit/app/**/*.ts', '../global.d.ts'],
291291
},
292292
sharedTsConfig: {
293-
include: ['../test/unit/shared/**/*.ts'],
293+
include: ['../test/unit/shared/**/*.ts', '../global.d.ts'],
294294
},
295295
nodeTsConfig: {
296296
compilerOptions: {
@@ -301,7 +301,7 @@ export default defineNuxtConfig({
301301
'#shared/*': ['../shared/*'],
302302
},
303303
},
304-
include: ['../*.ts', '../test/e2e/**/*.ts'],
304+
include: ['../*.ts', '../test/e2e/**/*.ts', '../global.d.ts'],
305305
},
306306
},
307307

shared/utils/emoji.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ const emojis = {
19071907

19081908
const emojisKeysRegex = new RegExp(
19091909
Object.keys(emojis)
1910-
.map(key => `:${key}:`)
1910+
.map(key => `:${RegExp.escape(key)}:`)
19111911
.join('|'),
19121912
'g',
19131913
)

0 commit comments

Comments
 (0)