2022-06-08 20:10:25 +08:00
|
|
|
import { escapeForRegEx, Range } from '@tiptap/core'
|
2023-02-03 00:37:33 +08:00
|
|
|
import { ResolvedPos } from '@tiptap/pm/model'
|
2021-01-15 21:49:28 +08:00
|
|
|
|
|
|
|
export interface Trigger {
|
2023-02-03 00:37:33 +08:00
|
|
|
char: string
|
|
|
|
allowSpaces: boolean
|
|
|
|
allowedPrefixes: string[] | null
|
|
|
|
startOfLine: boolean
|
|
|
|
$position: ResolvedPos
|
2021-01-15 21:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export type SuggestionMatch = {
|
2023-02-03 00:37:33 +08:00
|
|
|
range: Range
|
|
|
|
query: string
|
|
|
|
text: string
|
2021-01-15 21:49:28 +08:00
|
|
|
} | null
|
|
|
|
|
|
|
|
export function findSuggestionMatch(config: Trigger): SuggestionMatch {
|
|
|
|
const {
|
2023-02-03 00:37:33 +08:00
|
|
|
char, allowSpaces, allowedPrefixes, startOfLine, $position,
|
2021-01-15 21:49:28 +08:00
|
|
|
} = config
|
|
|
|
|
2022-01-13 20:57:33 +08:00
|
|
|
const escapedChar = escapeForRegEx(char)
|
2021-01-15 21:49:28 +08:00
|
|
|
const suffix = new RegExp(`\\s${escapedChar}$`)
|
|
|
|
const prefix = startOfLine ? '^' : ''
|
|
|
|
const regexp = allowSpaces
|
|
|
|
? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm')
|
|
|
|
: new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm')
|
|
|
|
|
2022-03-25 03:25:38 +08:00
|
|
|
const text = $position.nodeBefore?.isText && $position.nodeBefore.text
|
2022-03-25 03:32:59 +08:00
|
|
|
|
|
|
|
if (!text) {
|
|
|
|
return null
|
|
|
|
}
|
2022-03-25 03:35:15 +08:00
|
|
|
|
2022-03-25 03:25:38 +08:00
|
|
|
const textFrom = $position.pos - text.length
|
2021-02-08 03:00:13 +08:00
|
|
|
const match = Array.from(text.matchAll(regexp)).pop()
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-02-08 03:00:13 +08:00
|
|
|
if (!match || match.input === undefined || match.index === undefined) {
|
2021-01-20 23:52:56 +08:00
|
|
|
return null
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-09-07 04:33:16 +08:00
|
|
|
// JavaScript doesn't have lookbehinds. This hacks a check that first character
|
|
|
|
// is a space or the start of the line
|
2021-01-20 23:52:56 +08:00
|
|
|
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
|
2022-06-22 05:17:26 +08:00
|
|
|
const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2022-06-22 05:17:26 +08:00
|
|
|
if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
|
2021-01-20 23:52:56 +08:00
|
|
|
return null
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
// The absolute position of the match in the document
|
2022-03-25 03:25:38 +08:00
|
|
|
const from = textFrom + match.index
|
2021-01-20 23:52:56 +08:00
|
|
|
let to = from + match[0].length
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
// Edge case handling; if spaces are allowed and we're directly in between
|
|
|
|
// two triggers
|
|
|
|
if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {
|
|
|
|
match[0] += ' '
|
|
|
|
to += 1
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
// If the $position is located within the matched substring, return that range
|
|
|
|
if (from < $position.pos && to >= $position.pos) {
|
|
|
|
return {
|
|
|
|
range: {
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
},
|
|
|
|
query: match[0].slice(char.length),
|
|
|
|
text: match[0],
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
return null
|
2021-01-15 21:49:28 +08:00
|
|
|
}
|