GeezerVison is showing github.com — fix(shared): proposed plan titles ignore code comments and empty head… · patroza/t3code@8bbdd33
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { expect, it } from "vite-plus/test"; | | 2 | +import { proposedPlanTitle, stripDisplayedPlanMarkdown } from "./proposedPlanText.ts"; | | 3 | + | | 4 | +it("takes the plan title from headings outside code", () => { | | 5 | + for (const [open, inner, close] of [ | | 6 | + ["```bash", "~~~", "```"], | | 7 | + ["~~~bash", "```", "~~~"], | | 8 | + ["````bash", "```", "````"], | | 9 | + ]) { | | 10 | + const code = `${open}\n# Back up database\n${inner}\n# Still code\n${close}\n`; | | 11 | + expect(proposedPlanTitle(`${code}\n## Real Plan Title\n`)).toBe("Real Plan Title"); | | 12 | + } | | 13 | + expect(proposedPlanTitle("```bash\n# Back up database\n")).toBeNull(); | | 14 | + expect(proposedPlanTitle("\t# Back up database\n\n## Real Plan Title")).toBe("Real Plan Title"); | | 15 | +}); | | 16 | + | | 17 | +it("skips empty plan headings without reading into the next line", () => { | | 18 | + const markdown = "# \r\n###\t\r\n## Real Title\r\n\r\n## Summary\r\n\r\nBody text"; | | 19 | + expect(proposedPlanTitle(markdown)).toBe("Real Title"); | | 20 | + expect(stripDisplayedPlanMarkdown(markdown)).toBe("Body text"); | | 21 | +}); | | 22 | + | | 23 | +it("strips the displayed plan title and summary after leading blank lines", () => { | | 24 | + const markdown = "\r\n# My Plan\r\n\r\n## Summary\r\n\r\nBody text"; | | 25 | + expect(proposedPlanTitle(markdown)).toBe("My Plan"); | | 26 | + expect(stripDisplayedPlanMarkdown(markdown)).toBe("Body text"); | | 27 | +}); |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/proposedPlanText.ts +22-4Lines changed: 22 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | export function proposedPlanTitle(planMarkdown: string): string | null { | 2 | | - const heading = planMarkdown.match(/^\s{0,3}#{1,6}\s+(.+)$/m)?.[1]?.trim(); | 3 | | - return heading && heading.length > 0 ? heading : null; | | 2 | + let fence = ""; | | 3 | + for (const line of planMarkdown.split(/\r?\n/)) { | | 4 | + const [, marker = "", info = ""] = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/) ?? []; | | 5 | + if (fence) { | | 6 | + if (marker[0] === fence[0] && marker.length >= fence.length && !info.trim()) fence = ""; | | 7 | + continue; | | 8 | + } | | 9 | + // CommonMark: a backtick fence's info string cannot contain a backtick. | | 10 | + if (marker && (marker[0] !== "`" || !info.includes("`"))) { | | 11 | + fence = marker; | | 12 | + continue; | | 13 | + } | | 14 | + const heading = line.match(/^ {0,3}#{1,6}[ \t]+(.+)$/)?.[1]?.trim(); | | 15 | + if (heading) return heading; | | 16 | + } | | 17 | + return null; | 4 | 18 | } | 5 | 19 |
| 6 | 20 | export function stripDisplayedPlanMarkdown(planMarkdown: string): string { | 7 | 21 | const lines = planMarkdown.trimEnd().split(/\r?\n/); | 8 | | - let start = lines[0] && /^\s{0,3}#{1,6}\s+/.test(lines[0]) ? 1 : 0; | | 22 | + let start = 0; | | 23 | + while (lines[start]?.trim().length === 0 || /^ {0,3}#{1,6}[ \t]*$/.test(lines[start] ?? "")) { | | 24 | + start += 1; | | 25 | + } | | 26 | + if (/^ {0,3}#{1,6}[ \t]+\S/.test(lines[start] ?? "")) start += 1; | 9 | 27 | while (lines[start]?.trim().length === 0) { | 10 | 28 | start += 1; | 11 | 29 | } | 12 | | - const firstHeadingMatch = lines[start]?.match(/^\s{0,3}#{1,6}\s+(.+)$/); | | 30 | + const firstHeadingMatch = lines[start]?.match(/^ {0,3}#{1,6}[ \t]+(.+)$/); | 13 | 31 | if (firstHeadingMatch?.[1]?.trim().toLowerCase() === "summary") { | 14 | 32 | start += 1; | 15 | 33 | while (lines[start]?.trim().length === 0) { | |
|