Skip to content

Commit 0003f45

Browse files
committed
mrkdwn: escape backslash in code elements
1 parent e589230 commit 0003f45

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/mrkdwn.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ let unescape_omd =
1616

1717
let transform_text = escape_mrkdwn
1818

19+
let transform_code s =
20+
(** Omd.to_markdown escapes Text elements but not Code elements, so we do the same for
21+
Code elements so that unescape_omd can be applied uniformly to the whole string later *)
22+
String.substr_replace_all ~pattern:"\\" ~with_:"\\\\" @@ escape_mrkdwn s
23+
1924
let rec transform_list = List.map ~f:transform
2025

2126
and transform_flatten = List.map ~f:transform_list
@@ -43,9 +48,10 @@ and transform = function
4348
| Html_block _ as e -> Code_block ("", to_markdown [ e ])
4449
| Blockquote t -> Blockquote (transform_list t)
4550
| Img (alt, src, title) -> transform @@ Url (src, [ Text alt ], title)
46-
| Code_block (_, str) -> Code_block ("", str)
51+
| Code_block (_, str) -> Code_block ("", transform_code str)
52+
| Code (_, str) -> Code ("", transform_code str)
4753
| Text s -> Text (transform_text s)
48-
| (Code _ | Br | Hr | NL | Ref _ | Img_ref _ | Raw _ | Raw_block _ | X _) as e -> e
54+
| (Br | Hr | NL | Ref _ | Img_ref _ | Raw _ | Raw_block _ | X _) as e -> e
4955

5056
(* unescaping here is a workaround of to_markdown escaping parentheses in text (bug?) *)
5157
let mrkdwn_of_markdown str = unescape_omd @@ to_markdown @@ transform_list @@ of_string str

0 commit comments

Comments
 (0)