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

proposedPlanText.ts

47 lines1.5 KB

1function headingText(line: string): string | null {
2 const heading = line.match(/^ {0,3}#{1,6}([ \t].*)?$/);
3 if (!heading) return null;
4 const text = heading[1] ?? "";
5 return text.replace(/[ \t]#+[ \t]*$/, "").trim();
6}
7​
8export function proposedPlanTitle(planMarkdown: string): string | null {
9 let fence = "";
10 for (const line of planMarkdown.replace(/^\uFEFF/, "").split(/\r?\n/)) {
11 const [, marker = "", info = ""] = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/) ?? [];
12 if (fence) {
13 if (marker[0] === fence[0] && marker.length >= fence.length && !info.trim()) fence = "";
14 continue;
15 }
16 // CommonMark: a backtick fence's info string cannot contain a backtick.
17 if (marker && (marker[0] !== "`" || !info.includes("`"))) {
18 fence = marker;
19 continue;
20 }
21 const heading = headingText(line);
22 if (heading) return heading;
23 }
24 return null;
26​
27export function stripDisplayedPlanMarkdown(planMarkdown: string): string {
28 const lines = planMarkdown
29 .replace(/^\uFEFF/, "")
30 .trimEnd()
31 .split(/\r?\n/);
32 let start = 0;
33 while (lines[start]?.trim().length === 0 || headingText(lines[start] ?? "") === "") {
34 start += 1;
35 }
36 if (headingText(lines[start] ?? "")) start += 1;
37 while (lines[start]?.trim().length === 0) {
38 start += 1;
39 }
40 if (headingText(lines[start] ?? "")?.toLowerCase() === "summary") {
41 start += 1;
42 while (lines[start]?.trim().length === 0) {
43 start += 1;
44 }
45 }
46 return lines.slice(start).join("\n");