Skip to content

Commit f40d773

Browse files
committed
Parse and add frontmatter to page model
1 parent a45c770 commit f40d773

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

src/model/comment.ml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,34 @@ and link_content_of_inline_elements l =
130130
l |> List.map link_content_of_inline_element |> List.concat
131131

132132
let find_zero_heading docs : link_content option =
133-
let rec find_map f = function
134-
| [] -> None
135-
| x :: l -> (
136-
match f x with Some _ as result -> result | None -> find_map f l)
137-
in
138-
find_map
133+
Odoc_utils.List.find_map
139134
(fun doc ->
140135
match doc.Location_.value with
141136
| `Heading ({ heading_level = `Title; _ }, _, h_content) ->
142137
Some (link_content_of_inline_elements h_content)
143138
| _ -> None)
144139
docs
140+
141+
let extract_frontmatter docs : _ =
142+
let parse_frontmatter s =
143+
let lines = Astring.String.cuts ~sep:"\n" s in
144+
Odoc_utils.List.filter_map
145+
(fun line -> Astring.String.cut ~sep:":" line)
146+
lines
147+
in
148+
let extracted =
149+
let rec aux acc = function
150+
| [] -> None
151+
| doc :: l -> (
152+
match doc.Location_.value with
153+
| `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)
158+
in
159+
aux [] docs
160+
in
161+
match extracted with
162+
| None -> (None, docs)
163+
| Some (fm, docs) -> (Some fm, docs)

src/model/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
(backend landmarks --auto))
1717
(instrumentation
1818
(backend bisect_ppx))
19-
(libraries result compiler-libs.common odoc-parser))
19+
(libraries result compiler-libs.common odoc-parser odoc_utils))

src/model/lang.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,16 @@ end =
527527
module rec Page : sig
528528
type child = Page_child of string | Module_child of string
529529

530+
module Frontmatter : sig
531+
type t = (string * string) list
532+
end
533+
530534
type t = {
531535
name : Identifier.Page.t;
532536
root : Root.t;
533537
content : Comment.docs;
534538
children : child list;
539+
frontmatter : Frontmatter.t option;
535540
digest : Digest.t;
536541
linked : bool;
537542
}

src/odoc/compile.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,14 @@ let mld ~parent_id ~parents_children ~output ~children ~warnings_options input =
238238
>>= fun name ->
239239
let resolve content =
240240
let zero_heading = Comment.find_zero_heading content in
241+
let frontmatter, content = Comment.extract_frontmatter content in
241242
let root =
242243
let file = Root.Odoc_file.create_page root_name zero_heading in
243244
{ Root.id = (name :> Paths.Identifier.OdocId.t); file; digest }
244245
in
245246
let page =
246-
Lang.Page.{ name; root; children; content; digest; linked = false }
247+
Lang.Page.
248+
{ name; root; children; content; digest; linked = false; frontmatter }
247249
in
248250
Odoc_file.save_page output ~warnings:[] page;
249251
Ok ()

src/odoc/html_fragment.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input =
1515
in
1616
let to_html content =
1717
(* This is a mess. *)
18+
let frontmatter, content = Odoc_model.Comment.extract_frontmatter content in
1819
let page =
1920
Odoc_model.Lang.Page.
20-
{ name = id; root; content; children = []; digest; linked = false }
21+
{
22+
name = id;
23+
root;
24+
content;
25+
children = [];
26+
digest;
27+
linked = false;
28+
frontmatter;
29+
}
2130
in
2231
let env = Resolver.build_env_for_page resolver page in
2332
Odoc_xref2.Link.resolve_page ~filename:input_s env page

0 commit comments

Comments
 (0)