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