Files
paseo/packages/app/src/utils/markdown-list.ts
T
Mohamed Boudra eed61d338c Tighten markdown list spacing around prose
Lists previously had a static marginVertical that stacked with
paragraph.marginBottom, producing 20px gaps and oversized whitespace
around paragraphs sandwiched between lists.

Spacing is now context-aware via the render rule: list -> list keeps
the normal paragraph gap (back-to-back lists read as continuous
content) while p <-> list transitions get a slightly larger gap (lists
read as their own section). Static margins can't satisfy both because
RN doesn't collapse margins, so the bullet_list / ordered_list rule
inspects the next sibling type and picks marginBottom accordingly.
2026-05-15 14:03:14 +07:00

106 lines
2.5 KiB
TypeScript

interface MarkdownNode {
type?: string;
index?: number;
markup?: string;
attributes?: {
start?: number | string;
};
children?: MarkdownNode[];
}
const LIST_BULLET = "•";
const DEFAULT_ORDERED_LIST_MARKUP = ".";
function toParentNodes(parent: unknown): MarkdownNode[] {
if (Array.isArray(parent)) {
return parent;
}
if (parent && typeof parent === "object") {
return [parent as MarkdownNode];
}
return [];
}
function getNearestListParent(parent: unknown): MarkdownNode | undefined {
return toParentNodes(parent).find(
(ancestor) => ancestor?.type === "ordered_list" || ancestor?.type === "bullet_list",
);
}
function getOrderedListItemIndex(node: MarkdownNode, listParent: MarkdownNode): number {
if (typeof node.index === "number" && Number.isFinite(node.index) && node.index >= 0) {
return node.index;
}
if (Array.isArray(listParent.children)) {
const fallbackIndex = listParent.children.indexOf(node);
if (fallbackIndex >= 0) {
return fallbackIndex;
}
}
return 0;
}
function parseOrderedListStart(node: MarkdownNode): number {
const rawStart = node.attributes?.start;
if (typeof rawStart === "number" && Number.isFinite(rawStart)) {
return rawStart;
}
if (typeof rawStart === "string") {
const parsed = Number.parseInt(rawStart, 10);
if (Number.isFinite(parsed)) {
return parsed;
}
}
return 1;
}
export function getMarkdownNextSiblingType(
node: MarkdownNode,
parent: unknown,
): string | undefined {
const ancestors = toParentNodes(parent);
for (let i = ancestors.length - 1; i >= 0; i--) {
const ancestor = ancestors[i];
if (!Array.isArray(ancestor?.children)) continue;
const idx = ancestor.children.indexOf(node);
if (idx >= 0) {
return ancestor.children[idx + 1]?.type;
}
}
return undefined;
}
export function getMarkdownListMarker(
node: MarkdownNode,
parent: unknown,
): {
isOrdered: boolean;
marker: string;
} {
const listParent = getNearestListParent(parent);
if (!listParent || listParent.type !== "ordered_list") {
return {
isOrdered: false,
marker: LIST_BULLET,
};
}
const orderedIndex = getOrderedListItemIndex(node, listParent);
const orderedStart = parseOrderedListStart(listParent);
const orderedMarkup =
typeof node.markup === "string" && node.markup.length > 0
? node.markup
: DEFAULT_ORDERED_LIST_MARKUP;
return {
isOrdered: true,
marker: `${orderedStart + orderedIndex}${orderedMarkup}`,
};
}