From 0806fecb47f5a2a26e73ca8b6e28c08f9945ef75 Mon Sep 17 00:00:00 2001 From: "Simen A. W. Olsen" Date: Fri, 29 May 2026 00:06:48 +0200 Subject: fix problems after testing with epubcheck --- src/html.zig | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/html.zig b/src/html.zig index ec9ecfd..fe7f287 100644 --- a/src/html.zig +++ b/src/html.zig @@ -63,10 +63,16 @@ pub fn transform(allocator: std.mem.Allocator, page: []const u8) !Document { const no_sidebar = try removeNavigation(allocator, escaped); defer allocator.free(no_sidebar); + // Section headings link back to their entry in the removed sidebar + // (`href="#toc-X"`); redirect those to the matching anchors now in nav.xhtml, + // otherwise they are dangling fragments (epubcheck RSC-012, breaks Kindle). + const relinked = try std.mem.replaceOwned(u8, allocator, no_sidebar, "href=\"#toc-", "href=\"nav.xhtml#toc-"); + defer allocator.free(relinked); + var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try out.writer.writeAll(xml_decl); - try writeWithFixups(&out.writer, no_sidebar); + try writeWithFixups(&out.writer, relinked); const content = try out.toOwnedSlice(); assert(std.mem.startsWith(u8, content, "
  • Intro" ++ "
  • " ++ "" ++ - "

    Intro

    body

    " ++ + "

    Intro

    body

    " ++ ""; var doc = try transform(gpa, page); @@ -549,6 +555,9 @@ test "transform extracts TOC and removes the navigation sidebar" { try std.testing.expect(std.mem.indexOf(u8, doc.content, "
    ") != null); try std.testing.expect(std.mem.indexOf(u8, doc.content, "

    ") != null); try std.testing.expect(isWellFormed(doc.content)); + // Back-to-contents link is redirected to the nav document (no dangling fragment). + try std.testing.expect(std.mem.indexOf(u8, doc.content, "href=\"nav.xhtml#toc-Intro\"") != null); + try std.testing.expect(std.mem.indexOf(u8, doc.content, "href=\"#toc-Intro\"") == null); // TOC extracted, ul->ol, links rewritten into the content document. try std.testing.expect(doc.toc != null); -- cgit v1.2.3