fix: Remove parsing rules when not supported in editor (#12604)

* fix: Remove table markdown parsing rule when not supported in editor

* Additional nodes, tests
This commit is contained in:
Tom Moor
2026-06-05 23:42:40 -04:00
committed by GitHub
parent 997d38a6ac
commit be3f28afea
2 changed files with 46 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
import type { Schema } from "prosemirror-model";
import makeRules from "./rules";
const tableMarkdown = "| a | b |\n| --- | --- |\n| 1 | 2 |";
const schemaWith = (nodes: string[]) =>
({
nodes: Object.fromEntries(nodes.map((name) => [name, {}])),
}) as unknown as Schema;
describe("makeRules", () => {
it("parses tables when the schema supports them", () => {
const md = makeRules({ schema: schemaWith(["table"]) });
const types = md.parse(tableMarkdown, {}).map((token) => token.type);
expect(types).toContain("table_open");
});
it("does not emit table tokens when the schema lacks table support", () => {
const md = makeRules({ schema: schemaWith([]) });
const types = md.parse(tableMarkdown, {}).map((token) => token.type);
expect(types).not.toContain("table_open");
});
it("does not emit heading tokens for setext headings when the schema lacks heading support", () => {
const md = makeRules({ schema: schemaWith([]) });
const types = md.parse("Title\n=====\n", {}).map((token) => token.type);
expect(types).not.toContain("heading_open");
});
it("does not emit code_block tokens when the schema lacks code block support", () => {
const md = makeRules({ schema: schemaWith([]) });
const indented = md.parse(" foo\n", {}).map((token) => token.type);
const fenced = md.parse("```\nfoo\n```\n", {}).map((token) => token.type);
expect(indented).not.toContain("code_block");
expect(fenced).not.toContain("fence");
});
});
+9 -1
View File
@@ -36,7 +36,15 @@ export default function makeRules({
markdownIt.disable("hr");
}
if (!schema?.nodes.heading) {
markdownIt.disable("heading");
// "heading" is ATX (# Title), "lheading" is setext (Title\n=====).
markdownIt.disable(["heading", "lheading"]);
}
if (!schema?.nodes.table) {
markdownIt.disable("table");
}
if (!schema?.nodes.code_block) {
// "code" is indented code blocks, "fence" is ``` delimited blocks.
markdownIt.disable(["code", "fence"]);
}
plugins.forEach((plugin) => markdownIt.use(plugin));