Skip to content
Source
Sign inSign up

proposedPlan.ts

75 lines2.1 KB

1import { stripDisplayedPlanMarkdown, proposedPlanTitle } from "@t3tools/shared/proposedPlanText";
2​
3export function buildCollapsedProposedPlanPreviewMarkdown(
4 planMarkdown: string,
5 options?: {
6 maxLines?: number;
7 },
8): string {
9 const maxLines = options?.maxLines ?? 8;
10 const lines = stripDisplayedPlanMarkdown(planMarkdown)
11 .trimEnd()
12 .split(/\r?\n/)
13 .map((line) => line.trimEnd());
14 const previewLines: string[] = [];
15 let visibleLineCount = 0;
16 let hasMoreContent = false;
17​
18 for (const line of lines) {
19 const isVisibleLine = line.trim().length > 0;
20 if (isVisibleLine && visibleLineCount >= maxLines) {
21 hasMoreContent = true;
22 break;
23 }
24 previewLines.push(line);
25 if (isVisibleLine) {
26 visibleLineCount += 1;
27 }
28 }
29​
30 while (previewLines.length > 0 && previewLines.at(-1)?.trim().length === 0) {
31 previewLines.pop();
32 }
33​
34 if (previewLines.length === 0) {
35 return proposedPlanTitle(planMarkdown) ?? "Plan preview unavailable.";
36 }
37​
38 if (hasMoreContent) {
39 previewLines.push("", "...");
40 }
41​
42 return previewLines.join("\n");
44​
45function sanitizePlanFileSegment(input: string): string {
46 const sanitized = input
47 .toLowerCase()
48 .replace(/[`'".,!?()[\]{}]+/g, "")
49 .replace(/[^a-z0-9]+/g, "-")
50 .replace(/^-+|-+$/g, "");
51 return sanitized.length > 0 ? sanitized : "plan";
53​
54export const PLAN_IMPLEMENTATION_PROMPT_PREFIX = "PLEASE IMPLEMENT THIS PLAN:\n";
55​
56export function buildProposedPlanMarkdownFilename(planMarkdown: string): string {
57 const title = proposedPlanTitle(planMarkdown);
58 return `${sanitizePlanFileSegment(title ?? "plan")}.md`;
60​
61export function normalizePlanMarkdownForExport(planMarkdown: string): string {
62 return `${planMarkdown.trimEnd()}\n`;
64​
65export function downloadPlanAsTextFile(filename: string, contents: string): void {
66 const blob = new Blob([contents], { type: "text/markdown;charset=utf-8" });
67 const url = URL.createObjectURL(blob);
68 const anchor = document.createElement("a");
69 anchor.href = url;
70 anchor.download = filename;
71 anchor.click();
72 window.setTimeout(() => {
73 URL.revokeObjectURL(url);
74 }, 0);