Skip to content

Lightpanda: the headless browser designed for AI and automation

markdown: keep escaping `|` in inline code inside a table

Raphael Fakhri2fc3a6e2f9f8
diff --git a/src/browser/markdown.zig b/src/browser/markdown.zig
index 54a9be99a..e87a5ca90 100644
--- a/src/browser/markdown.zig
+++ b/src/browser/markdown.zig
@@ -478,7 +478,7 @@ const Context = struct {
}
if (self.state.in_code) {
- try self.writer.writeAll(word);
+ try self.writeCode(word);
} else {
try self.escape(word);
}
@@ -492,6 +492,20 @@ const Context = struct {
}
}
+ // A GFM table splits a row into cells before it parses code spans, so
+ // a `|` inside a code span in a table still has to be escaped.
+ fn writeCode(self: *Context, text: []const u8) !void {
+ if (!self.state.in_table) return self.writer.writeAll(text);
+ var start: usize = 0;
+ for (text, 0..) |c, i| {
+ if (c != '|') continue;
+ if (i > start) try self.writer.writeAll(text[start..i]);
+ try self.writer.writeAll("\\|");
+ start = i + 1;
+ }
+ if (start < text.len) try self.writer.writeAll(text[start..]);
+ }
+
fn escape(self: *Context, text: []const u8) !void {
var start: usize = 0;
for (text, 0..) |c, i| {
@@ -767,6 +781,16 @@ test "browser.markdown: inline code" {
\\`` a`b `` and ``` ``x`` ```
\\
);
+ try testMarkdownHTML(
+ \\<table><thead><tr><th>Type</th></tr></thead>
+ \\<tbody><tr><td><code>string | null</code></td></tr></tbody></table>
+ ,
+ \\
+ \\| Type |
+ \\|---|
+ \\| `string \| null` |
+ \\
+ );
}
test "browser.markdown: code fence keeps the language" {