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

proposedPlan.ts

79 lines2.2 KB

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