Skip to content

Commit

Permalink
Merge pull request #3 from tatchi/basic-cmarkit-conversion
Browse files Browse the repository at this point in the history
basic cmarkit to mrkdwn converter
  • Loading branch information
sewenthy authored Mar 19, 2024
2 parents 370262f + bddd8fd commit 6950a3f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
atdgen
atdgen-runtime
biniou
cmarkit
cstruct
devkit
extlib
Expand Down
74 changes: 74 additions & 0 deletions lib/mrkdwn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,77 @@ let highlight_mentions get_id_by_slack_name s =
|> Option.default s
in
Re2.replace_exn mention_re s ~f:(fun m -> subst (Re2.Match.get_exn ~sub:(`Index 0) m))

module Cmarkit_slack = struct
let renderer =
(* https://www.markdownguide.org/tools/slack/#slack-markdown-support-in-posts *)
(* https://slack.com/intl/en-gb/help/articles/202288908-Format-your-messages *)
let inline c inline =
let open Cmarkit in
let module C = Cmarkit_renderer.Context in
let strong_emphasis c e =
let i = Inline.Emphasis.inline e in
C.byte c '*';
C.inline c i;
C.byte c '*'
in
let emphasis c e =
let i = Inline.Emphasis.inline e in
C.byte c '_';
C.inline c i;
C.byte c '_'
in
let strikethrough c s =
let i = Inline.Strikethrough.inline s in
C.byte c '~';
C.inline c i;
C.byte c '~'
in
let link c l =
match Inline.Link.reference l with
| `Inline (ld, _) ->
begin
match Link_definition.dest ld with
| None -> C.inline c (Inline.Link.text l)
| Some (dest, _) ->
C.byte c '<';
C.string c dest;
C.byte c '|';
C.inline c (Inline.Link.text l);
C.byte c '>'
end;
true
| _ -> false
in
match inline with
| Inline.Strong_emphasis (e, _) ->
strong_emphasis c e;
true
| Inline.Emphasis (e, _) ->
emphasis c e;
true
| Inline.Ext_strikethrough (s, _) ->
strikethrough c s;
true
| Inline.Link (l, _) -> link c l
| _ -> false (* let the default renderer handle that *)
in
let block c block =
let open Cmarkit in
let module C = Cmarkit_renderer.Context in
match block with
| Block.Heading (heading, _) ->
let inline = Block.Heading.inline heading in
C.byte c '*';
C.inline c inline;
C.byte c '*';
C.byte c '\n';
true
| _ -> false
in
let default_renderer = Cmarkit_commonmark.renderer () in
let renderer = Cmarkit_renderer.make ~inline ~block () in
Cmarkit_renderer.compose default_renderer renderer
end

let of_doc = Cmarkit_renderer.doc_to_string Cmarkit_slack.renderer
11 changes: 11 additions & 0 deletions lib_test/mrkdwn/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(executable
(name mrkdwn_of_md)
(libraries slack_lib cmarkit))

(env
(_
(binaries
(./mrkdwn_of_md.exe as mrkdwn_of_md))))

(cram
(deps %{bin:mrkdwn_of_md}))
1 change: 1 addition & 0 deletions lib_test/mrkdwn/mrkdwn_of_md.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let () = print_string (Cmarkit.Doc.of_string ~strict:false (In_channel.input_all stdin) |> Slack_lib.Mrkdwn.of_doc)
46 changes: 46 additions & 0 deletions lib_test/mrkdwn/mrkdwn_of_md.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
bold
$ mrkdwn_of_md << "MD"
> **bold**
> __bold__
> MD
*bold*
*bold*

italic
$ mrkdwn_of_md << "MD"
> *italic*
> _italic_
> MD
_italic_
_italic_


strikethrough
$ mrkdwn_of_md << "MD"
> ~~strike~~
> ~strike~
> MD
~strike~
~strike~

link
$ mrkdwn_of_md << "MD"
> [hello](https://google.be)
> MD
<https://google.be|hello>

headings
$ mrkdwn_of_md << "MD"
> # one
> ## two
> ### three
> #### four
> ##### five
> ###### six
> MD
*one*
*two*
*three*
*four*
*five*
*six*

0 comments on commit 6950a3f

Please sign in to comment.