Files
outline/shared/editor/nodes/OrderedList.ts
huiseo 9ec5c473f1 fix: prevent list conversion inside heading nodes (#10462)
* fix: prevent list conversion inside heading nodes

Fixes a bug where typing list syntax (e.g., "1. ", "* ", "[ ]")
inside heading nodes would incorrectly trigger list conversion.

Previously, when a user selected H1 from the "/" menu and typed
"1. " followed by a space, the OrderedList inputRule would attempt
to convert the heading into an ordered list, causing a conflict
since headings can only contain inline content.

Changes:
- Add isInHeading utility to detect if selection is inside a heading
- Create safeWrappingInputRule wrapper that prevents list conversion
  when inside heading nodes
- Apply the fix to OrderedList, BulletList, and CheckboxList nodes

This ensures that list markdown syntax is preserved as plain text
when typed within headings, matching expected editor behavior.

* refactor: extract listWrappingInputRule to shared helper

Refactored duplicated safeWrappingInputRule implementations across
BulletList, OrderedList, and CheckboxList into a single shared helper
function named listWrappingInputRule in shared/editor/lib/listInputRule.ts.

This reduces code duplication and follows the same pattern as other
input rule helpers like markInputRule.

Changes:
- Create shared/editor/lib/listInputRule.ts with listWrappingInputRule
- Update BulletList.ts to use shared helper
- Update OrderedList.ts to use shared helper
- Update CheckboxList.ts to use shared helper
- Restore .env.development file

Co-Authored-By: huiseo <hui.seo@gmail.com>
2025-10-23 20:23:47 -04:00

181 lines
4.8 KiB
TypeScript

import { Token } from "markdown-it";
import {
NodeSpec,
NodeType,
Schema,
Node as ProsemirrorNode,
} from "prosemirror-model";
import toggleList from "../commands/toggleList";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import { listWrappingInputRule } from "../lib/listInputRule";
import Node from "./Node";
export default class OrderedList extends Node {
get name() {
return "ordered_list";
}
get schema(): NodeSpec {
return {
attrs: {
order: {
default: 1,
validate: "number",
},
listStyle: {
default: "number",
validate: (style: string) =>
["number", "lower-alpha", "upper-alpha"].includes(style),
},
},
content: "list_item+",
group: "block list",
parseDOM: [
{
tag: "ol",
getAttrs: (dom: HTMLOListElement) => ({
order: dom.hasAttribute("start")
? parseInt(dom.getAttribute("start") || "1", 10)
: 1,
listStyle: dom.style.listStyleType,
}),
},
],
toDOM: (node) => {
const attrs: { start?: number; style?: string } = {};
if (node.attrs.order !== 1) {
attrs.start = node.attrs.order;
}
if (node.attrs.listStyle !== "number") {
attrs.style = `list-style-type: ${node.attrs.listStyle}`;
}
return ["ol", attrs, 0];
},
};
}
commands({ type, schema }: { type: NodeType; schema: Schema }) {
return {
ordered_list: () => toggleList(type, schema.nodes.list_item),
toggleLowerLetterList: () =>
toggleList(type, schema.nodes.list_item, "lower-alpha"),
toggleUpperLetterList: () =>
toggleList(type, schema.nodes.list_item, "upper-alpha"),
};
}
keys({ type, schema }: { type: NodeType; schema: Schema }) {
return {
"Shift-Ctrl-9": toggleList(type, schema.nodes.list_item),
"Shift-Ctrl-5": this.commands({ type, schema }).toggleUpperLetterList(),
"Shift-Ctrl-6": this.commands({ type, schema }).toggleLowerLetterList(),
};
}
inputRules({ type }: { type: NodeType }) {
return [
listWrappingInputRule(
/^(\d+)\.\s$/,
type,
(match) => ({ order: +match[1], listStyle: "number" }),
(match, node) => node.childCount + node.attrs.order === +match[1]
),
listWrappingInputRule(
/^([a-z])\.\s$/,
type,
(match) => ({
order: match[1].charCodeAt(0) - 96,
listStyle: "lower-alpha",
}),
(match, node) => {
const expectedChar = String.fromCharCode(
96 + node.childCount + node.attrs.order
);
return expectedChar === match[1];
}
),
listWrappingInputRule(
/^([A-Z])\.\s$/,
type,
(match) => ({
order: match[1].charCodeAt(0) - 64,
listStyle: "upper-alpha",
}),
(match, node) => {
const expectedChar = String.fromCharCode(
64 + node.childCount + node.attrs.order
);
return expectedChar === match[1];
}
),
];
}
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
state.write("\n");
const start = node.attrs.order !== undefined ? node.attrs.order : 1;
const upperOrLowerAlpha =
node.attrs.listStyle === "lower-alpha" ||
node.attrs.listStyle === "upper-alpha";
if (upperOrLowerAlpha) {
const space = state.repeat(" ", 4);
state.renderList(node, space, (index: number) => {
const alphabetSize = 26;
const position = start + index;
const asciiStart = node.attrs.listStyle === "lower-alpha" ? 97 : 65;
let n = position - 1;
let result = "";
do {
const charCode = asciiStart + (n % alphabetSize);
result = String.fromCharCode(charCode) + result;
n = Math.floor(n / alphabetSize) - 1;
} while (n >= 0);
return result + ". ";
});
} else {
const maxW = `${start + node.childCount - 1}`.length;
const space = state.repeat(" ", maxW + 2);
state.renderList(node, space, (index: number) => {
const nStr = `${start + index}`;
return state.repeat(" ", maxW - nStr.length) + nStr + ". ";
});
}
}
parseMarkdown() {
return {
block: "ordered_list",
getAttrs: (tok: Token) => {
const start = tok.attrGet("start") || "1";
let listStyle = "number";
if (tok.markup && /^[a-z]/.test(tok.markup)) {
listStyle = "lower-alpha";
} else if (tok.markup && /^[A-Z]/.test(tok.markup)) {
listStyle = "upper-alpha";
}
return {
order: parseInt(start, 10),
listStyle,
};
},
};
}
}