Skip to content

Commit 54cb13a

Browse files
committed
Frontmatter: extract all frontmatters in a single page
1 parent 3a91556 commit 54cb13a

File tree

6 files changed

+37
-52
lines changed

6 files changed

+37
-52
lines changed

src/model/comment.ml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,16 @@ let extract_frontmatter docs : _ =
145145
(fun line -> Astring.String.cut ~sep:":" line)
146146
lines
147147
in
148-
let extracted =
149-
let rec aux acc = function
150-
| [] -> None
151-
| doc :: l -> (
148+
let fm, content =
149+
let fm, rev_content =
150+
List.fold_left
151+
(fun (fm_acc, content_acc) doc ->
152152
match doc.Location_.value with
153153
| `Code_block (Some "frontmatter", content, None) ->
154-
Some
155-
( parse_frontmatter content.Location_.value,
156-
List.rev_append acc l )
157-
| _ -> aux (doc :: acc) l)
154+
(parse_frontmatter content.Location_.value :: fm_acc, content_acc)
155+
| _ -> (fm_acc, doc :: content_acc))
156+
([], []) docs
158157
in
159-
aux [] docs
158+
(List.concat fm, List.rev rev_content)
160159
in
161-
match extracted with
162-
| None -> (None, docs)
163-
| Some (fm, docs) -> (Some fm, docs)
160+
(fm, content)

src/model/lang.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ module rec Page : sig
532532
root : Root.t;
533533
content : Comment.docs;
534534
children : child list;
535-
frontmatter : Frontmatter.t option;
535+
frontmatter : Frontmatter.t;
536536
digest : Digest.t;
537537
linked : bool;
538538
}

src/model/root.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Odoc_file = struct
3232
type page = {
3333
name : string;
3434
title : Comment.link_content option;
35-
frontmatter : Frontmatter.t option;
35+
frontmatter : Frontmatter.t;
3636
}
3737

3838
type t =

src/model/root.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module Odoc_file : sig
3131
type page = {
3232
name : string;
3333
title : Comment.link_content option;
34-
frontmatter : Frontmatter.t option;
34+
frontmatter : Frontmatter.t;
3535
}
3636

3737
type t =
@@ -42,8 +42,7 @@ module Odoc_file : sig
4242

4343
val create_unit : force_hidden:bool -> string -> t
4444

45-
val create_page :
46-
string -> Comment.link_content option -> Frontmatter.t option -> t
45+
val create_page : string -> Comment.link_content option -> Frontmatter.t -> t
4746

4847
val create_impl : string -> t
4948

src/model_desc/lang_desc.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,7 @@ and page_t =
697697
[
698698
F ("name", (fun t -> t.name), identifier);
699699
F ("root", (fun t -> t.root), root);
700-
F
701-
( "frontmatter",
702-
(fun t -> t.frontmatter),
703-
Option (List (Pair (string, string))) );
700+
F ("frontmatter", (fun t -> t.frontmatter), List (Pair (string, string)));
704701
F ("content", (fun t -> t.content), docs);
705702
F ("digest", (fun t -> t.digest), Digest.t);
706703
]

test/pages/frontmatter.t/run.t

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ When there is no frontmatter, everything is normal
22

33
$ odoc compile zero_frontmatter.mld
44
$ odoc_print page-zero_frontmatter.odoc | jq '.frontmatter'
5-
"None"
5+
[]
66

77
When there is one frontmatter, it is extracted from the content:
88

99
$ odoc compile one_frontmatter.mld
1010
$ odoc_print page-one_frontmatter.odoc | jq '.frontmatter'
11-
{
12-
"Some": [
13-
[
14-
"bli1",
15-
" bloblobloblo1"
16-
],
17-
[
18-
"bli2",
19-
" bloblobloblo2"
20-
]
11+
[
12+
[
13+
"bli1",
14+
" bloblobloblo1"
15+
],
16+
[
17+
"bli2",
18+
" bloblobloblo2"
2119
]
22-
}
20+
]
2321
$ odoc_print page-one_frontmatter.odoc | jq '.content'
2422
[
2523
{
@@ -52,18 +50,20 @@ When there is more than one frontmatter, they are all extracted from the content
5250

5351
$ odoc compile two_frontmatters.mld
5452
$ odoc_print page-two_frontmatters.odoc | jq '.frontmatter'
55-
{
56-
"Some": [
57-
[
58-
"bli1",
59-
" bloblobloblo1"
60-
],
61-
[
62-
"bli2",
63-
" bloblobloblo2"
64-
]
53+
[
54+
[
55+
"bli3",
56+
" bloblobloblo1"
57+
],
58+
[
59+
"bli1",
60+
" bloblobloblo1"
61+
],
62+
[
63+
"bli2",
64+
" bloblobloblo2"
6565
]
66-
}
66+
]
6767
$ odoc_print page-two_frontmatters.odoc | jq '.content'
6868
[
6969
{
@@ -89,13 +89,5 @@ When there is more than one frontmatter, they are all extracted from the content
8989
}
9090
]
9191
]
92-
},
93-
{
94-
"`Code_block": [
95-
{
96-
"Some": "frontmatter"
97-
},
98-
"bli3: bloblobloblo1"
99-
]
10092
}
10193
]

0 commit comments

Comments
 (0)