Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdownListIndentation.ts

137 lines3.8 KB

1interface MarkdownPosition {
2 readonly start?: {
3 readonly line?: number;
4 readonly offset?: number;
5 };
6}
7​
8interface MarkdownAstNode {
9 readonly type: string;
10 readonly value?: unknown;
11 readonly position?: MarkdownPosition;
12 children?: MarkdownAstNode[];
14​
15interface MarkdownFile {
16 readonly value?: unknown;
18​
19interface MarkdownParser {
20 parse(markdown: string): unknown;
22​
23interface RecoveredMarkdown {
24 readonly blocks: MarkdownAstNode[];
25 readonly source: string;
27​
28const INLINE_PARSE_PREFIX = "t3-markdown-inline-prefix:";
29​
30function isSameLineOverIndentedCode(
31 node: MarkdownAstNode,
32 parent: MarkdownAstNode | undefined,
33 markdown: string,
34): boolean {
35 if (
36 node.type !== "code" ||
37 parent?.type !== "listItem" ||
38 typeof node.value !== "string" ||
39 !/^[\t ]/.test(node.value)
40 ) {
41 return false;
42 }
43​
44 const nodeStart = node.position?.start;
45 const parentStart = parent.position?.start;
46 if (
47 nodeStart?.line === undefined ||
48 nodeStart.offset === undefined ||
49 parentStart?.line === undefined ||
50 nodeStart.line !== parentStart.line
51 ) {
52 return false;
53 }
54​
55 const sourceCharacter = markdown[nodeStart.offset];
56 return sourceCharacter !== "`" && sourceCharacter !== "~";
58​
59function parseRecoveredMarkdown(value: string, parser: MarkdownParser): RecoveredMarkdown {
60 // Prefix block-looking input to parse inline extensions; retain later block children after blank lines.
61 const source = `${INLINE_PARSE_PREFIX}${value}`;
62 const document = parser.parse(source) as MarkdownAstNode;
63 const blocks = document.children;
64 const paragraph = blocks?.[0];
65 const children = paragraph?.type === "paragraph" ? paragraph.children : undefined;
66 const first = children?.[0];
67 if (
68 !blocks ||
69 !children ||
70 first?.type !== "text" ||
71 typeof first.value !== "string" ||
72 !first.value.startsWith(INLINE_PARSE_PREFIX)
73 ) {
74 return { blocks: [{ type: "text", value }], source };
75 }
76​
77 const firstValue = first.value.slice(INLINE_PARSE_PREFIX.length);
78 return {
79 blocks: [
80 {
81 ...paragraph,
82 type: "paragraph",
83 children: [...(firstValue ? [{ ...first, value: firstValue }] : []), ...children.slice(1)],
84 },
85 ...blocks.slice(1),
86 ],
87 source,
88 };
90​
91function blocksFromIndentedCode(node: MarkdownAstNode, parser: MarkdownParser): RecoveredMarkdown {
92 const value = typeof node.value === "string" ? node.value.trim() : "";
93 const recovered = parseRecoveredMarkdown(value, parser);
94 const first = recovered.blocks[0];
95 return {
96 ...recovered,
97 blocks:
98 first && node.position
99 ? [{ ...first, position: node.position }, ...recovered.blocks.slice(1)]
100 : recovered.blocks,
101 };
103​
104// CommonMark treats four or more spaces after a list marker as code.
105function attachListItemIndentationNormalizer(this: MarkdownParser) {
106 return (tree: MarkdownAstNode, file: MarkdownFile) => {
107 if (typeof file.value !== "string") {
108 return;
109 }
110 const markdown = file.value;
111​
112 const visit = (node: MarkdownAstNode, source: string) => {
113 if (!node.children) {
114 return;
115 }
116 if (!node.children.some((child) => isSameLineOverIndentedCode(child, node, source))) {
117 for (const child of node.children) visit(child, source);
118 return;
119 }
120 node.children = node.children.flatMap((child) => {
121 if (isSameLineOverIndentedCode(child, node, source)) {
122 const recovered = blocksFromIndentedCode(child, this);
123 for (const block of recovered.blocks) {
124 visit(block, recovered.source);
125 }
126 return recovered.blocks;
127 }
128 visit(child, source);
129 return [child];
130 });
131 };
132​
133 visit(tree, markdown);
134 };
136​
137export const remarkNormalizeListItemIndentation = attachListItemIndentationNormalizer;