aboutsummaryrefslogtreecommitdiffstats
path: root/src/html.zig
diff options
context:
space:
mode:
authorSimen A. W. Olsen <hello@simenandre.no>2026-05-29 00:06:48 +0200
committerSimen A. W. Olsen <hello@simenandre.no>2026-05-29 00:06:48 +0200
commit0806fecb47f5a2a26e73ca8b6e28c08f9945ef75 (patch)
tree824a12cbcba1b0c2ad704e7bd64e25fa2389f34b /src/html.zig
parent330a0c896970d99320529a47f186c831a6ecc4a0 (diff)
downloadziglang-docs-epub-0806fecb47f5a2a26e73ca8b6e28c08f9945ef75.tar.gz
ziglang-docs-epub-0806fecb47f5a2a26e73ca8b6e28c08f9945ef75.zip
fix problems after testing with epubcheck
Diffstat (limited to 'src/html.zig')
-rw-r--r--src/html.zig13
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);