Files
outline/shared/editor/nodes/Math.ts
Tom Moor fa8685d241 Add support for LaTeX inline and block expressions. (#4446)
* Add support for LaTeX inline and block expressions. (#4364)

Co-authored-by: Tom Moor <tom@getoutline.com>

* tsc

* Show heading markers when LaTeX block is being edited

* Tab to space, name katex chunk

* Fork htmldiff, add support for math nodes

Co-authored-by: luisbc92 <luiscarlos.banuelos@gmail.com>
2022-11-27 06:27:56 -08:00

87 lines
1.9 KiB
TypeScript

import {
mathBackspaceCmd,
insertMathCmd,
makeInlineMathInputRule,
REGEX_INLINE_MATH_DOLLARS,
mathSchemaSpec,
} from "@benrbray/prosemirror-math";
import { PluginSimple } from "markdown-it";
import {
chainCommands,
deleteSelection,
selectNodeBackward,
joinBackward,
} from "prosemirror-commands";
import {
NodeSpec,
NodeType,
Schema,
Node as ProsemirrorNode,
} from "prosemirror-model";
import { EditorState, Plugin } from "prosemirror-state";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import MathPlugin from "../plugins/Math";
import mathRule from "../rules/math";
import { Dispatch } from "../types";
import Node from "./Node";
export default class Math extends Node {
get name() {
return "math_inline";
}
get schema(): NodeSpec {
return mathSchemaSpec.nodes.math_inline;
}
commands({ type }: { type: NodeType }) {
return () => (state: EditorState, dispatch: Dispatch) => {
dispatch(state.tr.replaceSelectionWith(type.create()).scrollIntoView());
return true;
};
}
inputRules({ schema }: { schema: Schema }) {
return [
makeInlineMathInputRule(
REGEX_INLINE_MATH_DOLLARS,
schema.nodes.math_inline
),
];
}
keys({ type }: { type: NodeType }) {
return {
"Mod-Space": insertMathCmd(type),
Backspace: chainCommands(
deleteSelection,
mathBackspaceCmd,
joinBackward,
selectNodeBackward
),
};
}
get plugins(): Plugin[] {
return [MathPlugin];
}
get rulePlugins(): PluginSimple[] {
return [mathRule];
}
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
state.write("$");
state.text(node.textContent, false);
state.write("$");
}
parseMarkdown() {
return {
node: "math_inline",
block: "math_inline",
noCloseToken: true,
};
}
}