@@ -121,6 +121,52 @@ let docstring_code_blocks str =
121121 in
122122 loop [] |> List. rev)
123123
124+ let output_of_line x =
125+ match String. trim x with "..." -> `Ellipsis | _ -> `Output x
126+
127+ let slice file_contents (loc : Location.t ) =
128+ let start = loc.loc_start.pos_cnum in
129+ let len = loc.loc_end.pos_cnum - start in
130+ String. sub file_contents start len
131+
132+ (* extract "Error: expected int, got string" from:
133+ {delim@ocaml[
134+ let x = 1 + "a string is not an int"
135+ ]delim[
136+ {err@mdx-error[
137+ Error: expected int, got string
138+ ]err}
139+ ]} *)
140+ let slice_error (code_block : Code_block.t ) file_contents =
141+ let starts = code_block.content.loc_end.pos_cnum in
142+ let ends = code_block.code_block.loc_end.pos_cnum in
143+ let len = ends - starts in
144+ let str = String. sub file_contents starts len in
145+ let no_errors = Fmt. str " ]%a}" Fmt. (option string ) code_block.delimiter in
146+ if str = no_errors then []
147+ else
148+ let sep = Fmt. str " ]%a[\n " Fmt. (option string ) code_block.delimiter in
149+ assert (Astring.String. is_prefix ~affix: sep str);
150+ assert (Astring.String. is_suffix ~affix: " ]}" str);
151+ let str =
152+ String. sub str (String. length sep)
153+ (String. length str - String. length sep - 2 )
154+ in
155+ let location =
156+ { code_block.content.loc_end with pos_cnum = starts + String. length sep }
157+ in
158+ match extract_code_block_info [] ~location ~docstring: str with
159+ | [ x ] ->
160+ let lines =
161+ x.content |> slice file_contents |> String. split_on_char '\n'
162+ in
163+ let lines =
164+ (* Discard the first and last lines *)
165+ List. tl (List. rev (List. tl (List. rev lines)))
166+ in
167+ List. map output_of_line lines
168+ | _ -> assert false
169+
124170(* Given code block metadata and the original file, this function splices the
125171 contents of the code block from the original text and creates an Mdx
126172 Block.t, or reports the error (e.g., from invalid tags) *)
@@ -147,15 +193,13 @@ let make_block code_block file_contents =
147193 match handle_header code_block.Code_block. metadata with
148194 | Error _ as e -> e
149195 | Ok (header , labels ) ->
150- let slice (loc : Location.t ) =
151- let start = loc.loc_start.pos_cnum in
152- let len = loc.loc_end.pos_cnum - start in
153- String. sub file_contents start len
154- in
155196 let delim = code_block.delimiter in
156- let contents = slice code_block.content |> String. split_on_char '\n' in
197+ let contents =
198+ slice file_contents code_block.content |> String. split_on_char '\n'
199+ in
200+ let errors = slice_error code_block file_contents in
157201 Block. mk ~loc: code_block.code_block ~section: None ~labels ~header
158- ~contents ~legacy_labels: false ~errors: [] ~delim
202+ ~contents ~legacy_labels: false ~errors ~delim
159203
160204(* Given the locations of the code blocks within [file_contents], then slice it up into
161205 [Text] and [Block] parts by using the starts and ends of those blocks as
0 commit comments