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