22
33# MDX
44
5- MDX allows to execute code blocks inside markdown and mli/mld documentation
6- to help keeping them up to date .
5+ MDX is a tool that generates evaluation results for code interactions inside Markdown ( ` .md ` )
6+ and ` .ml{i,d} ` documentation .
77
8- Use the
9- [ dune stanza] ( https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4 )
10- to enable it on your documentation.
11-
12- ` mdx ` is released on opam and can be installed by running:
8+ MDX is released on opam and can be installed by running:
139
1410``` sh
1511$ opam install mdx
@@ -18,110 +14,140 @@ $ opam install mdx
1814If you want to contribute to the project, please see the
1915[ CONTRIBUTING.md] ( CONTRIBUTING.md ) .
2016
21- ## Basic Usage
17+ ## Setup
2218
23- You can use MDX with your Markdown or ` .ml{i,d} ` documentation, which ensures
24- code in multi-line or verbatim code blocks is correct.
19+ MDX is integrated with Dune. To enable MDX for your Dune project,
20+ you must first add a [ ` dune-project ` stanza ] ( https://dune.readthedocs.io/en/latest/dune-files.html#mdx ) to allow Dune to recognize MDX:
2521
26- To enable MDX on specific files you must first enable it for your project by
27- adding the following stanza to your ` dune-project ` :
2822```
29- (using mdx 0.2 )
23+ (using mdx 0.4 )
3024```
3125
32- Note that version ` 0.2 ` of the MDX stanza is only available in dune ` 3.0 ` or
33- higher. You can use the first, ` 0.1 ` version with dune ` 2.4 ` or higher.
26+ See [ compatibility] ( #compatibility ) for more details about version compatibility.
27+
28+ Then add the following MDX stanza in relevant ` dune ` files:
3429
35- Then add the following in the relevant ` dune ` file:
3630```
37- (mdx)
31+ (mdx (files :standard *.mli) )
3832```
39- That enables MDX on all markdown files in the folder.
33+
34+ Under a folder with a ` dune ` file, ` :standard ` enables MDX on all Markdown files (` *.md ` )
35+ and odoc's documentation pages (` *.mld ` ); ` *.mli ` enables MDX on the OCaml interface files.
4036The MDX stanza can be further configured. Please visit the relevant section of
41- [ dune's manual] ( https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4 )
42- for more information.
37+ [ Dune's manual] ( https://dune.readthedocs.io/en/latest/dune-files.html#mdx ) and
38+ the rest of this README for more information.
39+
40+ ### Compatibility
41+
42+ - ` (using mdx 0.1) ` requires Dune 2.4 or higher, with no restriction on MDX version.
43+ - ` (using mdx 0.2) ` requires Dune 3.0 or higher and MDX 1.9.0 or higher.
44+ - ` (using mdx 0.3) ` requires TODO
45+ - ` (using mdx 0.4) ` requires Dune 3.8 or higher and MDX 2.3.0 or higher.
46+
47+ ## Getting Started
4348
44- MDX supports various type of code blocks but the most common are OCaml toplevel
45- blocks. We illustrate one in our example below. In a Markdown file, you
46- would write something similar to this:
49+ MDX ensures that your code examples behave the way you expect by actually running them.
50+
51+ MDX supports various types of code blocks but the most common ones are
52+ _ OCaml interactive toplevel blocks_ .
53+ We illustrate one in the example below. In a Markdown file, we may have:
4754
4855```` markdown
49- Let's look at how good OCaml is with integers and strings :
56+ Let's look at how good OCaml is with integers and floats :
5057```ocaml
5158# 1 + 2;;
5259- : int = 2
53- # "a" ^ "bc" ;;
54- - : string = "ab"
60+ # 1.2 + 3.4 ;;
61+ - : float = 4.6
5562```
5663````
5764or in an ` mli ` file:
5865``` ocaml
59- (** Let's look at how good OCaml is with integers and strings :
66+ (** Let's look at how good OCaml is with integers and floats :
6067 {@ocaml[
6168 # 1 + 2;;
6269 - : int = 2
63- # "a" ^ "bc" ;;
64- - : string = "ab"
70+ # 1.2 + 3.4 ;;
71+ - : float = 4.6
6572 ]}
6673*)
6774```
6875
69- The content of the toplevel blocks looks just like an interactive toplevel
70- session. Phrases , i.e., the toplevel "input", start with a ` # ` and end with ` ;; ` .
76+ The content of the interactive toplevel blocks looks just like an interactive toplevel
77+ session. _ Phrases _ , i.e., the toplevel "input", start with a ` # ` and end with ` ;; ` .
7178The toplevel evaluation, or "output" follows each phrase.
7279
73- Now you probably have noticed that ` 1 + 2 ` is not equal to ` 2 ` nor is ` "a" ^ "bc" `
74- to ` "ab" ` . Somebody must have updated the phrases, but then forgot to update
75- the evaluation.
80+ Now you probably have noticed that ` 1 + 2 ` is not equal to ` 2 ` nor is ` 1.2 + 3.4 ` type-checked.
81+ Somebody must have introduced a typo, or updated the phrases but then forgot to update the evaluation result.
7682
7783That's exactly why MDX is here!
7884
79- If you enable MDX for this file and then ran ` dune runtest ` , this would be the
80- result:
85+ If we run ` dune runtest ` , this would be the result:
8186
87+ ```` sh
88+ $ dune runtest
89+ File " README.md" , line 1, characters 0-0:
90+ diff --git a/_build/default/README.md b/_build/default/.mdx/README.md.corrected
91+ index e74437a..dfeeb64 100644
92+ --- a/_build/default/README.md
93+ +++ b/_build/default/.mdx/README.md.corrected
94+ @@ -1,7 +1,9 @@
95+ Let' s look at how good OCaml is with integers and floats:
96+ ```ocaml
97+ # 1 + 2;;
98+ -- : int = 2
99+ +- : int = 3
100+ # 1.2 + 3.4;;
101+ -- : float = 4.6
102+ +Line 1, characters 1-4:
103+ +Error: This expression has type float but an expression was expected of type
104+ + int
105+ ```
82106````
107+
108+ The test run just failed, and Dune showed the diff between what we have
109+ locally and what we should have, according to MDX.
110+ This uses Dune' s promotion workflow so that at this point we can investigate
111+ it further if we are surprised by this diff. In this case, we may want to fix
112+ the second phrase to ` # 1.2 +. 3.4;;` . Then, we can re-run ` dune runtest` again.
113+
114+ ` ` ` ` sh
83115$ dune runtest
84116File " README.md" , line 1, characters 0-0:
85- git (internal) (exit 1)
86- (cd _build/default && /usr/bin/git --no-pager diff --no-index --color=always -u README.md .mdx/README.md.corrected)
87- diff --git a/README.md b/.mdx/README.md.corrected
88- index 181b86f..458ecec 100644
89- --- a/README.md
90- +++ b/.mdx/README.md.corrected
91- @@ -1,13 +1,13 @@
92- Let's look at how good OCaml is with integers and strings:
93- ```ocaml
94- # 1 + 2;;
117+ diff --git a/_build/default/README.md b/_build/default/.mdx/README.md.corrected
118+ index 760debc..8e5878b 100644
119+ --- a/_build/default/README.md
120+ +++ b/_build/default/.mdx/README.md.corrected
121+ @@ -1,7 +1,7 @@
122+ Let' s look at how good OCaml is with integers and floats:
123+ ```ocaml
124+ # 1 + 2;;
95125-- : int = 2
96126+- : int = 3
97- # "a" ^ "bc";;
98- -- : string = "ab"
99- +- : string = "abc"
100- ```
127+ # 1.2 +. 3.4;;
128+ - : float = 4.6
129+ ```
101130````
102131
103- The test run just failed and dune is showing the diff between what we have
104- locally and what should be, according to MDX.
105- This uses dune's promotion workflow so at this point you can either investigate
106- it further if you're surprised by this diff or if you're happy with it, simply
107- accept it by running:
132+ Since we are now happy with the diff, we can accept it by running:
108133
109134```
110135dune promote
111136```
112137
113- Now the documentation is up-to-date and running ` dune runtest ` again should be
114- successful!
115-
116- Note that to use the ` dune runtest/promote ` workflow with ` mli ` or ` mld ` files,
117- you will need to adjust the ` mdx ` stanza in the ` dune ` file, as by
118- [ default] ( https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4 ) ,
119- Dune only checks markdown files with ` mdx ` . E.g.,
138+ Now the documentation is up-to-date with the content:
120139
140+ ````markdown
141+ Let' s look at how good OCaml is with integers and floats:
142+ ` ` ` ocaml
143+ # 1 + 2;;
144+ - : int = 3
145+ # 1.2 +. 3.4;;
146+ - : float = 4.6
121147` ` `
122- (mdx
123- (files :standard *.mli))
124- ```
148+ ` ` ` `
149+
150+ and running ` dune runtest ` again should be successful !
125151
126152# # Supported Extensions
127153
0 commit comments