From 4410a692079920376fb7215c7cd4c7debd8c7a15 Mon Sep 17 00:00:00 2001 From: "Simen A. W. Olsen" Date: Fri, 29 May 2026 00:16:48 +0200 Subject: feat: flatten TOC and add kindlegen validation --- src/html.zig | 71 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/html.zig b/src/html.zig index fe7f287..8ec8564 100644 --- a/src/html.zig +++ b/src/html.zig @@ -8,7 +8,8 @@ //! 4. normalizes HTML named entities (e.g. `—`) to numeric references, //! since XML predefines only `& < > " '`, //! 5. removes invalid control characters, -//! 6. extracts the in-page table of contents for use as the EPUB navigation, +//! 6. extracts the in-page table of contents (flattened to one level, since +//! Amazon's kindlegen rejects nested nav TOCs) for the EPUB navigation, //! 7. removes the `#navigation` sidebar (it is `position: fixed`, so it bleeds //! onto every page, and the version-switcher dropdown is dead in an EPUB), //! 8. self-closes void elements (`` -> ``, etc.). @@ -94,48 +95,52 @@ fn extractToc(allocator: std.mem.Allocator, html: []const u8) !?[]u8 { const m = std.mem.indexOf(u8, html, marker) orelse return null; const ul_open = findTagOpen(html, m, "ul") orelse return null; const ul_end = matchingClose(html, ul_open, "ul") orelse return null; - return try rewriteToc(allocator, html[ul_open..ul_end]); + return try flattenToc(allocator, html[ul_open..ul_end]); } -/// Rewrite an extracted TOC list: `