Lightpanda: the headless browser designed for AI and automation
markdown: write a text/markdown document as-is
Jay Hemnani68dbf42ef3dc
diff --git a/src/TestHTTPServer.zig b/src/TestHTTPServer.zigindex 1d920dbe8..1633cc382 100644--- a/src/TestHTTPServer.zig+++ b/src/TestHTTPServer.zig@@ -166,6 +166,10 @@ fn getContentType(file_path: []const u8) []const u8 {return "application/xhtml+xml";}+ if (std.mem.endsWith(u8, file_path, ".md")) {+ return "text/markdown";+ }+if (std.mem.endsWith(u8, file_path, ".xml")) {// some wpt tests do thisreturn "text/xml";diff --git a/src/browser/markdown.zig b/src/browser/markdown.zigindex 585fc18f2..d4ab05239 100644--- a/src/browser/markdown.zig+++ b/src/browser/markdown.zig@@ -24,6 +24,7 @@ const RenderTree = @import("RenderTree.zig");const StyleManager = @import("StyleManager.zig");const Node = @import("webapi/Node.zig");+const Document = @import("webapi/Document.zig");const Element = @import("webapi/Element.zig");const Slot = @import("webapi/element/html/Slot.zig");@@ -477,8 +478,29 @@ const Context = struct {}};+/// A text/markdown response is loaded as a text document, its body a single+/// `<pre>`. Converting that would fence the whole file, so the document's+/// text, which is the markdown the server sent, is written as-is.+fn markdownSource(node: *Node) ?*Node {+ const doc = node.is(Document) orelse return null;+ if (!std.mem.eql(u8, doc.getContentType(), "text/markdown")) return null;+ const root = doc.getDocumentElement() orelse return null;+ return root.asNode();+}++fn writeSource(source: *Node, opts: Opts, writer: *std.Io.Writer) !void {+ var lw = LimitedWriter.init(writer, opts.max_bytes);+ source.getTextContent(&lw.writer) catch |err| {+ if (!lw.truncated) return err;+ try writer.writeAll(truncation_marker);+ };+}+pub fn dump(state: RenderTree.State, opts: Opts, writer: *std.Io.Writer, frame: *Frame) !void {const node = state.root;+ if (markdownSource(node)) |source| {+ return writeSource(source, opts, writer);+ }if (opts.max_bytes) |limit| {var lw = LimitedWriter.init(writer, limit);var ctx: Context = .{@@ -956,6 +978,21 @@ test "browser.markdown: max_bytes truncates with marker" {try testing.expect(out.len <= 50 + truncation_marker.len);}+test "browser.markdown: a text/markdown document is written as-is" {+ var page = try testing.pageTest("markdown_source.md", .{});+ defer page.close();+ const frame = page.frame().?;+ const source = @embedFile("tests/markdown_source.md");++ var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);+ try dump(.{ .root = frame.window._document.asNode() }, .{}, &aw.writer, frame);+ try testing.expectString(source, aw.written());++ var capped: std.Io.Writer.Allocating = .init(testing.arena_allocator);+ try dump(.{ .root = frame.window._document.asNode() }, .{ .max_bytes = 7 }, &capped.writer, frame);+ try testing.expectString("# Title" ++ truncation_marker, capped.written());+}+// Builds a shadow host <div>, populates its light DOM with `light` and its// (open) shadow tree with `shadow`, then dumps the host. Declarative shadow DOM// parsing isn't implemented, so the shadow tree is attached imperatively.diff --git a/src/browser/tests/markdown_source.md b/src/browser/tests/markdown_source.mdnew file mode 100644index 000000000..69a67e088--- /dev/null+++ b/src/browser/tests/markdown_source.md@@ -0,0 +1,7 @@+# Title++Some *emphasis*, a [link](/x) & a <tag>.++```zig+const x = 1;+```