Adjust heading from README to at least level 2 (fixes #164)

This commit is contained in:
Hannes Mehnert 2023-08-25 11:32:17 +02:00 committed by Reynir Björnsson
parent 5feb615e12
commit 1293e081c6
3 changed files with 27 additions and 3 deletions

View file

@ -45,6 +45,30 @@ let compare_pkgs p1 p2 =
in in
diff_map (parse_pkgs p1) (parse_pkgs p2) diff_map (parse_pkgs p1) (parse_pkgs p2)
let md_to_html ?adjust_heading ?(safe = true) data =
let open Cmarkit in
let doc = Doc.of_string ~heading_auto_ids:true data in
let doc =
Option.fold ~none:doc
~some:(fun lvl ->
let block _m = function
| Block.Heading (h, meta) ->
let open Block.Heading in
let level = level h
and id = id h
and layout = layout h
and inline = inline h
in
let h' = make ?id ~layout ~level:(level + lvl) inline in
Mapper.ret (Block.Heading (h', meta))
| _ -> Mapper.default
in
let mapper = Mapper.make ~block () in
Mapper.map_doc mapper doc)
adjust_heading
in
Cmarkit_html.of_doc ~safe doc
module Path = struct module Path = struct
let to_url ~path ~queries = let to_url ~path ~queries =

View file

@ -285,7 +285,7 @@ have questions or suggestions.
let make_header = let make_header =
[ [
H.Unsafe.data (Cmarkit_html.of_doc ~safe:true (Cmarkit.Doc.of_string ~heading_auto_ids:true data)); H.Unsafe.data (Utils.md_to_html data);
H.form ~a:H.[a_action "/hash"; a_method `Get] [ H.form ~a:H.[a_action "/hash"; a_method `Get] [
H.label [ H.label [
H.txt "Search artifact by SHA256"; H.txt "Search artifact by SHA256";
@ -383,7 +383,7 @@ module Job = struct
[ [
H.h2 ~a:H.[a_id "readme"] [H.txt "README"]; H.h2 ~a:H.[a_id "readme"] [H.txt "README"];
H.a ~a:H.[a_href "#builds"] [H.txt "Skip to builds"]; H.a ~a:H.[a_href "#builds"] [H.txt "Skip to builds"];
H.Unsafe.data (Cmarkit_html.of_doc ~safe:true (Cmarkit.Doc.of_string ~heading_auto_ids:true data)) H.Unsafe.data (Utils.md_to_html ~adjust_heading:2 data)
] ]
) )

View file

@ -1,4 +1,4 @@
let markdown_to_html data = Cmarkit_html.of_doc ~safe:true (Cmarkit.Doc.of_string ~heading_auto_ids:true data) let markdown_to_html = Builder_web__Utils.md_to_html
let test_simple () = let test_simple () =
let markdown = {|# Hello world|} in let markdown = {|# Hello world|} in