fix: Disable smart text replacements in code mark (#6839)

This commit is contained in:
Tom Moor
2024-04-23 22:30:52 -04:00
committed by GitHub
parent 3f8990520b
commit 9b12d486f5
2 changed files with 40 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
import { InputRule as ProsemirrorInputRule } from "prosemirror-inputrules";
import { EditorState } from "prosemirror-state";
import isInCode from "../queries/isInCode";
/**
* A factory function for creating Prosemirror input rules that automatically insert text
* that matches a given regular expression unless the selection is inside a code block or code mark.
*/
export class InputRule extends ProsemirrorInputRule {
constructor(rule: RegExp, insert: string) {
super(
rule,
(
state: EditorState,
match: RegExpMatchArray,
start: number,
end: number
) => {
if (isInCode(state)) {
return null;
}
if (match[1]) {
const offset = match[0].lastIndexOf(match[1]);
insert += match[0].slice(offset + match[1].length);
start += offset;
const cutOff = start - end;
if (cutOff > 0) {
insert = match[0].slice(offset - cutOff, offset) + insert;
start = end;
}
}
return state.tr.insertText(insert, start, end);
}
);
}
}