Files
outline/shared/editor/nodes/HorizontalRule.ts
dependabot[bot] 0e75edf7e3 chore(deps): bump prosemirror-markdown from 1.13.0 to 1.13.1 (#7811)
* chore(deps): bump prosemirror-markdown from 1.13.0 to 1.13.1

Bumps [prosemirror-markdown](https://github.com/prosemirror/prosemirror-markdown) from 1.13.0 to 1.13.1.
- [Changelog](https://github.com/ProseMirror/prosemirror-markdown/blob/master/CHANGELOG.md)
- [Commits](https://github.com/prosemirror/prosemirror-markdown/compare/1.13.0...1.13.1)

---
updated-dependencies:
- dependency-name: prosemirror-markdown
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* tsc

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom.moor@gmail.com>
2024-10-21 17:50:32 -07:00

80 lines
1.9 KiB
TypeScript

import { Token } from "markdown-it";
import { InputRule } from "prosemirror-inputrules";
import { NodeSpec, NodeType, Node as ProsemirrorNode } from "prosemirror-model";
import { Command } from "prosemirror-state";
import { Primitive } from "utility-types";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import Node from "./Node";
export default class HorizontalRule extends Node {
get name() {
return "hr";
}
get schema(): NodeSpec {
return {
attrs: {
markup: {
default: "---",
},
},
group: "block",
parseDOM: [{ tag: "hr" }],
toDOM: (node) => [
"hr",
{ class: node.attrs.markup === "***" ? "page-break" : "" },
],
};
}
commands({ type }: { type: NodeType }) {
return (attrs: Record<string, Primitive>): Command =>
(state, dispatch) => {
dispatch?.(
state.tr.replaceSelectionWith(type.create(attrs)).scrollIntoView()
);
return true;
};
}
keys({ type }: { type: NodeType }): Record<string, Command> {
return {
"Mod-_": (state, dispatch) => {
dispatch?.(
state.tr.replaceSelectionWith(type.create()).scrollIntoView()
);
return true;
},
};
}
inputRules({ type }: { type: NodeType }) {
return [
new InputRule(/^(?:---|___\s|\*\*\*\s)$/, (state, match, start, end) => {
const { tr } = state;
if (match[0]) {
const markup = match[0].trim();
tr.replaceWith(start - 1, end, type.create({ markup }));
}
return tr;
}),
];
}
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
state.write(`\n${node.attrs.markup}`);
state.closeBlock(node);
}
parseMarkdown() {
return {
node: "hr",
getAttrs: (tok: Token) => ({
markup: tok.markup,
}),
};
}
}