Skip to content

Commit 0601d94

Browse files
committed
mrkdwn: remove old code and promote two tests
[commit_comment.mrkdwn_comment] - add more MD elements to be tested (headings ordered lists, etc.) - properly escape characters - remove extraneous line after blockquotes and nested lists
1 parent 316d015 commit 0601d94

File tree

3 files changed

+5
-60
lines changed

3 files changed

+5
-60
lines changed

lib/mrkdwn.ml

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
open Omd
22
open Base
33

4-
let escape_url_chars = Staged.unstage @@ String.Escaping.escape ~escapeworthy:[ '<'; '>'; '|' ] ~escape_char:'\\'
5-
64
(* https://api.slack.com/reference/surfaces/formatting#escaping *)
75
let escape_mrkdwn =
86
String.concat_map ~f:(function
@@ -11,58 +9,6 @@ let escape_mrkdwn =
119
| '&' -> "&amp;"
1210
| c -> String.make 1 c)
1311

14-
(** Unescape markdown characters escaped because "any ASCII punctuation
15-
character may be backslash-escaped"
16-
https://spec.commonmark.org/0.30/#backslash-escapes
17-
18-
This pertains to '\\', '[', ']', '(', ')', '`', '*' unconditionally,
19-
and '.', '-', '+', '!', '<', '>', '#' depending on chars before/after.
20-
Argument escapeworthy_map can be left blank because escaped chars are
21-
unescaped to themselves. *)
22-
let unescape_omd = Staged.unstage @@ String.Escaping.unescape_gen_exn ~escapeworthy_map:[] ~escape_char:'\\'
23-
24-
(** Escape the `escape_char` '\\' for use with `unescape_omd` later *)
25-
let escape_omd = Staged.unstage @@ String.Escaping.escape_gen_exn ~escapeworthy_map:[] ~escape_char:'\\'
26-
27-
let transform_text = escape_mrkdwn
28-
29-
(** `Omd.to_markdown` escapes backslash (and other applicable chars) in
30-
`Text` elements but not `Code` elements, so do the same for the latter so
31-
that `unescape_omd` can apply uniformly to the whole mrkdwn string later *)
32-
let transform_code s = escape_omd @@ escape_mrkdwn s
33-
34-
let rec transform_list = List.map ~f:transform
35-
36-
and transform_flatten = List.map ~f:transform_list
37-
38-
and surround s t =
39-
let t = to_markdown @@ transform_list t in
40-
Raw (Printf.sprintf "%s%s%s" s t s)
41-
42-
(** massage markdown AST so that rendered result looks like slack mrkdwn *)
43-
and transform = function
44-
| H1 t | H2 t | H3 t | H4 t | H5 t | H6 t -> Paragraph (transform_list [ Bold t ])
45-
| Paragraph t -> Paragraph (transform_list t)
46-
| Emph t -> surround "_" (transform_list t)
47-
| Bold t -> surround "*" (transform_list t)
48-
| Ul ts -> Ul (transform_flatten ts)
49-
| Ol ts -> Ol (transform_flatten ts)
50-
| Ulp ts -> Ulp (transform_flatten ts)
51-
| Olp ts -> Olp (transform_flatten ts)
52-
| Url (href, label, title) ->
53-
let label = escape_url_chars @@ to_markdown @@ transform_list label in
54-
let title = if String.length title > 0 then Printf.sprintf "%s - " @@ escape_url_chars title else title in
55-
Raw (Printf.sprintf "<%s|%s%s>" href title label)
56-
| Html _ as e -> Raw (Printf.sprintf "`%s`" @@ to_markdown [ e ])
57-
| Html_comment _ -> Br
58-
| Html_block _ as e -> Code_block ("", to_markdown [ e ])
59-
| Blockquote t -> Blockquote (transform_list t)
60-
| Img (alt, src, title) -> transform @@ Url (src, [ Text alt ], title)
61-
| Code_block (_, str) -> Code_block ("", transform_code str)
62-
| Code (_, str) -> Code ("", transform_code str)
63-
| Text s -> Text (transform_text s)
64-
| (Br | Hr | NL | Ref _ | Img_ref _ | Raw _ | Raw_block _ | X _) as e -> e
65-
6612
(** Translates omd AST to a Slack mrkdwn string. Code heavily adapted
6713
from omd 1.3.1 source.
6814
https://github.com/ocaml/omd/blob/1.3.1/src/omd_backend.ml#L872
@@ -224,8 +170,7 @@ let rec mrkdwn_of_md md =
224170
loop list_indent s;
225171
Buffer.add_char b '>';
226172
loop list_indent tl
227-
| H1 md' | H2 md' | H3 md' | H4 md' | H5 md' | H6 md' ->
228-
loop list_indent (Paragraph (transform_list [ Bold md' ]) :: tl)
173+
| H1 md' | H2 md' | H3 md' | H4 md' | H5 md' | H6 md' -> loop list_indent (Paragraph [ Bold md' ] :: tl)
229174
| Br ->
230175
(* the string "\\n" (backslash-newline) or end of line double-space renders Br *)
231176
nl b;
@@ -254,4 +199,4 @@ let rec mrkdwn_of_md md =
254199
end;
255200
Buffer.contents b
256201

257-
let mrkdwn_of_markdown str = unescape_omd @@ to_markdown @@ transform_list @@ of_string str
202+
let mrkdwn_of_markdown str = mrkdwn_of_md @@ of_string str

mock_payloads/commit_comment.mrkdwn_comment.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"created_at": "2020-06-07T15:11:50Z",
3333
"updated_at": "2020-06-07T15:11:50Z",
3434
"author_association": "OWNER",
35-
"body": "**bold**, *italic*\n> blockquote\n\n* list-element 1\n* list-element 2\n * list-element2.1\n * [list-element2.2](www.google.com)\n\n<div>html block</div>\n```ocaml\nStdio.printf \"hello ocaml\"\n```\nescaped chars \\\\\\(\\)\\[\\]\\`\\*\n`keep \\`\n```\nkeep \\\n```"
35+
"body": "# header 1\r\n## header 2\r\n### header 3\r\n#### header 4\r\n##### header 5\r\n###### header 6\r\n\r\n***\r\n\r\nparagraph 1 **bold**, _italic_, ~strikethrough~, **_bold italic_**, _**italic bold**_\r\n\r\nparagraph 2\r\nhard line break\r\n\r\n> blockquote\r\n\r\n> > > multi\r\n> >\r\n> > level\r\n>\r\n> blockquote\r\n\r\n* list-element 1\r\n* list-element 2\r\n * list-element 2.1\r\n * list-element 2.1.1\r\n * [list-element 2.2](www.google.com)\r\n\r\n1. list-element 1\r\n1. list-element 2\r\n 1. list-element 2.1\r\n 1. list-element 2.1.1\r\n 1. [list-element 2.2](www.google.com)\r\n1. list-element 3\r\n\r\n[some link text](www.google.com \"link with title\")\r\n\r\n* > quote in list\r\n\r\n> * list in quote\r\n\r\n![image](https://via.placeholder.com/20x20)\r\n\r\n![image](https://via.placeholder.com/20x20 \"with title\")\r\n\r\n![](https://via.placeholder.com/20x20 \"no alt\")\r\n\r\n![](https://via.placeholder.com/20x20)\r\n\r\n<div>html block with <i>styling</i></div>\r\n\r\n```ocaml\r\nStdio.printf \"hello ocaml\"\r\n```\r\n\r\ninline `code block`\r\n\r\nescape these \\\\\\(\\)\\[\\]\\`\\* `but keep \\`\r\n\r\n```\r\nand keep \\\r\n```\r\n"
3636
},
3737
"repository": {
3838
"id": 265741348,

test/slack_payloads.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ will notify #all-push-events
9595
"pretext":
9696
"<https://github.com/xinyuluo/pr_test|[xinyuluo/pr_test]> *xinyuluo* commented on `<https://github.com/xinyuluo/pr_test/commit/cd5b85afa306840e0790b62e349ee1f828b2a3c2#commitcomment-39729580|cd5b85af>` add new line at EOF",
9797
"text":
98-
"*bold*, _italic_\n\n> blockquote\n\n\n- list-element 1\n- list-element 2\n - list-element2.1\n - <www.google.com|list-element2.2>\n\n\n```\n<div>html block</div>\n```\n```\nStdio.printf \"hello ocaml\"\n```\nescaped chars \\()[]`*\n`keep \\`\n\n```\nkeep \\\n```",
98+
"*header 1*\n\n*header 2*\n\n*header 3*\n\n*header 4*\n\n*header 5*\n\n*header 6*\n\n* * *\nparagraph 1 *bold*, _italic_, ~strikethrough~, *_bold italic_*, _*italic bold*_\n\nparagraph 2\nhard line break\n\n> blockquote\n\n> > > multi\n> > \n> > level\n> \n> blockquote\n\n- list-element 1\n- list-element 2\n - list-element 2.1\n - list-element 2.1.1\n - <www.google.com|list-element 2.2>\n\n1. list-element 1\n2. list-element 2\n 1. list-element 2.1\n 1. list-element 2.1.1\n 2. <www.google.com|list-element 2.2>\n3. list-element 3\n\n<www.google.com|link with title - some link text>\n\n- > quote in list\n\n> - list in quote\n\n<https://via.placeholder.com/20x20|image>\n\n<https://via.placeholder.com/20x20|with title - image>\n\n<https://via.placeholder.com/20x20|no alt - >\n\n<https://via.placeholder.com/20x20|>\n\n```\n&lt;div&gt;html block with &lt;i&gt;styling&lt;/i&gt;&lt;/div&gt;\n```\n```\nStdio.printf \"hello ocaml\"\n```\ninline `code block`\n\nescape these \\()[]`* `but keep \\`\n\n```\nand keep \\\n```",
9999
"footer":
100100
"New comment by xinyuluo in <https://github.com/xinyuluo/pr_test/commit/cd5b85afa306840e0790b62e349ee1f828b2a3c2#commitcomment-39729580|runner/chat.ml>"
101101
}
@@ -196,7 +196,7 @@ will notify #frontend-bot
196196
"pretext":
197197
"<https://git.ahrefs.com/ahrefs/frontend|[ahrefs/frontend]> *raman* <https://git.ahrefs.com/ahrefs/frontend/pull/6168#issuecomment-17123|commented> on #6168 <https://git.ahrefs.com/ahrefs/frontend/pull/6168|[ADMIN] Search results by IP (Blocked IP Info UI)>",
198198
"text":
199-
"> I do not understand this\n\n\nDo you have access to AA logs in ahrefs.me?"
199+
"> I do not understand this\n\nDo you have access to AA logs in ahrefs.me?"
200200
}
201201
]
202202
}

0 commit comments

Comments
 (0)