Skip to content
GitHubDiscord

prosekit/extensions/autocomplete

An autocomplete rule that can be used to create an autocomplete extension.

new AutocompleteRule(options: AutocompleteRuleOptions): AutocompleteRule

Options for creating an AutocompleteRule

canMatch?: CanMatchPredicate

A predicate to determine if the rule can be applied in the current editor state. If not provided, it defaults to only allowing matches in empty selections that are not inside a code block or code mark.

onEnter: MatchHandler

A callback that is called when the rule starts to match, and also on subsequent updates while the rule continues to match.

onLeave?: VoidFunction

A callback that is called when the rule stops matching.

regex: RegExp

The regular expression to match against the text before the cursor. The last match before the cursor is used.

For a slash menu, you might use //(|\S.*)$/u. For a mention, you might use /@\w*$/


Options for the CanMatchPredicate callback.

state: EditorState

The editor state.


Options for the MatchHandler callback.

deleteMatch: () => void

Call this function to delete the matched text. For example, in a slash menu, you might want to delete the matched text first then do something else when the user presses the Enter key.

from: number

The start position of the matched text.

ignoreMatch: () => void

Call this function to ignore the match. You probably want to call this function when the user presses the Escape key.

match: RegExpExecArray

The result of RegExp.exec.

state: EditorState

The editor state.

to: number

The end position of the matched text.

type CanMatchPredicate = (options: CanMatchOptions) => boolean

A predicate to determine if the rule can be applied in the current editor state.


type MatchHandler = (options: MatchHandlerOptions) => void

A callback that is called when the rule starts to match, and also on subsequent updates while the rule continues to match.

function defineAutocomplete(rule: AutocompleteRule): Extension