Skip to content

Lightpanda: the headless browser designed for AI and automation

markdown: drop empty code spans, keep inline markup out of code

Adrià Arrufat50b6fd37a805
diff --git a/src/browser/RenderTree.zig b/src/browser/RenderTree.zig
index 251f68029..425bfd214 100644
--- a/src/browser/RenderTree.zig
+++ b/src/browser/RenderTree.zig
@@ -420,6 +420,30 @@ pub fn analyzeContent(root: *Node, frame: *Frame) ContentInfo {
return result;
}
+/// The href of the one `<a href>` holding all of root's visible content.
+pub fn soleHref(root: *Node, frame: *Frame) ?[]const u8 {
+ var found: ?[]const u8 = null;
+ var tw = TreeWalker.FullExcludeSelf.init(root, .{});
+ while (tw.next()) |node| {
+ if (isSignificantText(node)) return null;
+ const el = node.is(Element) orelse continue;
+ if (!isVisibleElement(el, frame)) {
+ tw.skipChildren();
+ continue;
+ }
+ switch (el.getTag()) {
+ .img => return null,
+ .a => if (el.getAttributeInterned("href")) |href| {
+ if (found != null) return null;
+ found = href;
+ tw.skipChildren();
+ },
+ else => {},
+ }
+ }
+ return found;
+}
+
const testing = @import("../testing.zig");
test "RenderTree: resolveStrip keeps shell when the content holds the text" {
diff --git a/src/browser/markdown.zig b/src/browser/markdown.zig
index 54a9be99a..54c979276 100644
--- a/src/browser/markdown.zig
+++ b/src/browser/markdown.zig
@@ -125,8 +125,7 @@ const Context = struct {
element: Element.Tag,
block_anchor: struct { href: []const u8, label: ?[]const u8 },
inline_anchor: struct { href: []const u8, standalone: bool },
- // The length of the backtick fence around an inline code span.
- inline_code: usize,
+ inline_code: struct { ticks: usize, href: ?[]const u8 },
};
fn deinit(self: *Context) void {
@@ -140,6 +139,17 @@ const Context = struct {
}
}
+ fn resolveHref(self: *Context, raw: []const u8) []const u8 {
+ const frame = self.frame;
+ return URL.resolve(frame.local_arena, frame.base(), raw, .{ .encoding = frame.charset }) catch raw;
+ }
+
+ fn writeLinkTarget(self: *Context, href: []const u8) !void {
+ try self.writer.writeAll("](");
+ try self.writer.writeAll(href);
+ try self.writer.writeByte(')');
+ }
+
fn render(self: *Context, node: *Node) Error!void {
switch (node._type) {
.document, .document_fragment => try self.open(.init(self.tree.children(node, false)), .none),
@@ -198,6 +208,21 @@ const Context = struct {
try self.ensureNewline();
}
+ // Markdown syntax is literal inside a code span or fence, so inline
+ // markup keeps only its text.
+ if (self.state.pre_node != null or self.state.in_code) {
+ switch (tag) {
+ .a, .b, .strong, .i, .em, .s, .del, .code => return self.open(.init(self.tree.content(el, boxed)), .none),
+ .img => {
+ const alt = el.getAttributeInterned("alt") orelse return;
+ try self.writer.writeAll(alt);
+ self.state.last_char_was_newline = false;
+ return;
+ },
+ else => {},
+ }
+ }
+
switch (tag) {
.h1 => try self.writer.writeAll("# "),
.h2 => try self.writer.writeAll("## "),
@@ -255,9 +280,14 @@ const Context = struct {
self.state.last_char_was_newline = true;
},
.code => {
- if (self.state.pre_node != null or self.state.in_code) {
+ // An empty span would read as a literal pair of backticks.
+ if (!RenderTree.analyzeContent(el.asNode(), self.frame).has_visible) {
return self.open(.init(self.tree.content(el, boxed)), .none);
}
+ // A code span can't hold a link, so a link holding the whole
+ // span is written around it instead.
+ const href = if (RenderTree.soleHref(el.asNode(), self.frame)) |raw| self.resolveHref(raw) else null;
+ if (href != null) try self.writer.writeByte('[');
// Backslash escapes are literal inside a code span, so its
// text is written as-is. The fence has to be longer than any
// run of backticks in the text, and is padded with a space
@@ -269,7 +299,7 @@ const Context = struct {
if (ticks > 1) try self.writer.writeByte(' ');
self.state.in_code = true;
self.state.last_char_was_newline = false;
- return self.open(.init(self.tree.content(el, boxed)), .{ .inline_code = ticks });
+ return self.open(.init(self.tree.content(el, boxed)), .{ .inline_code = .{ .ticks = ticks, .href = href } });
},
.b, .strong => {
try self.writer.writeAll("**");
@@ -325,7 +355,7 @@ const Context = struct {
const info = RenderTree.analyzeContent(el.asNode(), frame);
const label = getAnchorLabel(el);
- const href = URL.resolve(frame.local_arena, frame.base(), href_raw, .{ .encoding = frame.charset }) catch href_raw;
+ const href = self.resolveHref(href_raw);
if (info.has_block) {
return self.open(.init(self.tree.content(el, boxed)), .{ .block_anchor = .{
@@ -376,15 +406,12 @@ const Context = struct {
try self.ensureNewline();
try self.writer.writeByte('[');
try self.writer.writeAll(anchor.label orelse anchor.href);
- try self.writer.writeAll("](");
- try self.writer.writeAll(anchor.href);
- try self.writer.writeAll(")\n");
+ try self.writeLinkTarget(anchor.href);
+ try self.writer.writeByte('\n');
return;
},
.inline_anchor => |anchor| {
- try self.writer.writeAll("](");
- try self.writer.writeAll(anchor.href);
- try self.writer.writeByte(')');
+ try self.writeLinkTarget(anchor.href);
if (anchor.standalone) {
try self.writer.writeByte('\n');
self.state.last_char_was_newline = true;
@@ -393,9 +420,10 @@ const Context = struct {
}
return;
},
- .inline_code => |ticks| {
- if (ticks > 1) try self.writer.writeByte(' ');
- try self.writer.splatByteAll('`', ticks);
+ .inline_code => |code| {
+ if (code.ticks > 1) try self.writer.writeByte(' ');
+ try self.writer.splatByteAll('`', code.ticks);
+ if (code.href) |href| try self.writeLinkTarget(href);
self.state.in_code = false;
self.state.last_char_was_newline = false;
return;
@@ -769,6 +797,34 @@ test "browser.markdown: inline code" {
);
}
+test "browser.markdown: empty inline code" {
+ try testMarkdownHTML("<p>a<code></code>b<code> </code>c</p>",
+ \\
+ \\ab c
+ \\
+ );
+}
+
+test "browser.markdown: links inside inline code" {
+ try testMarkdownHTML("<p>See <code><a href=\"/vec\">Vec</a></code> and <code><a href=\"/vec\">Vec</a>&lt;<a href=\"/t\">T</a>&gt;</code></p>",
+ \\
+ \\See [`Vec`](http://localhost/vec) and `Vec<T>`
+ \\
+ );
+}
+
+test "browser.markdown: inline markup inside code is literal" {
+ try testMarkdownHTML("<p><code><b>x</b> <i>y</i> <img src=\"/e.png\" alt=\"z\"></code></p><pre><a href=\"/f\">f</a>(<em>a</em>)</pre>",
+ \\
+ \\`x y z`
+ \\
+ \\```
+ \\f(a)
+ \\```
+ \\
+ );
+}
+
test "browser.markdown: code fence keeps the language" {
try testMarkdownHTML("<pre><code class=\"hljs language-zig\">const x = 1;</code></pre>",
\\