|
| 1 | +{0 MDX Manual} |
| 2 | + |
| 3 | +Welcome to the MDX Manual! |
| 4 | + |
| 5 | +MDX is a tool to help developpers and authors keep their documentation |
| 6 | +up-to-date. |
| 7 | +It ensures that the code examples you write compile and behave the way you |
| 8 | +expect them to by actually running them. |
| 9 | + |
| 10 | +It is for example used to verify all of |
| 11 | +{{:https://dev.realworldocaml.org/}Realworlocaml}'s examples! |
| 12 | + |
| 13 | +MDX is released on opam. You can install it in your switch by running: |
| 14 | +{@sh[ |
| 15 | + opam install mdx |
| 16 | +]} |
| 17 | + |
| 18 | +{1 Basic Usage} |
| 19 | + |
| 20 | +You can use MDX with your Markdown or [.mli] documentation, where it will ensure |
| 21 | +the correctness of code respectively written in multi-line code blocks and |
| 22 | +verbatim code blocks. |
| 23 | + |
| 24 | +To enable MDX, you need to add [(mdx)] stanzas to the right dune files. Before |
| 25 | +that you will need to enable MDX for your project by adding the following to |
| 26 | +your [dune-project]: |
| 27 | + |
| 28 | +{@dune[ |
| 29 | + (using mdx 0.2) |
| 30 | +]} |
| 31 | + |
| 32 | +You can then add the following in the relevant dune file: |
| 33 | +{@dune[ |
| 34 | + (mdx) |
| 35 | +]} |
| 36 | +That will enable MDX on all markdown files in the folder. |
| 37 | +The MDX stanza can be further configured. If you want to know more about it, |
| 38 | +visit the {{!page-mdx_dune_stanza}corresponding section of this manual} or the |
| 39 | +one in |
| 40 | +{{:https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4}dune's manual}. |
| 41 | + |
| 42 | +MDX supports various type of code blocks but the most common are OCaml toplevel |
| 43 | +blocks so we'll look at one of those for our example. In a markdown file, you |
| 44 | +would write something along those lines: |
| 45 | + |
| 46 | +{@markdown[ |
| 47 | + Let's look at how good OCaml is with integers and strings: |
| 48 | + ```ocaml |
| 49 | + # 1 + 2;; |
| 50 | + - : int = 2 |
| 51 | + # "a" ^ "bc";; |
| 52 | + - : string = "ab" |
| 53 | + ``` |
| 54 | +]} |
| 55 | + |
| 56 | +The content of the toplevel blocks looks just like an interactive toplevel |
| 57 | +session. Phrases, i.e. the toplevel "input", start with a [#] and end with [;;]. |
| 58 | +Each of them is followed by the toplevel evaluation, or "output" which you |
| 59 | +probably are already familiar with. |
| 60 | + |
| 61 | +Now you probably have noticed that [1 + 2] is not equal to [3] nor ["a" ^ "bc"] |
| 62 | +to ["ab"]. Somebody must have updated the phrases but they then forgot to update |
| 63 | +the evaluation. |
| 64 | + |
| 65 | +That's exactly what MDX is here for! |
| 66 | + |
| 67 | +If MDX were enabled for this file and they ran [dune runtest], here's what they |
| 68 | +would have gotten: |
| 69 | + |
| 70 | +{@sh[ |
| 71 | + $ dune runtest |
| 72 | + File "README.md", line 1, characters 0-0: |
| 73 | + git (internal) (exit 1) |
| 74 | + (cd _build/default && /usr/bin/git --no-pager diff --no-index --color=always -u README.md .mdx/README.md.corrected) |
| 75 | + diff --git a/README.md b/.mdx/README.md.corrected |
| 76 | + index 181b86f..458ecec 100644 |
| 77 | + --- a/README.md |
| 78 | + +++ b/.mdx/README.md.corrected |
| 79 | + @@ -1,13 +1,13 @@ |
| 80 | + Let's look at how good OCaml is with integers and strings: |
| 81 | + ```ocaml |
| 82 | + # 1 + 2;; |
| 83 | + -- : int = 2 |
| 84 | + +- : int = 3 |
| 85 | + # "a" ^ "bc";; |
| 86 | + -- : string = "ab" |
| 87 | + +- : string = "abc" |
| 88 | + ``` |
| 89 | +]} |
| 90 | + |
| 91 | +The test run just failed and dune is showing the diff between what we have |
| 92 | +locally and what should be, according to MDX. |
| 93 | +This uses dune's promotion workflow so at this point you can either investigate |
| 94 | +it further if you're surprised by this diff or if you're happy with it, simply |
| 95 | +accept it by running: |
| 96 | + |
| 97 | +{@sh[ |
| 98 | + dune promote |
| 99 | +]} |
| 100 | + |
| 101 | +Now the documentation is up-to-date and running [dune runtest] again should be |
| 102 | +successful! |
| 103 | + |
| 104 | +{1 Table of Content} |
| 105 | +- {{!page-markdown_syntax}Markdown MDX Syntax} |
| 106 | +- {{!page-mli_syntax}.mli MDX Syntax} |
| 107 | +- {{!page-types_of_blocks}Types of Blocks} |
| 108 | +- {{!page-labels}Labels} |
| 109 | +- {{!page-dune_stanza}Dune stanza} |
| 110 | +- {{!page-preludes}Preludes} |
| 111 | +- {{!page-using_libs}Using Libraries in your code examples} |
0 commit comments