fix: code cannot contain LaTeX syntax (#10179)

This commit is contained in:
Tom Moor
2025-09-16 02:58:20 +02:00
committed by GitHub
parent 792fd96f38
commit 9bc6ae44ad
3 changed files with 50 additions and 22 deletions
+30 -5
View File
@@ -1,7 +1,6 @@
import {
mathBackspaceCmd,
insertMathCmd,
makeInlineMathInputRule,
mathSchemaSpec,
} from "@benrbray/prosemirror-math";
import { PluginSimple } from "markdown-it";
@@ -16,6 +15,8 @@ import MathPlugin from "../extensions/Math";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import mathRule, { REGEX_INLINE_MATH_DOLLARS } from "../rules/math";
import Node from "./Node";
import { InputRule } from "prosemirror-inputrules";
import { isInCode } from "../queries/isInCode";
export default class Math extends Node {
get name() {
@@ -35,10 +36,34 @@ export default class Math extends Node {
inputRules({ schema }: { schema: Schema }) {
return [
makeInlineMathInputRule(
REGEX_INLINE_MATH_DOLLARS,
schema.nodes.math_inline
),
new InputRule(REGEX_INLINE_MATH_DOLLARS, (state, match, start, end) => {
if (isInCode(state)) {
return null;
}
let $start = state.doc.resolve(start);
let index = $start.index();
let $end = state.doc.resolve(end);
// check if replacement valid
if (
!$start.parent.canReplaceWith(
index,
$end.index(),
schema.nodes.math_inline
)
) {
return null;
}
// perform replacement
return state.tr.replaceRangeWith(
start,
end,
schema.nodes.math_inline.create(
undefined,
schema.nodes.math_inline.schema.text(match[1])
)
);
}),
];
}
+13 -8
View File
@@ -17,14 +17,19 @@ export function getMarksBetween(
let marks: { start: number; end: number; mark: Mark }[] = [];
state.doc.nodesBetween(start, end, (node, pos) => {
marks = [
...marks,
...node.marks.map((mark) => ({
start: pos,
end: pos + node.nodeSize,
mark,
})),
];
if (node.isText) {
const nodeStart = Math.max(start, pos);
const nodeEnd = Math.min(end, pos + node.nodeSize);
marks = [
...marks,
...node.marks.map((mark) => ({
start: nodeStart,
end: nodeEnd,
mark,
})),
];
}
});
return marks;
+7 -9
View File
@@ -20,21 +20,21 @@ type Options = {
*/
export function isInCode(state: EditorState, options?: Options): boolean {
const { nodes, marks } = state.schema;
const opts =
options?.inclusive !== undefined
? { inclusive: options?.inclusive }
: undefined;
if (!options?.onlyMark) {
if (
nodes.code_block &&
isNodeActive(nodes.code_block, undefined, {
inclusive: options?.inclusive,
})(state)
isNodeActive(nodes.code_block, undefined, opts)(state)
) {
return true;
}
if (
nodes.code_fence &&
isNodeActive(nodes.code_fence, undefined, {
inclusive: options?.inclusive,
})(state)
isNodeActive(nodes.code_fence, undefined, opts)(state)
) {
return true;
}
@@ -42,9 +42,7 @@ export function isInCode(state: EditorState, options?: Options): boolean {
if (!options?.onlyBlock) {
if (marks.code_inline) {
return isMarkActive(marks.code_inline, undefined, {
inclusive: options?.inclusive,
})(state);
return isMarkActive(marks.code_inline, undefined, opts)(state);
}
}