feat: Allow multiple prefix characters to trigger a suggestion (#2896)

* feat: Allow multiple prefix characters to trigger a suggestion

* review: Turn `allowedPrefixes` into an array instead
This commit is contained in:
Ricardo Amaral
2022-06-21 22:17:26 +01:00
committed by GitHub
parent 53e39d0c47
commit 482cb960d6
3 changed files with 12 additions and 7 deletions

View File

@@ -4,7 +4,7 @@ import { ResolvedPos } from 'prosemirror-model'
export interface Trigger {
char: string,
allowSpaces: boolean,
prefixSpace: boolean,
allowedPrefixes: string[] | null,
startOfLine: boolean,
$position: ResolvedPos,
}
@@ -19,7 +19,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
const {
char,
allowSpaces,
prefixSpace,
allowedPrefixes,
startOfLine,
$position,
} = config
@@ -47,9 +47,9 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
// JavaScript doesn't have lookbehinds. This hacks a check that first character
// is a space or the start of the line
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix)
const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)
if (prefixSpace && !matchPrefixIsSpace) {
if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
return null
}