realworldocaml / mdx

Execute code blocks inside your documentation
ISC License
268 stars 45 forks source link

Feature request: overlapping code parts #320

Open TyOverby opened 3 years ago

TyOverby commented 3 years ago

I expected to be able to write this:

(* $MDX part-begin=textbox *)
(* $MDX part-begin=textbox-def *)
let textbox : (string * Vdom.Node.t) Bonsai.Computation.t =
  (* $MDX part-end=textbox-def *)
  let%sub cur_state, set_state = Bonsai.state [%here] (module String) ~default_model:"" in
  return
    (let%map cur_state = cur_state
     and set_state = set_state in
     let view =
       Vdom.Node.input
         [ Vdom.Attr.value cur_state
         ; Vdom.Attr.on_change (fun _ new_state -> set_state new_state)
         ]
         []
     in
     cur_state, view)
;;

(* $MDX part-end=textbox *)

where the "textbox-def" region is contained within the "textbox" region.

but mdx tells me

File "03-state.mdx", lines 101-116: Error in the OCaml file include code block
In file ./../../examples/bonsai_guide_code/state_examples.ml, line 7: Part textbox has no end.
NathanReb commented 3 years ago

I'd expect this to work as well, might be a bug in the parts delimiters parsing, I'll take a look, thanks for reporting it!

smondet commented 3 years ago

@NathanReb With 1.10.1 the only ocaml comments I can seem to make work are (* $MDX part-end *), without the =<...> portion that @TyOverby suggests. What version did you expect to work?

NathanReb commented 3 years ago

Sorry for the delay on this.

@smondet you're right that part-end does not expect a value.

I'm not sure why I expected it to work but it's indeed not supported at the moment. I agree it would be a nice feature and we should look into it.

We need to make sure that other delimiters are dropped from the output, i.e. that in your example, if you import text-box, you can't see the text-box-def delimiters in your mdx block.

We have to wrap up the 2.0.0 release but then we can look into implementing this feature! CC @Leonidas-from-XIV