Skip to content

Commit 6c77d36

Browse files
committed
mrkdwn: add pretty-printer skeleton
1 parent 0c63ed8 commit 6c77d36

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

lib/mrkdwn.ml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,48 @@ and transform = function
6363
| Text s -> Text (transform_text s)
6464
| (Br | Hr | NL | Ref _ | Img_ref _ | Raw _ | Raw_block _ | X _) as e -> e
6565

66+
(** Translates omd AST to a Slack mrkdwn string. Code heavily adapted
67+
from omd 1.3.1 source.
68+
https://github.com/ocaml/omd/blob/1.3.1/src/omd_backend.ml#L872
69+
*)
70+
let mrkdwn_of_md md =
71+
let b = Buffer.create 128 in
72+
let rec f tl = function
73+
| X _ -> loop tl
74+
| Blockquote _q -> loop tl
75+
| Ref (_rc, _name, _text, _fallback) -> loop tl
76+
| Img_ref (_rc, _name, _text, _fallback) -> loop tl
77+
| Paragraph _md -> loop tl
78+
| Img (_alt, _src, _title) -> loop tl
79+
| Text _t -> loop tl
80+
| Raw _t -> loop tl
81+
| Raw_block _t -> loop tl
82+
| Emph _md -> loop tl
83+
| Bold _md -> loop tl
84+
| Ul _l -> loop tl
85+
| Ol _l -> loop tl
86+
| Ulp _l -> loop tl
87+
| Olp _l -> loop tl
88+
| Code_block (_lang, _c) -> loop tl
89+
| Code (_lang, _c) -> loop tl
90+
| Br -> loop tl
91+
| Hr -> loop tl
92+
| Html (_tagname, _attrs, _body) -> loop tl
93+
| Html_block (_tagname, _attrs, _body) -> loop tl
94+
| Html_comment _s -> loop tl
95+
| Url (_href, _s, _title) -> loop tl
96+
| H1 _md -> loop tl
97+
| H2 _md -> loop tl
98+
| H3 _md -> loop tl
99+
| H4 _md -> loop tl
100+
| H5 _md -> loop tl
101+
| H6 _md -> loop tl
102+
| NL -> loop tl
103+
and loop = function
104+
| hd :: tl -> f tl hd
105+
| [] -> ()
106+
in
107+
loop md;
108+
Buffer.contents b
109+
66110
let mrkdwn_of_markdown str = unescape_omd @@ to_markdown @@ transform_list @@ of_string str

0 commit comments

Comments
 (0)