yoctomarkdown 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +9 -1
- package/src/index.ts +24 -9
- package/tests/index.test.ts +8 -4
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yoctomarkdown",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"module": "src/index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"devDependencies": {
|
|
@@ -30,5 +30,13 @@
|
|
|
30
30
|
"format": "prettier --write .",
|
|
31
31
|
"format:check": "prettier --check .",
|
|
32
32
|
"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
|
+
}
|
|
33
41
|
}
|
|
34
42
|
}
|
package/src/index.ts
CHANGED
|
@@ -83,17 +83,17 @@ export function createHighlighter(options?: Options): Highlighter {
|
|
|
83
83
|
return result;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
function parseLine(line: string,
|
|
86
|
+
function parseLine(line: string, isPartial: boolean): string {
|
|
87
87
|
if (inCodeBlock) {
|
|
88
88
|
if (line.trim().startsWith("```")) {
|
|
89
|
-
inCodeBlock = false;
|
|
89
|
+
if (!isPartial) inCodeBlock = false;
|
|
90
90
|
return theme.codeBlock(line);
|
|
91
91
|
}
|
|
92
92
|
return theme.codeBlock(line);
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
if (line.trim().startsWith("```")) {
|
|
96
|
-
inCodeBlock = true;
|
|
96
|
+
if (!isPartial) inCodeBlock = true;
|
|
97
97
|
return theme.codeBlock(line);
|
|
98
98
|
}
|
|
99
99
|
|
|
@@ -127,6 +127,8 @@ export function createHighlighter(options?: Options): Highlighter {
|
|
|
127
127
|
return parseInline(line);
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
let hasPartial = false;
|
|
131
|
+
|
|
130
132
|
return {
|
|
131
133
|
write(chunk: string | Uint8Array): string {
|
|
132
134
|
const text =
|
|
@@ -136,16 +138,29 @@ export function createHighlighter(options?: Options): Highlighter {
|
|
|
136
138
|
const lines = buffer.split("\n");
|
|
137
139
|
buffer = lines.pop() ?? "";
|
|
138
140
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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;
|
|
143
158
|
},
|
|
144
159
|
end(): string {
|
|
145
160
|
if (buffer.length > 0) {
|
|
146
|
-
const out = parseLine(buffer,
|
|
161
|
+
const out = parseLine(buffer, false);
|
|
147
162
|
buffer = "";
|
|
148
|
-
return out;
|
|
163
|
+
return (hasPartial ? "\x1b[2K\r" : "") + out;
|
|
149
164
|
}
|
|
150
165
|
return "";
|
|
151
166
|
},
|
package/tests/index.test.ts
CHANGED
|
@@ -32,6 +32,12 @@ describe("Programmatic Usage: highlightSync", () => {
|
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
34
|
|
|
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
|
+
|
|
35
41
|
describe("Programmatic Usage: createHighlighter (Streaming)", () => {
|
|
36
42
|
test("should handle chunks without breaking output", () => {
|
|
37
43
|
const highlighter = createHighlighter({ theme: "none" });
|
|
@@ -55,8 +61,7 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
|
|
|
55
61
|
}
|
|
56
62
|
res += highlighter.end();
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
expect(res).toBe(expected);
|
|
64
|
+
expectStreamMatch(res, markdown);
|
|
60
65
|
});
|
|
61
66
|
|
|
62
67
|
test("should handle random chunk sizes", () => {
|
|
@@ -74,7 +79,6 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
|
|
|
74
79
|
}
|
|
75
80
|
res += highlighter.end();
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
expect(res).toBe(expected);
|
|
82
|
+
expectStreamMatch(res, markdown);
|
|
79
83
|
});
|
|
80
84
|
});
|