From 42d3ee8fc9882a9fb9f87e696cebd0e6146d2e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BChn?= Date: Thu, 13 Jan 2022 13:57:33 +0100 Subject: [PATCH] fix: support all characters for suggestion char, fix #2385 --- packages/core/src/index.ts | 1 + packages/core/src/utilities/escapeForRegEx.ts | 4 ++++ packages/suggestion/src/findSuggestionMatch.ts | 8 ++------ 3 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 packages/core/src/utilities/escapeForRegEx.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a9028dfe..9ab8174f 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -21,6 +21,7 @@ export * from './pasteRules/markPasteRule' export * from './pasteRules/textPasteRule' export * from './utilities/callOrReturn' +export * from './utilities/escapeForRegEx' export * from './utilities/mergeAttributes' export * from './helpers/combineTransactionSteps' diff --git a/packages/core/src/utilities/escapeForRegEx.ts b/packages/core/src/utilities/escapeForRegEx.ts new file mode 100644 index 00000000..e06bd41b --- /dev/null +++ b/packages/core/src/utilities/escapeForRegEx.ts @@ -0,0 +1,4 @@ +// source: https://stackoverflow.com/a/6969486 +export function escapeForRegEx(string: string): string { + return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') +} diff --git a/packages/suggestion/src/findSuggestionMatch.ts b/packages/suggestion/src/findSuggestionMatch.ts index fe7be2b9..17dd3894 100644 --- a/packages/suggestion/src/findSuggestionMatch.ts +++ b/packages/suggestion/src/findSuggestionMatch.ts @@ -1,4 +1,4 @@ -import { Range } from '@tiptap/core' +import { Range, escapeForRegEx } from '@tiptap/core' import { ResolvedPos } from 'prosemirror-model' export interface Trigger { @@ -24,11 +24,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch { $position, } = config - // Matching expressions used for later - const escapedChar = char - .split('') - .map(c => `\\${c}`) - .join('') + const escapedChar = escapeForRegEx(char) const suffix = new RegExp(`\\s${escapedChar}$`) const prefix = startOfLine ? '^' : '' const regexp = allowSpaces