Skip to content

Commit

Permalink
fix: Disable smart text replacements in code mark (#6839)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor committed Apr 24, 2024
1 parent 3f89905 commit 9b12d48
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/editor/extensions/SmartText.ts
@@ -1,5 +1,6 @@
import { ellipsis, smartQuotes, InputRule } from "prosemirror-inputrules";
import { ellipsis, smartQuotes } from "prosemirror-inputrules";
import Extension from "@shared/editor/lib/Extension";
import { InputRule } from "@shared/editor/lib/InputRule";

const rightArrow = new InputRule(/->$/, "→");
const emdash = new InputRule(/--$/, "—");
Expand Down
38 changes: 38 additions & 0 deletions shared/editor/lib/InputRule.ts
@@ -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);
}
);
}
}

0 comments on commit 9b12d48

Please sign in to comment.