xonecas.io
79113
Ma

mini-coding-agent / commit cf44c76

feat: format assistant markdown

xonecas2026-10-25 cf44c76a6509a8b97827883a365dac3a1cb11128 parent 9306b69

3 files changed

src/tui/format.zig+344−0
@@ -0,0 +1,344 @@
1+const std = @import("std");
2+const render = @import("render.zig");
3+
4+/// The text-level half of the renderer: canonical markers, whitespace, and tables.
5+/// Styling runs afterwards, in highlight.zig, over the bytes produced here.
6+pub const Normalizer = struct {
7+ ordered_indent: ?usize = null,
8+ ordered_next: usize = 1,
9+
10+ pub fn reset(self: *Normalizer) void {
11+ self.ordered_indent = null;
12+ self.ordered_next = 1;
13+ }
14+
15+ /// Canonical form of one markdown line: even indentation, one space after every
16+ /// marker, `-` bullets, sequential ordered runs, `> ` quotes, `---` rules, no
17+ /// trailing whitespace. Fence bodies never reach this.
18+ pub fn line(self: *Normalizer, a: std.mem.Allocator, raw: []const u8) []const u8 {
19+ const text = std.mem.trimEnd(u8, raw, " \t\r");
20+ const ind = indentOf(text);
21+ var rest = text[ind.end..];
22+ if (fenceMarker(rest)) |fence_run| return self.fence(a, ind.cols, rest, fence_run);
23+ if (isRule(rest)) {
24+ self.reset();
25+ return std.fmt.allocPrint(a, "{s}---", .{spaces(a, ind.cols)}) catch "---";
26+ }
27+ var out: std.ArrayList(u8) = .empty;
28+ out.appendSlice(a, spaces(a, ind.cols)) catch {};
29+ while (rest.len > 0 and rest[0] == '>') {
30+ out.appendSlice(a, "> ") catch {};
31+ rest = std.mem.trimStart(u8, rest[1..], " \t");
32+ }
33+ return self.body(a, &out, ind.cols, rest);
34+ }
35+
36+ fn body(self: *Normalizer, a: std.mem.Allocator, out: *std.ArrayList(u8), indent: usize, rest: []const u8) []const u8 {
37+ if (heading(rest)) |h| {
38+ self.reset();
39+ out.appendNTimes(a, '#', h.level) catch {};
40+ if (h.text.len > 0) {
41+ out.append(a, ' ') catch {};
42+ out.appendSlice(a, h.text) catch {};
43+ }
44+ return out.items;
45+ }
46+ if (marker(rest)) |m| switch (m) {
47+ .bullet => {
48+ self.ordered_indent = null;
49+ out.appendSlice(a, "- ") catch {};
50+ out.appendSlice(a, std.mem.trimStart(u8, rest[1..], " \t")) catch {};
51+ },
52+ .ordered => |o| {
53+ const num = if (self.ordered_indent == indent) self.ordered_next else o.num;
54+ self.ordered_indent = indent;
55+ self.ordered_next = num + 1;
56+ out.appendSlice(a, std.fmt.allocPrint(a, "{d}.", .{num}) catch "-") catch {};
57+ out.append(a, ' ') catch {};
58+ out.appendSlice(a, std.mem.trimStart(u8, rest[o.at..], " \t")) catch {};
59+ },
60+ } else {
61+ self.ordered_indent = null;
62+ out.appendSlice(a, rest) catch {};
63+ }
64+ return trimmed(out);
65+ }
66+
67+ fn fence(self: *Normalizer, a: std.mem.Allocator, indent: usize, rest: []const u8, run: []const u8) []const u8 {
68+ self.reset();
69+ var out: std.ArrayList(u8) = .empty;
70+ out.appendSlice(a, spaces(a, indent)) catch {};
71+ out.appendSlice(a, run) catch {};
72+ out.appendSlice(a, std.mem.trim(u8, rest[run.len..], " \t")) catch {};
73+ return out.items;
74+ }
75+};
76+
77+fn spaces(a: std.mem.Allocator, n: usize) []const u8 {
78+ const out = a.alloc(u8, n) catch return "";
79+ @memset(out, ' ');
80+ return out;
81+}
82+
83+fn trimmed(out: *std.ArrayList(u8)) []const u8 {
84+ while (out.items.len > 0 and (out.items[out.items.len - 1] == ' ' or out.items[out.items.len - 1] == '\t'))
85+ out.items.len -= 1;
86+ return out.items;
87+}
88+
89+const Indent = struct { end: usize, cols: usize };
90+
91+/// Leading whitespace, with tabs worth two columns and the depth rounded down to even
92+/// so nested levels line up.
93+fn indentOf(text: []const u8) Indent {
94+ var end: usize = 0;
95+ var cols: usize = 0;
96+ while (end < text.len and (text[end] == ' ' or text[end] == '\t')) : (end += 1)
97+ cols += if (text[end] == '\t') 2 else 1;
98+ return .{ .end = end, .cols = cols & ~@as(usize, 1) };
99+}
100+
101+fn fenceMarker(rest: []const u8) ?[]const u8 {
102+ if (rest.len == 0) return null;
103+ const c = rest[0];
104+ if (c != '`' and c != '~') return null;
105+ var n: usize = 0;
106+ while (n < rest.len and rest[n] == c) n += 1;
107+ return if (n >= 3) rest[0..n] else null;
108+}
109+
110+fn isRule(rest: []const u8) bool {
111+ if (rest.len == 0) return false;
112+ const c = rest[0];
113+ if (c != '-' and c != '*' and c != '_') return false;
114+ var marks: usize = 0;
115+ for (rest) |ch| {
116+ if (ch == c) {
117+ marks += 1;
118+ } else if (ch != ' ' and ch != '\t') {
119+ return false;
120+ }
121+ }
122+ return marks >= 3;
123+}
124+
125+const Heading = struct { level: usize, text: []const u8 };
126+
127+fn heading(rest: []const u8) ?Heading {
128+ var n: usize = 0;
129+ while (n < rest.len and rest[n] == '#') n += 1;
130+ if (n == 0 or n > 6) return null;
131+ if (n < rest.len and rest[n] != ' ' and rest[n] != '\t') return null;
132+ var text = std.mem.trimStart(u8, rest[n..], " \t");
133+ // A closing run of `#`s is decoration; only one that follows a space counts.
134+ var end = text.len;
135+ while (end > 0 and text[end - 1] == '#') end -= 1;
136+ if (end < text.len and (end == 0 or text[end - 1] == ' ' or text[end - 1] == '\t'))
137+ text = std.mem.trimEnd(u8, text[0..end], " \t");
138+ return .{ .level = n, .text = text };
139+}
140+
141+const Marker = union(enum) {
142+ bullet,
143+ ordered: struct { num: usize, at: usize },
144+};
145+
146+/// Byte offset just past the list marker, or null when the line has none.
147+fn marker(rest: []const u8) ?Marker {
148+ if (rest.len == 0) return null;
149+ const c = rest[0];
150+ if (c == '-' or c == '*' or c == '+')
151+ return if (rest.len == 1 or rest[1] == ' ' or rest[1] == '\t') .bullet else null;
152+ var at: usize = 0;
153+ while (at < rest.len and std.ascii.isDigit(rest[at])) at += 1;
154+ if (at == 0 or at >= rest.len) return null;
155+ if (rest[at] != '.' and rest[at] != ')') return null;
156+ if (at + 1 < rest.len and rest[at + 1] != ' ' and rest[at + 1] != '\t') return null;
157+ return .{ .ordered = .{ .num = std.fmt.parseInt(usize, rest[0..at], 10) catch 1, .at = at + 1 } };
158+}
159+
160+const Align = enum { none, left, center, right };
161+
162+fn alignOf(cell: []const u8) Align {
163+ const left = cell.len > 0 and cell[0] == ':';
164+ const right = cell.len > 1 and cell[cell.len - 1] == ':';
165+ if (left and right) return .center;
166+ if (left) return .left;
167+ if (right) return .right;
168+ return .none;
169+}
170+
171+fn splitCells(a: std.mem.Allocator, line: []const u8) []const []const u8 {
172+ const trimmed_line = std.mem.trim(u8, line, " \t\r");
173+ var cells: std.ArrayList([]const u8) = .empty;
174+ var start: usize = 0;
175+ var i: usize = 0;
176+ while (i < trimmed_line.len) : (i += 1) {
177+ if (trimmed_line[i] == '\\' and i + 1 < trimmed_line.len) {
178+ i += 1;
179+ continue;
180+ }
181+ if (trimmed_line[i] == '|') {
182+ cells.append(a, std.mem.trim(u8, trimmed_line[start..i], " \t")) catch {};
183+ start = i + 1;
184+ }
185+ }
186+ cells.append(a, std.mem.trim(u8, trimmed_line[start..], " \t")) catch {};
187+ if (trimmed_line.len > 0 and trimmed_line[0] == '|' and cells.items.len > 0) _ = cells.orderedRemove(0);
188+ if (trimmed_line.len > 1 and trimmed_line[trimmed_line.len - 1] == '|' and cells.items.len > 0) _ = cells.pop();
189+ return cells.items;
190+}
191+
192+/// Cell text with its inline markers removed: what the styling pass will actually paint.
193+fn visible(a: std.mem.Allocator, cell: []const u8) []const u8 {
194+ var out: std.ArrayList(u8) = .empty;
195+ var i: usize = 0;
196+ while (i < cell.len) {
197+ const c = cell[i];
198+ if (c == '\\' and i + 1 < cell.len) {
199+ out.appendSlice(a, cell[i .. i + 2]) catch {};
200+ i += 2;
201+ continue;
202+ }
203+ if (c == '`' or c == '*' or c == '_') {
204+ i += 1;
205+ continue;
206+ }
207+ if (c == '[') {
208+ if (std.mem.indexOfScalarPos(u8, cell, i, ']')) |close| {
209+ if (close + 1 < cell.len and cell[close + 1] == '(') {
210+ if (std.mem.indexOfScalarPos(u8, cell, close + 1, ')')) |end| {
211+ out.appendSlice(a, cell[i + 1 .. close]) catch {};
212+ i = end + 1;
213+ continue;
214+ }
215+ }
216+ }
217+ }
218+ out.append(a, c) catch {};
219+ i += 1;
220+ }
221+ return out.items;
222+}
223+
224+fn cellWidth(a: std.mem.Allocator, cell: []const u8) usize {
225+ return render.displayWidth(visible(a, cell));
226+}
227+
228+/// Delimiter row (`|---|:--:|`) as per-column alignments, or null when the line is not one.
229+fn delimiterRow(a: std.mem.Allocator, line: []const u8) ?[]const Align {
230+ if (std.mem.indexOfScalar(u8, line, '-') == null) return null;
231+ const cells = splitCells(a, line);
232+ if (cells.len == 0) return null;
233+ const aligns = a.alloc(Align, cells.len) catch return null;
234+ for (cells, 0..) |cell, k| {
235+ var body = cell;
236+ if (body.len > 0 and body[0] == ':') body = body[1..];
237+ if (body.len > 0 and body[body.len - 1] == ':') body = body[0 .. body.len - 1];
238+ if (body.len == 0) return null;
239+ for (body) |c| {
240+ if (c != '-') return null;
241+ }
242+ aligns[k] = alignOf(cell);
243+ }
244+ return aligns;
245+}
246+
247+fn tableCandidate(line: []const u8) bool {
248+ const trimmed_line = std.mem.trim(u8, line, " \t\r");
249+ if (trimmed_line.len == 0) return false;
250+ // Tables nested in quotes or lists keep their marker; reflowing them would drop it.
251+ if (trimmed_line[0] == '>' or trimmed_line[0] == '-' or trimmed_line[0] == '*' or trimmed_line[0] == '+') return false;
252+ return std.mem.indexOfScalar(u8, trimmed_line, '|') != null;
253+}
254+
255+fn rowCells(line: []const u8) bool {
256+ const trimmed_line = std.mem.trim(u8, line, " \t\r");
257+ return trimmed_line.len > 0 and std.mem.indexOfScalar(u8, trimmed_line, '|') != null;
258+}
259+
260+fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), cells: []const []const u8, named: usize, widths: []const usize, aligns: []const Align, delimiter: bool) void {
261+ block.append(a, '|') catch {};
262+ for (0..named) |k| {
263+ const cell = if (k < cells.len) cells[k] else "";
264+ const alignment = aligns[k];
265+ block.append(a, ' ') catch {};
266+ if (delimiter) {
267+ block.append(a, if (alignment == .left or alignment == .center) ':' else '-') catch {};
268+ block.appendNTimes(a, '-', widths[k] - 2) catch {};
269+ block.append(a, if (alignment == .right or alignment == .center) ':' else '-') catch {};
270+ } else {
271+ const pad = widths[k] - cellWidth(a, cell);
272+ const before: usize = switch (alignment) {
273+ .right => pad,
274+ .center => pad / 2,
275+ else => 0,
276+ };
277+ block.appendNTimes(a, ' ', before) catch {};
278+ block.appendSlice(a, cell) catch {};
279+ block.appendNTimes(a, ' ', pad - before) catch {};
280+ }
281+ block.appendSlice(a, " |") catch {};
282+ }
283+}
284+
285+fn formatTable(a: std.mem.Allocator, header: []const u8, aligns: []const Align, body: []const []const u8, trailing: bool) ?[]const u8 {
286+ const named = aligns.len;
287+ const rows = a.alloc([]const []const u8, body.len + 1) catch return null;
288+ rows[0] = splitCells(a, header);
289+ if (rows[0].len != named) return null;
290+ for (body, 0..) |line, i| {
291+ const cells = splitCells(a, line);
292+ if (cells.len > named) return null;
293+ rows[i + 1] = cells;
294+ }
295+
296+ const widths = a.alloc(usize, named) catch return null;
297+ @memset(widths, 3);
298+ for (rows) |cells| {
299+ for (cells, 0..) |cell, k| widths[k] = @max(widths[k], cellWidth(a, cell));
300+ }
301+
302+ var block: std.ArrayList(u8) = .empty;
303+ for (rows, 0..) |cells, r| {
304+ if (r > 0) block.append(a, '\n') catch {};
305+ emitRow(a, &block, cells, named, widths, aligns, false);
306+ if (r == 0) {
307+ block.append(a, '\n') catch {};
308+ emitRow(a, &block, &.{}, named, widths, aligns, true);
309+ }
310+ }
311+ if (trailing) block.append(a, '\n') catch {};
312+ return block.items;
313+}
314+
315+pub fn formatTables(a: std.mem.Allocator, text: []const u8) []const u8 {
316+ var lines: std.ArrayList([]const u8) = .empty;
317+ var it = std.mem.splitScalar(u8, text, '\n');
318+ while (it.next()) |line| lines.append(a, line) catch {};
319+
320+ var out: std.ArrayList(u8) = .empty;
321+ var i: usize = 0;
322+ while (i < lines.items.len) {
323+ const line = lines.items[i];
324+ if (i + 1 < lines.items.len and tableCandidate(line)) {
325+ if (delimiterRow(a, lines.items[i + 1])) |aligns| {
326+ var end = i + 2;
327+ while (end < lines.items.len and rowCells(lines.items[end])) end += 1;
328+ const block = formatTable(a, line, aligns, lines.items[i + 2 .. end], end < lines.items.len);
329+ if (block) |b| {
330+ out.appendSlice(a, b) catch {};
331+ i = end;
332+ continue;
333+ }
334+ }
335+ }
336+ out.appendSlice(a, line) catch {};
337+ out.append(a, '\n') catch {};
338+ i += 1;
339+ }
340+ // A single trailing split always leaves one empty entry; drop its newline.
341+ if (out.items.len > 0 and text.len > 0 and text[text.len - 1] != '\n' and out.items[out.items.len - 1] == '\n')
342+ out.items.len -= 1;
343+ return out.items;
344+}
src/tui/highlight.zig+2−152
@@ -1,6 +1,5 @@
11 const std = @import("std");
22 const styles = @import("styles.zig");
3-const render = @import("render.zig");
43
54 const Span = struct {
65 start: u32,
@@ -198,7 +197,8 @@ fn isRule(line: []const u8) bool {
198197
199198 /// Byte offset just past a list or quote marker, or null when the line has none.
200199 fn blockMarker(line: []const u8) ?usize {
201- const at = leadingSpaces(line);
200+ var at: usize = 0;
201+ while (at < line.len and (line[at] == ' ' or line[at] == '\t')) at += 1;
202202 if (at >= line.len) return null;
203203 const c = line[at];
204204 if (c == '>' or c == '-' or c == '*' or c == '+') {
@@ -272,153 +272,3 @@ pub fn diffKind(line: []const u8, in_hunk: bool) DiffKind {
272272 if (line.len > 0 and line[0] == '-') return .del;
273273 return .note;
274274 }
275-
276-const Align = enum { none, left, center, right };
277-
278-fn alignOf(cell: []const u8) Align {
279- const left = cell.len > 0 and cell[0] == ':';
280- const right = cell.len > 1 and cell[cell.len - 1] == ':';
281- if (left and right) return .center;
282- if (left) return .left;
283- if (right) return .right;
284- return .none;
285-}
286-
287-fn splitCells(a: std.mem.Allocator, line: []const u8) []const []const u8 {
288- const trimmed = std.mem.trim(u8, line, " \t\r");
289- var cells: std.ArrayList([]const u8) = .empty;
290- var start: usize = 0;
291- var i: usize = 0;
292- while (i < trimmed.len) : (i += 1) {
293- if (trimmed[i] == '\\' and i + 1 < trimmed.len) {
294- i += 1;
295- continue;
296- }
297- if (trimmed[i] == '|') {
298- cells.append(a, std.mem.trim(u8, trimmed[start..i], " \t")) catch {};
299- start = i + 1;
300- }
301- }
302- cells.append(a, std.mem.trim(u8, trimmed[start..], " \t")) catch {};
303- if (trimmed.len > 0 and trimmed[0] == '|' and cells.items.len > 0) _ = cells.orderedRemove(0);
304- if (trimmed.len > 1 and trimmed[trimmed.len - 1] == '|' and cells.items.len > 0) _ = cells.pop();
305- return cells.items;
306-}
307-
308-/// Delimiter row (`|---|:--:|`) as per-column alignments, or null when the line is not one.
309-fn delimiterRow(a: std.mem.Allocator, line: []const u8) ?[]const Align {
310- if (std.mem.indexOfScalar(u8, line, '-') == null) return null;
311- const cells = splitCells(a, line);
312- if (cells.len == 0) return null;
313- const aligns = a.alloc(Align, cells.len) catch return null;
314- for (cells, 0..) |cell, k| {
315- var body = cell;
316- if (body.len > 0 and body[0] == ':') body = body[1..];
317- if (body.len > 0 and body[body.len - 1] == ':') body = body[0 .. body.len - 1];
318- if (body.len == 0) return null;
319- for (body) |c| {
320- if (c != '-') return null;
321- }
322- aligns[k] = alignOf(cell);
323- }
324- return aligns;
325-}
326-
327-fn tableCandidate(line: []const u8) bool {
328- const trimmed = std.mem.trim(u8, line, " \t\r");
329- if (trimmed.len == 0) return false;
330- // Tables nested in quotes or lists keep their marker; reflowing them would drop it.
331- if (trimmed[0] == '>' or trimmed[0] == '-' or trimmed[0] == '*' or trimmed[0] == '+') return false;
332- return std.mem.indexOfScalar(u8, trimmed, '|') != null;
333-}
334-
335-fn rowCells(line: []const u8) bool {
336- const trimmed = std.mem.trim(u8, line, " \t\r");
337- return trimmed.len > 0 and std.mem.indexOfScalar(u8, trimmed, '|') != null;
338-}
339-
340-fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), cells: []const []const u8, named: usize, widths: []const usize, aligns: []const Align, delimiter: bool) void {
341- block.append(a, '|') catch {};
342- for (0..named) |k| {
343- const cell = if (k < cells.len) cells[k] else "";
344- const alignment = aligns[k];
345- block.append(a, ' ') catch {};
346- if (delimiter) {
347- block.append(a, if (alignment == .left or alignment == .center) ':' else '-') catch {};
348- block.appendNTimes(a, '-', widths[k] - 2) catch {};
349- block.append(a, if (alignment == .right or alignment == .center) ':' else '-') catch {};
350- } else {
351- const pad = widths[k] - render.displayWidth(cell);
352- const before: usize = switch (alignment) {
353- .right => pad,
354- .center => pad / 2,
355- else => 0,
356- };
357- block.appendNTimes(a, ' ', before) catch {};
358- block.appendSlice(a, cell) catch {};
359- block.appendNTimes(a, ' ', pad - before) catch {};
360- }
361- block.appendSlice(a, " |") catch {};
362- }
363-}
364-
365-fn formatTable(a: std.mem.Allocator, header: []const u8, aligns: []const Align, body: []const []const u8, trailing: bool) ?[]const u8 {
366- const named = aligns.len;
367- const rows = a.alloc([]const []const u8, body.len + 1) catch return null;
368- rows[0] = splitCells(a, header);
369- if (rows[0].len != named) return null;
370- for (body, 0..) |line, i| {
371- const cells = splitCells(a, line);
372- if (cells.len > named) return null;
373- rows[i + 1] = cells;
374- }
375-
376- const widths = a.alloc(usize, named) catch return null;
377- @memset(widths, 3);
378- for (rows) |cells| {
379- for (cells, 0..) |cell, k| widths[k] = @max(widths[k], render.displayWidth(cell));
380- }
381-
382- var block: std.ArrayList(u8) = .empty;
383- for (rows, 0..) |cells, r| {
384- if (r > 0) block.append(a, '\n') catch {};
385- emitRow(a, &block, cells, named, widths, aligns, false);
386- if (r == 0) {
387- block.append(a, '\n') catch {};
388- emitRow(a, &block, &.{}, named, widths, aligns, true);
389- }
390- }
391- if (trailing) block.append(a, '\n') catch {};
392- return block.items;
393-}
394-
395-pub fn formatTables(a: std.mem.Allocator, text: []const u8) []const u8 {
396- var lines: std.ArrayList([]const u8) = .empty;
397- var it = std.mem.splitScalar(u8, text, '\n');
398- while (it.next()) |line| lines.append(a, line) catch {};
399-
400- var out: std.ArrayList(u8) = .empty;
401- var i: usize = 0;
402- while (i < lines.items.len) {
403- const line = lines.items[i];
404- if (i + 1 < lines.items.len and tableCandidate(line)) {
405- if (delimiterRow(a, lines.items[i + 1])) |aligns| {
406- var end = i + 2;
407- while (end < lines.items.len and rowCells(lines.items[end])) end += 1;
408- const block = formatTable(a, line, aligns, lines.items[i + 2 .. end], end < lines.items.len);
409- if (block) |b| {
410- out.appendSlice(a, b) catch {};
411- i = end;
412- continue;
413- }
414- }
415- }
416- out.appendSlice(a, line) catch {};
417- out.append(a, '\n') catch {};
418- i += 1;
419- }
420- // A single trailing split always leaves one empty entry; drop its newline.
421- if (out.items.len > 0 and text.len > 0 and text[text.len - 1] != '\n' and out.items[out.items.len - 1] == '\n')
422- out.items.len -= 1;
423- return out.items;
424-}
src/tui/stream.zig+11−7
@@ -1,4 +1,5 @@
11 const std = @import("std");
2+const format = @import("format.zig");
23 const highlight = @import("highlight.zig");
34 const render = @import("render.zig");
45 const text = @import("../text.zig");
@@ -36,6 +37,7 @@ pub const MarkdownStream = struct {
3637 rest: std.ArrayList(u8) = .empty,
3738 fence: ?struct { marker: []const u8, lines: std.ArrayList([]const u8) } = null,
3839 prose: std.ArrayList([]const u8) = .empty,
40+ norm: format.Normalizer = .{},
3941
4042 pub fn init(backing: std.mem.Allocator) MarkdownStream {
4143 return .{ .arena = std.heap.ArenaAllocator.init(backing) };
@@ -62,15 +64,16 @@ pub const MarkdownStream = struct {
6264 return out.items;
6365 }
6466
65- fn commit(self: *MarkdownStream, out_a: std.mem.Allocator, line: []const u8, out: *std.ArrayList(BodyLine)) void {
67+ fn commit(self: *MarkdownStream, out_a: std.mem.Allocator, raw: []const u8, out: *std.ArrayList(BodyLine)) void {
6668 if (self.fence) |*f| {
67- if (isFenceClose(line, f.marker)) {
68- self.releaseFence(out_a, line, out);
69+ if (isFenceClose(raw, f.marker)) {
70+ self.releaseFence(out_a, self.norm.line(self.a, raw), out);
6971 } else {
70- f.lines.append(self.a, line) catch {};
72+ f.lines.append(self.a, raw) catch {};
7173 }
7274 return;
7375 }
76+ const line = self.norm.line(self.a, raw);
7477 if (isFenceOpen(line)) |marker| {
7578 self.releaseProse(out_a, "", out);
7679 var fl: std.ArrayList([]const u8) = .empty;
@@ -80,7 +83,7 @@ pub const MarkdownStream = struct {
8083 }
8184 if (std.mem.trim(u8, line, " \t\r").len == 0) {
8285 self.releaseProse(out_a, "", out);
83- out.append(out_a, .{ .text = line }) catch {};
86+ out.append(out_a, .{ .text = "" }) catch {};
8487 return;
8588 }
8689 self.prose.append(self.a, line) catch {};
@@ -92,7 +95,7 @@ pub const MarkdownStream = struct {
9295 if (tail.len > 0) prose.append(self.a, tail) catch {};
9396 if (prose.items.len == 0) return;
9497 const joined = std.fmt.allocPrint(out_a, "{s}\n", .{text.join(out_a, prose.items, "\n") catch ""}) catch "";
95- emitHighlighted(out_a, highlight.highlightMarkdown(out_a, highlight.formatTables(out_a, joined)), out);
98+ emitHighlighted(out_a, highlight.highlightMarkdown(out_a, format.formatTables(out_a, joined)), out);
9699 }
97100
98101 fn releaseFence(self: *MarkdownStream, out_a: std.mem.Allocator, closing: ?[]const u8, out: *std.ArrayList(BodyLine)) void {
@@ -121,7 +124,7 @@ pub const MarkdownStream = struct {
121124 self.releaseFence(out_a, null, &out);
122125 if (self.rest.items.len > 0) out.append(out_a, .{ .text = highlight.highlightMarkdown(out_a, self.rest.items) }) catch {};
123126 } else {
124- self.releaseProse(out_a, self.rest.items, &out);
127+ self.releaseProse(out_a, self.norm.line(self.a, self.rest.items), &out);
125128 }
126129 self.reset();
127130 return out.items;
@@ -131,6 +134,7 @@ pub const MarkdownStream = struct {
131134 self.rest = .empty;
132135 self.fence = null;
133136 self.prose = .empty;
137+ self.norm.reset();
134138 _ = self.arena.reset(.retain_capacity);
135139 self.a = self.arena.allocator();
136140 }