diff options
Diffstat (limited to 'src/html.zig')
| -rw-r--r-- | src/html.zig | 13 |
1 files changed, 11 insertions, 2 deletions
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, "<?xml")); @@ -536,7 +542,7 @@ test "transform extracts TOC and removes the navigation sidebar" { "<ul><li><a id=\"toc-Intro\" href=\"#Intro\">Intro</a>" ++ "<ul><li><a href=\"#Sub\">Sub</a></li></ul></li></ul>" ++ "</nav></div>" ++ - "<main id=\"contents\"><h2 id=\"Intro\">Intro</h2><p>body</p></main>" ++ + "<main id=\"contents\"><h2 id=\"Intro\"><a href=\"#toc-Intro\">Intro</a></h2><p>body</p></main>" ++ "</div></body></html>"; 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, "<main id=\"contents\">") != null); try std.testing.expect(std.mem.indexOf(u8, doc.content, "<h2 id=\"Intro\">") != 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); |
