Skip to content

Commit e6019f4

Browse files
jonludlamclaude
andcommitted
Fix URL remapping for page references
Page links were not being remapped because the remap prefix check only matched against the directory path. For modules, the module name is part of the directory (e.g., prefix/otherpkg/Otherlib/index.html), so it matched. But for pages, the page name is in the filename (e.g., prefix/otherpkg/otherpage.html), leaving only prefix/otherpkg in the directory - which didn't match prefix/otherpkg/ due to the trailing slash. Fix by including the filename in the path used for prefix matching. Also normalize remap entries at config creation time (ensure trailing slashes) rather than on each lookup. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent d8460cd commit e6019f4

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

src/html/config.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ type t = {
1515
home_breadcrumb : string option;
1616
}
1717

18+
let ensure_trailing_slash s =
19+
if Astring.String.is_suffix ~affix:"/" s then s else s ^ "/"
20+
1821
let v ?(search_result = false) ?theme_uri ?support_uri ?(search_uris = [])
1922
~semantic_uris ~indent ~flat ~open_details ~as_json ~remap ?home_breadcrumb
2023
() =
24+
let remap =
25+
List.map
26+
(fun (prefix, replacement) ->
27+
(ensure_trailing_slash prefix, ensure_trailing_slash replacement))
28+
remap
29+
in
2130
{
2231
semantic_uris;
2332
indent;

src/html/link.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ module Path = struct
1111

1212
let is_leaf_page url = url.Url.Path.kind = `LeafPage
1313

14-
let remap config f =
15-
let l = String.concat "/" f in
14+
let remap config dir file =
15+
let path = String.concat "/" dir ^ "/" ^ file in
1616
let remaps =
1717
List.filter
18-
(fun (prefix, _replacement) -> Astring.String.is_prefix ~affix:prefix l)
18+
(fun (prefix, _) -> Astring.String.is_prefix ~affix:prefix path)
1919
(Config.remap config)
2020
in
2121
let remaps =
@@ -26,9 +26,11 @@ module Path = struct
2626
match remaps with
2727
| [] -> None
2828
| (prefix, replacement) :: _ ->
29-
let len = String.length prefix in
30-
let l = String.sub l len (String.length l - len) in
31-
Some (replacement ^ l)
29+
let remainder =
30+
String.sub path (String.length prefix)
31+
(String.length path - String.length prefix)
32+
in
33+
Some (replacement ^ remainder)
3234

3335
let get_dir_and_file ~config url =
3436
let l = Url.Path.to_list url in
@@ -52,9 +54,9 @@ module Path = struct
5254

5355
let for_linking ~config url =
5456
let dir, file = get_dir_and_file ~config url in
55-
match remap config dir with
57+
match remap config dir file with
5658
| None -> Relative (dir, file)
57-
| Some x -> Absolute (x ^ "/" ^ file)
59+
| Some url -> Absolute url
5860

5961
let as_filename ~config (url : Url.Path.t) =
6062
let dir, file = get_dir_and_file ~config url in
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{0 Other Page}
2+
3+
This is documentation from the other package.

test/integration/remap.t/run.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
$ ocamlc -c -bin-annot otherlib.mli
22
$ ocamlc -c -bin-annot test.mli
33
$ odoc compile --parent-id prefix/otherpkg --output-dir _odoc otherlib.cmti
4+
$ odoc compile --parent-id prefix/otherpkg --output-dir _odoc otherpage.mld
45
$ odoc compile --parent-id prefix/mypkg --output-dir _odoc test.cmti
56
$ odoc link _odoc/prefix/otherpkg/otherlib.odoc
7+
$ odoc link _odoc/prefix/otherpkg/page-otherpage.odoc
68
$ odoc link -I _odoc/prefix/otherpkg _odoc/prefix/mypkg/test.odoc
79

810
We should be able to remap the links to one of the packages:
@@ -14,6 +16,12 @@ We should be able to remap the links to one of the packages:
1416
_html/prefix/mypkg/Test/index.html: <a href="../../otherpkg/Otherlib/index.html#type-t">Otherlib.t</a>
1517
_html2/prefix/mypkg/Test/index.html: href="https://mysite.org/p/otherpkg/1.2.3/Otherlib/index.html#type-t">
1618

19+
Page links should also be remapped:
20+
21+
$ grep otherpage.html _html/prefix/mypkg/Test/index.html _html2/prefix/mypkg/Test/index.html
22+
_html/prefix/mypkg/Test/index.html: <a href="../../otherpkg/otherpage.html"><code>otherpage</code></a>
23+
_html2/prefix/mypkg/Test/index.html: <a href="https://mysite.org/p/otherpkg/1.2.3/otherpage.html">
24+
1725
This shouldn't stop us from outputting the remapped package though, and the following should complete without error
1826

1927
$ odoc html-generate -o _html3 _odoc/prefix/otherpkg/otherlib.odocl -R prefix/otherpkg/:https://mysite.org/p/otherpkg/1.2.3/

test/integration/remap.t/test.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
type t = Otherlib.t
22

3+
(** See the {!page-otherpage} for more info. *)
4+

0 commit comments

Comments
 (0)