feat(dump): add semantic_tree and semantic_tree_text formats

Adds support for dumping the semantic tree in JSON or text format
via the --dump option. Updates the Config enum and usage help.
This commit is contained in:
Adrià Arrufat
2026-03-09 18:23:52 +09:00
parent c77cb317c4
commit 3c97332fd8
2 changed files with 22 additions and 1 deletions

View File

@@ -192,6 +192,8 @@ pub const DumpFormat = enum {
html, html,
markdown, markdown,
wpt, wpt,
semantic_tree,
semantic_tree_text,
}; };
pub const Fetch = struct { pub const Fetch = struct {
@@ -338,7 +340,7 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\ \\
\\Options: \\Options:
\\--dump Dumps document to stdout. \\--dump Dumps document to stdout.
\\ Argument must be 'html' or 'markdown'. \\ Argument must be 'html', 'markdown', 'semantic_tree', or 'semantic_tree_text'.
\\ Defaults to no dump. \\ Defaults to no dump.
\\ \\
\\--strip_mode Comma separated list of tag groups to remove from dump \\--strip_mode Comma separated list of tag groups to remove from dump

View File

@@ -32,6 +32,7 @@ pub const js = @import("browser/js/js.zig");
pub const dump = @import("browser/dump.zig"); pub const dump = @import("browser/dump.zig");
pub const markdown = @import("browser/markdown.zig"); pub const markdown = @import("browser/markdown.zig");
pub const SemanticTree = @import("SemanticTree.zig"); pub const SemanticTree = @import("SemanticTree.zig");
pub const CDPNode = @import("cdp/Node.zig");
pub const mcp = @import("mcp.zig"); pub const mcp = @import("mcp.zig");
pub const build_config = @import("build_config"); pub const build_config = @import("build_config");
pub const crash_handler = @import("crash_handler.zig"); pub const crash_handler = @import("crash_handler.zig");
@@ -108,6 +109,24 @@ pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
switch (mode) { switch (mode) {
.html => try dump.root(page.window._document, opts.dump, writer, page), .html => try dump.root(page.window._document, opts.dump, writer, page),
.markdown => try markdown.dump(page.window._document.asNode(), .{}, writer, page), .markdown => try markdown.dump(page.window._document.asNode(), .{}, writer, page),
.semantic_tree, .semantic_tree_text => {
var registry = CDPNode.Registry.init(app.allocator);
defer registry.deinit();
const st = SemanticTree{
.dom_node = page.window._document.asNode(),
.registry = &registry,
.page = page,
.arena = page.call_arena,
.prune = true,
};
if (mode == .semantic_tree) {
try std.json.Stringify.value(st, .{}, writer);
} else {
try st.textStringify(writer);
}
},
.wpt => try dumpWPT(page, writer), .wpt => try dumpWPT(page, writer),
} }
} }