Skip to content

Commit 4d5d7c4

Browse files
committed
Sidebar: better default order
1 parent d9d70b7 commit 4d5d7c4

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/index/skeleton_of.ml

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,41 @@ module ModuleName = Odoc_model.Names.ModuleName
88

99
type t = Entry.t Tree.t
1010

11-
let compare_entry (e1 : Entry.t) (e2 : Entry.t) =
12-
let int_of_kind (kind : Entry.kind) =
13-
match kind with
14-
| Page _ -> -10
15-
| Dir -> 0
11+
let compare_entry (t1 : t) (t2 : t) =
12+
let by_kind (t : t) =
13+
match t.node.kind with
14+
| Page _ when List.is_empty t.children -> -10
15+
| Page _ | Dir -> 0
1616
| Module _ -> 10
1717
| Impl -> 20
1818
| _ -> 30
1919
in
20-
match Int.compare (int_of_kind e1.kind) (int_of_kind e2.kind) with
21-
| 0 -> Astring.String.compare (Id.name e1.id) (Id.name e2.id)
22-
| i -> i
20+
(* Heuristic: If a dir contains only pages, place it before. *)
21+
let by_content (t : t) =
22+
if
23+
List.for_all
24+
(fun x ->
25+
match x.Tree.node.Entry.kind with Page _ -> true | _ -> false)
26+
t.children
27+
then -10
28+
else 10
29+
in
30+
let by_name (t : t) =
31+
match t.node.kind with
32+
| Page { short_title = Some title; _ } -> Comment.to_string title
33+
| _ -> (
34+
match t.node.id.iv with
35+
| `LeafPage (Some parent, name)
36+
when Names.PageName.to_string name = "index" ->
37+
Id.name parent
38+
| _ -> Id.name t.node.id)
39+
in
40+
let try_ comp f fallback =
41+
match comp (f t1) (f t2) with 0 -> fallback () | i -> i
42+
in
43+
try_ (compare : int -> int -> int) by_kind @@ fun () ->
44+
try_ (compare : int -> int -> int) by_content @@ fun () ->
45+
try_ Astring.String.compare by_name @@ fun () -> 0
2346

2447
let rec t_of_in_progress (dir : In_progress.in_progress) : t =
2548
let entry_of_page page =
@@ -158,7 +181,7 @@ let rec t_of_in_progress (dir : In_progress.in_progress) : t =
158181
|> List.map snd
159182
in
160183
let unordered =
161-
List.sort (fun (_, x) (_, y) -> compare_entry x.Tree.node y.node) unordered
184+
List.sort (fun (_, x) (_, y) -> compare_entry x y) unordered
162185
in
163186
let contents = ordered @ unordered |> List.map snd in
164187
{ Tree.node = index; children = contents }

src/model/comment.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,20 @@ let find_zero_heading docs : link_content option =
145145
Some (link_content_of_inline_elements h_content)
146146
| _ -> None)
147147
docs
148+
149+
(* Used in particular to sort the title names *)
150+
let to_string (l : link_content) =
151+
let rec s_of_i (i : non_link_inline_element) =
152+
match i with
153+
| `Code_span s -> s
154+
| `Word w -> w
155+
| `Math_span m -> m
156+
| `Space -> " "
157+
| `Styled (_, is) -> s_of_is is
158+
| `Raw_markup (_, r) -> r
159+
and s_of_is is =
160+
is
161+
|> List.map (fun { Location_.value; _ } -> s_of_i value)
162+
|> String.concat ""
163+
in
164+
s_of_is l

0 commit comments

Comments
 (0)