From 1293e081c6711009f4993da8ffd7b8136dca9c38 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Fri, 25 Aug 2023 11:32:17 +0200 Subject: [PATCH] Adjust heading from README to at least level 2 (fixes #164) --- lib/utils.ml | 24 ++++++++++++++++++++++++ lib/views.ml | 4 ++-- test/markdown_to_html.ml | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/utils.ml b/lib/utils.ml index 142718d..de7062a 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -45,6 +45,30 @@ let compare_pkgs p1 p2 = in 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 let to_url ~path ~queries = diff --git a/lib/views.ml b/lib/views.ml index 61ee7c9..a800402 100644 --- a/lib/views.ml +++ b/lib/views.ml @@ -285,7 +285,7 @@ have questions or suggestions. 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.label [ H.txt "Search artifact by SHA256"; @@ -383,7 +383,7 @@ module Job = struct [ H.h2 ~a:H.[a_id "readme"] [H.txt "README"]; 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) ] ) diff --git a/test/markdown_to_html.ml b/test/markdown_to_html.ml index c3a2c76..19bb825 100644 --- a/test/markdown_to_html.ml +++ b/test/markdown_to_html.ml @@ -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 markdown = {|# Hello world|} in