markdown: refactor content discovery to use TreeWalker

This commit is contained in:
Adrià Arrufat
2026-03-05 14:36:15 +09:00
parent 3256a57230
commit 26db481d46

View File

@@ -20,6 +20,7 @@ const std = @import("std");
const Page = @import("Page.zig"); const Page = @import("Page.zig");
const URL = @import("URL.zig"); const URL = @import("URL.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const CData = @import("webapi/CData.zig"); const CData = @import("webapi/CData.zig");
const Element = @import("webapi/Element.zig"); const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig"); const Node = @import("webapi/Node.zig");
@@ -114,26 +115,24 @@ fn isAllWhitespace(text: []const u8) bool {
} else true; } else true;
} }
fn hasBlockDescendant(node: *Node) bool { fn hasBlockDescendant(root: *Node) bool {
var it = node.childrenIterator(); var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{});
return while (it.next()) |child| { while (tw.next()) |el| {
if (child.is(Element)) |el| { if (isBlock(el.getTag())) return true;
if (isBlock(el.getTag())) break true;
if (hasBlockDescendant(child)) break true;
} }
} else false; return false;
} }
fn hasVisibleContent(node: *Node) bool { fn hasVisibleContent(root: *Node) bool {
var it = node.childrenIterator(); var tw = TreeWalker.FullExcludeSelf.init(root, .{});
while (it.next()) |child| { while (tw.next()) |node| {
if (isSignificantText(child)) return true; if (isSignificantText(node)) return true;
if (child.is(Element)) |el| { if (node.is(Element)) |el| {
if (!isVisibleElement(el)) continue; if (!isVisibleElement(el)) {
// Images are visible tw.skipChildren();
if (el.getTag() == .img) return true; } else if (el.getTag() == .img) {
// Recursive check return true;
if (hasVisibleContent(child)) return true; }
} }
} }
return false; return false;