diff options
| author | Robert Günzler <r@gnzler.io> | 2026-10-02 10:28:11 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2026-10-02 10:39:48 +0200 |
| commit | 2ccea4e0a128225ab6877aa3b9ffebef9f81e213 (patch) | |
| tree | 5f0cb3e9f22f07ecaf36be975f6c06dc88e92899 | |
| parent | ab3b2cf9e1fe76d1253af6c8cb39c85d2a44f59f (diff) | |
| download | site-2ccea4e0a128225ab6877aa3b9ffebef9f81e213.tar.gz | |
let formatLink handle url fragments
Signed-off-by: Robert Günzler <r@gnzler.io>
| -rw-r--r-- | src/main.zig | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/main.zig b/src/main.zig index 60aa3a0..7a90f95 100644 --- a/src/main.zig +++ b/src/main.zig @@ -361,7 +361,7 @@ const Cli = struct { ); for (back_refs.?) |back_ref| { log.debug("{s}: backref path={s}", .{ target.path, back_ref }); - const back_ref_ = try formatRef(gpa, back_ref, LINK_PREFIX, ""); + const back_ref_ = try formatRef(gpa, back_ref, LINK_PREFIX); defer gpa.free(back_ref_); try buf.writer.print( \\{s} @@ -409,7 +409,7 @@ const Cli = struct { for (index.items) |e| { if (std.mem.eql(u8, std.fs.path.stem(e), std.fs.path.stem(target.path))) continue; log.debug("{s}: index path={s}", .{ target.path, e }); - const e_ = try formatRef(gpa, std.fs.path.stem(e), LINK_PREFIX, ""); + const e_ = try formatRef(gpa, std.fs.path.stem(e), LINK_PREFIX); defer gpa.free(e_); try buf.writer.print( \\<li>{s}</li> @@ -869,12 +869,7 @@ const Builder = struct { } } - const ref_ = try formatRef( - gpa, - ref, - self.options.link_prefix, - if (fragment) |i| ref_str[i..] else "", - ); + const ref_ = try formatRef(gpa, ref_str, self.options.link_prefix); defer gpa.free(ref_); try w.writeAll(ref_); @@ -1013,20 +1008,23 @@ fn formatName(gpa: Allocator, name: []const u8) ![]u8 { return try std.mem.replaceOwned(u8, gpa, name, "_", " "); } -fn formatLink(gpa: Allocator, name: []const u8, prefix: []const u8) ![]u8 { - const link_name = try std.fmt.allocPrint(gpa, "{s}.html", .{name}); +fn formatLink(gpa: Allocator, name_str: []const u8, prefix: []const u8) ![]u8 { + const fragment = std.mem.indexOf(u8, name_str, "#"); + const name = if (fragment) |i| name_str[0..i] else name_str; + + const link_name = try std.fmt.allocPrint(gpa, "{s}.html{s}", .{ name, if (fragment) |i| name_str[i..] else "" }); defer gpa.free(link_name); return try std.fs.path.join(gpa, &.{ prefix, link_name }); } -fn formatRef(gpa: Allocator, name: []const u8, link_prefix: []const u8, fragment: []const u8) ![]u8 { +fn formatRef(gpa: Allocator, name: []const u8, link_prefix: []const u8) ![]u8 { const link = try formatLink(gpa, name, link_prefix); defer gpa.free(link); const text = try formatName(gpa, name); defer gpa.free(text); return try std.fmt.allocPrint(gpa, - \\<a href="{s}{s}">{s}</a> - , .{ link, fragment, text }); + \\<a href="{s}">{s}</a> + , .{ link, text }); } fn targetExists( |