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

markdownListIndentation.ts

139 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 // A text prefix forces block-looking input into a paragraph while preserving
61 // the processor's configured inline extensions (for example, GFM syntax).
62 // Later root children are kept as blocks so blank-line-separated content is
63 // never discarded.
64 const source = `${INLINE_PARSE_PREFIX}${value}`;
65 const document = parser.parse(source) as MarkdownAstNode;
66 const blocks = document.children;
67 const paragraph = blocks?.[0];
68 const children = paragraph?.type === "paragraph" ? paragraph.children : undefined;
69 const first = children?.[0];
70 if (
71 !blocks ||
72 !children ||
73 first?.type !== "text" ||
74 typeof first.value !== "string" ||
75 !first.value.startsWith(INLINE_PARSE_PREFIX)
76 ) {
77 return { blocks: [{ type: "text", value }], source };
78 }
79​
80 const firstValue = first.value.slice(INLINE_PARSE_PREFIX.length);
81 return {
82 blocks: [
83 {
84 ...paragraph,
85 type: "paragraph",
86 children: [...(firstValue ? [{ ...first, value: firstValue }] : []), ...children.slice(1)],
87 },
88 ...blocks.slice(1),
89 ],
90 source,
91 };
93​
94function blocksFromIndentedCode(node: MarkdownAstNode, parser: MarkdownParser): RecoveredMarkdown {
95 const value = typeof node.value === "string" ? node.value.trim() : "";
96 const recovered = parseRecoveredMarkdown(value, parser);
97 const first = recovered.blocks[0];
98 return {
99 ...recovered,
100 blocks:
101 first && node.position
102 ? [{ ...first, position: node.position }, ...recovered.blocks.slice(1)]
103 : recovered.blocks,
104 };
106​
107/**
108 * CommonMark treats four or more spaces after a list marker as an indented code
109 * block.
110 */
111function attachListItemIndentationNormalizer(this: MarkdownParser) {
112 return (tree: MarkdownAstNode, file: MarkdownFile) => {
113 if (typeof file.value !== "string") {
114 return;
115 }
116 const markdown = file.value;
117​
118 const visit = (node: MarkdownAstNode, source: string) => {
119 if (!node.children) {
120 return;
121 }
122 node.children = node.children.flatMap((child) => {
123 if (isSameLineOverIndentedCode(child, node, source)) {
124 const recovered = blocksFromIndentedCode(child, this);
125 for (const block of recovered.blocks) {
126 visit(block, recovered.source);
127 }
128 return recovered.blocks;
129 }
130 visit(child, source);
131 return [child];
132 });
133 };
134​
135 visit(tree, markdown);
136 };
138​
139export const remarkNormalizeListItemIndentation = attachListItemIndentationNormalizer;