@@ -121,6 +121,59 @@ 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 (String. starts_with ~prefix: sep str);
153+ assert (String. ends_with ~suffix: " ]}" str);
154+ let str =
155+ String. sub str (String. length sep)
156+ (String. length str - String. length sep - 2 )
157+ in
158+ let loc =
159+ Location.
160+ {
161+ loc_start =
162+ {
163+ code_block.content.loc_end with
164+ pos_cnum = starts + String. length sep + 1 ;
165+ };
166+ loc_end = { code_block.code_block.loc_end with pos_cnum = ends - 2 };
167+ loc_ghost = code_block.code_block.loc_ghost;
168+ }
169+ in
170+ let location = loc.loc_start in
171+ match extract_code_block_info [] ~location ~docstring: str with
172+ | [ x ] ->
173+ x.content |> slice file_contents |> String. split_on_char '\n'
174+ |> List. map output_of_line
175+ | _ -> assert false
176+
124177(* Given code block metadata and the original file, this function splices the
125178 contents of the code block from the original text and creates an Mdx
126179 Block.t, or reports the error (e.g., from invalid tags) *)
@@ -147,15 +200,13 @@ let make_block code_block file_contents =
147200 match handle_header code_block.Code_block. metadata with
148201 | Error _ as e -> e
149202 | 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
155203 let delim = code_block.delimiter in
156- let contents = slice code_block.content |> String. split_on_char '\n' in
204+ let contents =
205+ slice file_contents code_block.content |> String. split_on_char '\n'
206+ in
207+ let errors = slice_error code_block file_contents in
157208 Block. mk ~loc: code_block.code_block ~section: None ~labels ~header
158- ~contents ~legacy_labels: false ~errors: [] ~delim
209+ ~contents ~legacy_labels: false ~errors ~delim
159210
160211(* Given the locations of the code blocks within [file_contents], then slice it up into
161212 [Text] and [Block] parts by using the starts and ends of those blocks as
0 commit comments