Open History to see the last commit
1import remarkParse from "remark-parse";
2import { unified } from "unified";
3
4const planParser = unified().use(remarkParse).freeze();
5
6function headingText(line: string): string | null {
7 const heading = line.match(/^ {0,3}#{1,6}([ \t].*)?$/);
8 if (!heading) return null;
9 const text = heading[1] ?? "";
10 return text.replace(/[ \t]#+[ \t]*$/, "").trim();
11}
12
13function findPlanTitle(lines: readonly string[]) {
14 for (const node of planParser.parse(lines.join("\n")).children) {
15 if (node.type !== "heading" || !node.position) continue;
16 const index = node.position.start.line - 1;
17 const heading = headingText(lines[index] ?? "");
18 if (heading) return { index, text: heading };
19 }
20 return null;
21}
22
23export function proposedPlanTitle(planMarkdown: string): string | null {
24 return findPlanTitle(planMarkdown.replace(/^\uFEFF/, "").split(/\r\n?|\n/))?.text ?? null;
25}
26
27/** The separator line is a zero-based index into the returned Markdown. */
28export function displayedPlanMarkdown(planMarkdown: string): {
29 markdown: string;
30 separatorLine?: number;
31} {
32 const lines = planMarkdown
33 .replace(/^\uFEFF/, "")
34 .trimEnd()
35 .split(/\r\n?|\n/);
36 const title = findPlanTitle(lines);
37 let start = 0;
38 let commentStart = -1;
39 const preambleBlocks: Array<{ start: number; end: number }> = [];
40 for (const line of lines) {
41 if (commentStart < 0 && /^ {0,3}<!--/.test(line)) commentStart = start;
42 if (commentStart < 0 && line.trim().length > 0 && headingText(line) !== "") break;
43 const end = commentStart >= 0 ? line.indexOf("-->") : -1;
44 if (end >= 0) {
45 if (preambleBlocks.length > 0 || line.slice(end + 3).trim()) {
46 preambleBlocks.push({ start: commentStart, end: start + 1 });
47 }
48 commentStart = -1;
49 }
50 start += 1;
51 }
52 const firstPreambleBlock = preambleBlocks[0];
53 if (!title && firstPreambleBlock) {
54 return { markdown: lines.slice(firstPreambleBlock.start).join("\n") };
55 }
56 const contentStart = start;
57 if (title) start = title.index + 1;
58 while (lines[start]?.trim().length === 0) {
59 start += 1;
60 }
61 if (headingText(lines[start] ?? "")?.toLowerCase() === "summary") {
62 start += 1;
63 while (lines[start]?.trim().length === 0) {
64 start += 1;
65 }
66 }
67 const body = lines.slice(start).join("\n");
68 if (title && title.index > contentStart) {
69 const before = lines
70 .slice(firstPreambleBlock?.start ?? contentStart, title.index)
71 .join("\n")
72 .trimEnd();
73 if (!body) return { markdown: before };
74 // A link definition keeps adjacent lists separate even when mobile strips HTML comments.
75 // Use a label absent from the plan so the definition cannot resolve an authored reference.
76 const usedLabels = new Set(`${before}\n${body}`.match(/\d+/g));
77 let label = 0;
78 while (usedLabels.has(String(label))) label += 1;
79 return {
80 markdown: `${before}\n\n[${label}]: <>\n\n${body}`,
81 separatorLine: before.split("\n").length + 1,
82 };
83 }
84 if (!firstPreambleBlock) return { markdown: body };
85 const preamble = preambleBlocks
86 .map(({ start, end }) => lines.slice(start, end).join("\n"))
87 .join("\n");
88 return { markdown: body ? `${preamble}\n\n${body}` : preamble };
89}
90
91export function stripDisplayedPlanMarkdown(planMarkdown: string): string {
92 return displayedPlanMarkdown(planMarkdown).markdown;
93}