Elffuss BraidHub

kikocis/continue public

main · 3.9 KiBRawPermalink
1/**
2 * Utility functions for working with markdown files and code blocks
3 */
4
5/**
6 * Determines if a code block header indicates markdown content
7 */
8export function headerIsMarkdown(header: string): boolean {
9 return (
10 header === "md" ||
11 header === "markdown" ||
12 header === "gfm" ||
13 header === "github-markdown" ||
14 header.includes(" md") ||
15 header.includes(" markdown") ||
16 header.includes(" gfm") ||
17 header.includes(" github-markdown") ||
18 header.split(" ")[0]?.split(".").pop() === "md" ||
19 header.split(" ")[0]?.split(".").pop() === "markdown" ||
20 header.split(" ")[0]?.split(".").pop() === "gfm"
21 );
22}
23
24/**
25 * Determines if a file is a markdown file based on its filepath.
26 */
27export function isMarkdownFile(filepath?: string): boolean {
28 if (!filepath) {
29 return false;
30 }
31
32 const ext = filepath.split(".").pop()?.toLowerCase() || "";
33 return ["md", "markdown", "gfm"].includes(ext);
34}
35
36/**
37 * State tracker for markdown block analysis to avoid recomputing on each call.
38 * Optimized to handle nested markdown code blocks.
39 */
40export class MarkdownBlockStateTracker {
41 protected trimmedLines: string[];
42 protected bareBacktickPositions: number[];
43 private markdownNestCount: number = 0;
44 private lastProcessedIndex: number = -1;
45
46 constructor(allLines: string[]) {
47 this.trimmedLines = allLines.map((l) => l.trim());
48 // Pre-compute positions of all bare backtick lines for faster lookup
49 this.bareBacktickPositions = [];
50 for (let i = 0; i < this.trimmedLines.length; i++) {
51 if (this.trimmedLines[i].match(/^`+$/)) {
52 this.bareBacktickPositions.push(i);
53 }
54 }
55 }
56
57 /**
58 * Determines if we should stop at the given markdown block position.
59 * Maintains state across calls to avoid redundant computation.
60 */
61 shouldStopAtPosition(currentIndex: number): boolean {
62 if (this.trimmedLines[currentIndex] !== "```") {
63 return false;
64 }
65
66 // Process any lines we haven't seen yet up to currentIndex
67 for (let j = this.lastProcessedIndex + 1; j <= currentIndex; j++) {
68 const currentLine = this.trimmedLines[j];
69
70 if (this.markdownNestCount > 0) {
71 // Inside a markdown block
72 if (currentLine.match(/^`+$/)) {
73 // Found bare backticks - check if this is the last one
74 if (j === currentIndex) {
75 const remainingBareBackticks =
76 this.getRemainingBareBackticksAfter(j);
77 if (remainingBareBackticks === 0) {
78 this.markdownNestCount = 0;
79 this.lastProcessedIndex = j;
80 return true;
81 }
82 }
83 } else if (currentLine.startsWith("```")) {
84 // Going into a nested codeblock
85 this.markdownNestCount++;
86 }
87 } else {
88 // Not inside a markdown block yet
89 if (currentLine.startsWith("```")) {
90 const header = currentLine.replaceAll("`", "");
91 if (headerIsMarkdown(header)) {
92 this.markdownNestCount = 1;
93 }
94 }
95 }
96 }
97
98 this.lastProcessedIndex = currentIndex;
99 return false;
100 }
101
102 /**
103 * Efficiently determines if there are remaining bare backticks after the given position.
104 */
105 getRemainingBareBackticksAfter(currentIndex: number): number {
106 return this.bareBacktickPositions.filter((pos) => pos > currentIndex)
107 .length;
108 }
109
110 /**
111 * Checks if the line at the given index is a bare backtick line.
112 */
113 isBareBacktickLine(index: number): boolean {
114 return this.bareBacktickPositions.includes(index);
115 }
116
117 /**
118 * Gets the trimmed lines array.
119 */
120 getTrimmedLines(): string[] {
121 return this.trimmedLines;
122 }
123}
124
125/**
126 * Collects all lines from a LineStream into an array for analysis.
127 */
128export async function collectAllLines<T>(
129 stream: AsyncGenerator<T>,
130): Promise<T[]> {
131 const allLines: T[] = [];
132 for await (const line of stream) {
133 allLines.push(line);
134 }
135 return allLines;
136}