mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
fix: code cannot contain LaTeX syntax (#10179)
This commit is contained in:
@@ -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])
|
||||
)
|
||||
);
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user