xonecas.io
6533
Yo

yoctomarkdown / commit 1ea9e5d

fix: resolve streaming chunk updates and add types export

xonecas2026-03-15 1ea9e5dd588031a93e79145aa8d16d82dfb2f16e parent 22efbef

3 files changed

package.json+8−0
@@ -30,5 +30,13 @@
3030 "format": "prettier --write .",
3131 "format:check": "prettier --check .",
3232 "test": "bun test"
33+ },
34+ "types": "./src/index.ts",
35+ "exports": {
36+ ".": {
37+ "types": "./src/index.ts",
38+ "import": "./src/index.ts",
39+ "default": "./src/index.ts"
40+ }
3341 }
3442 }
src/index.ts+24−9
@@ -83,17 +83,17 @@ export function createHighlighter(options?: Options): Highlighter {
8383 return result;
8484 }
8585
86- function parseLine(line: string, _isLastLine: boolean): string {
86+ function parseLine(line: string, isPartial: boolean): string {
8787 if (inCodeBlock) {
8888 if (line.trim().startsWith("```")) {
89- inCodeBlock = false;
89+ if (!isPartial) inCodeBlock = false;
9090 return theme.codeBlock(line);
9191 }
9292 return theme.codeBlock(line);
9393 }
9494
9595 if (line.trim().startsWith("```")) {
96- inCodeBlock = true;
96+ if (!isPartial) inCodeBlock = true;
9797 return theme.codeBlock(line);
9898 }
9999
@@ -127,6 +127,8 @@ export function createHighlighter(options?: Options): Highlighter {
127127 return parseInline(line);
128128 }
129129
130+ let hasPartial = false;
131+
130132 return {
131133 write(chunk: string | Uint8Array): string {
132134 const text =
@@ -136,16 +138,29 @@ export function createHighlighter(options?: Options): Highlighter {
136138 const lines = buffer.split("\n");
137139 buffer = lines.pop() ?? "";
138140
139- return (
140- lines.map((line) => parseLine(line, false)).join("\n") +
141- (lines.length > 0 ? "\n" : "")
142- );
141+ let out = "";
142+ if (hasPartial) {
143+ out += "\x1b[2K\r";
144+ }
145+
146+ if (lines.length > 0) {
147+ out += `${lines.map((line) => parseLine(line, false)).join("\n")}\n`;
148+ }
149+
150+ if (buffer.length > 0) {
151+ out += parseLine(buffer, true);
152+ hasPartial = true;
153+ } else {
154+ hasPartial = false;
155+ }
156+
157+ return out;
143158 },
144159 end(): string {
145160 if (buffer.length > 0) {
146- const out = parseLine(buffer, true);
161+ const out = parseLine(buffer, false);
147162 buffer = "";
148- return out;
163+ return (hasPartial ? "\x1b[2K\r" : "") + out;
149164 }
150165 return "";
151166 },
tests/index.test.ts+8−4
@@ -32,6 +32,12 @@ describe("Programmatic Usage: highlightSync", () => {
3232 });
3333 });
3434
35+function expectStreamMatch(res: string, markdown: string) {
36+ const expected = highlightSync(markdown, { theme: "none" });
37+ // biome-ignore lint/suspicious/noControlCharactersInRegex: clear line ansi escape
38+ expect(res.replace(/[^\n]*\x1b\[2K\r/g, "")).toBe(expected);
39+}
40+
3541 describe("Programmatic Usage: createHighlighter (Streaming)", () => {
3642 test("should handle chunks without breaking output", () => {
3743 const highlighter = createHighlighter({ theme: "none" });
@@ -55,8 +61,7 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
5561 }
5662 res += highlighter.end();
5763
58- const expected = highlightSync(markdown, { theme: "none" });
59- expect(res).toBe(expected);
64+ expectStreamMatch(res, markdown);
6065 });
6166
6267 test("should handle random chunk sizes", () => {
@@ -74,7 +79,6 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
7479 }
7580 res += highlighter.end();
7681
77- const expected = highlightSync(markdown, { theme: "none" });
78- expect(res).toBe(expected);
82+ expectStreamMatch(res, markdown);
7983 });
8084 });