Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Disable smart text replacements in code mark (#6839)
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
); | ||
} | ||
} |