Skip to content

Commit 13fac54

Browse files
committed
Add docs for streaming parser
1 parent 9df2b4e commit 13fac54

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A TypeScript library for parsing and serializing multiple code files in Markdown
1818
- Numbered Bold Format (`1. **filename.js**`)
1919
- Numbered Backtick Format (`### 1. `filename.js``)
2020

21+
- Streaming parser for real-time code editing views
2122
- Serialize code files back to Markdown in a consistent format
2223
- TypeScript support with full type definitions
2324
- Zero dependencies
@@ -124,6 +125,56 @@ Optional `format` parameter to specify a particular format to parse:
124125

125126
Converts an array of file objects into a Markdown string using the Bold Format.
126127

128+
### StreamingMarkdownParser
129+
130+
A streaming parser that processes Markdown content chunk by chunk, emitting callbacks when file names or code blocks are detected. This is particularly useful for real-time code editing views where users can see LLMs edit specific files as they generate content.
131+
132+
```typescript
133+
import { StreamingMarkdownParser, StreamingParserCallbacks } from "llm-code-format";
134+
135+
// Define callbacks for file name changes and code lines
136+
const callbacks: StreamingParserCallbacks = {
137+
onFileNameChange: (fileName, format) => {
138+
console.log(`File changed to: ${fileName} (${format})`);
139+
// Update UI to show the current file being edited
140+
},
141+
onCodeLine: (line) => {
142+
console.log(`Code line: ${line}`);
143+
// Append the line to the current file's content in the UI
144+
}
145+
};
146+
147+
// Create a new parser instance with the callbacks
148+
const parser = new StreamingMarkdownParser(callbacks);
149+
150+
// Process chunks as they arrive (e.g., from an LLM streaming response)
151+
parser.processChunk("**index.html**\n");
152+
parser.processChunk("```html\n");
153+
parser.processChunk("<h1>Hello World</h1>\n");
154+
parser.processChunk("</html>\n");
155+
parser.processChunk("```\n");
156+
157+
// Flush any remaining content when the stream ends
158+
parser.flushRemaining();
159+
```
160+
161+
#### Methods
162+
163+
- **constructor(callbacks: StreamingParserCallbacks)**: Creates a new parser instance with the specified callbacks.
164+
- **processChunk(chunk: string)**: Processes a chunk of text from the stream.
165+
- **flushRemaining()**: Processes any remaining content in the buffer after the stream has ended.
166+
167+
#### Callback Interface
168+
169+
```typescript
170+
type StreamingParserCallbacks = {
171+
onFileNameChange: (fileName: string, format: string) => void;
172+
onCodeLine: (line: string) => void;
173+
};
174+
```
175+
176+
Currently, the streaming parser only supports the "Bold Format" (`**filename.js**`), but is designed to be extensible for supporting additional formats in the future.
177+
127178
## License
128179

129180
MIT © Curran Kelleher

0 commit comments

Comments
 (0)