Skip to content

Commit 373b3dd

Browse files
committed
Temporarily import Basic usage section from the WIP manual
Signed-off-by: Nathan Rebours <[email protected]>
1 parent 4a0b817 commit 373b3dd

File tree

1 file changed

+89
-11
lines changed

1 file changed

+89
-11
lines changed

README.md

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,106 @@ $ opam install mdx
1818
If you want to contribute to the project, please see the
1919
[CONTRIBUTING.md](CONTRIBUTING.md).
2020

21+
### Basic Usage
22+
23+
You can use MDX with your Markdown or `.mli` documentation, which ensures
24+
code in multi-line or verbatim code blocks is correct.
25+
26+
To enable MDX, you need to add `(mdx)` stanzas to the right `dune` files. Before
27+
that you must enable MDX for your project by adding the following to
28+
your `dune-project`:
29+
30+
```
31+
(using mdx 0.2)
32+
```
33+
34+
Then add the following in the relevant `dune` file:
35+
```
36+
(mdx)
37+
```
38+
That enables MDX on all markdown files in the folder.
39+
The MDX stanza can be further configured. Please visit the relevant section of
40+
[dune's manual](https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4)
41+
for more information.
42+
43+
MDX supports various type of code blocks but the most common are OCaml toplevel
44+
blocks. We illustrate one in our example below. In a Markdown file, you
45+
would write something similar to this:
46+
47+
````markdown
48+
Let's look at how good OCaml is with integers and strings:
49+
```ocaml
50+
# 1 + 2;;
51+
- : int = 2
52+
# "a" ^ "bc";;
53+
- : string = "ab"
54+
```
55+
````
56+
57+
The content of the toplevel blocks looks just like an interactive toplevel
58+
session. Phrases, i.e., the toplevel "input", start with a `#` and end with `;;`.
59+
The toplevel evaluation, or "output" follows each phrase.
60+
61+
Now you probably have noticed that `1 + 2` is not equal to `2` nor is `"a" ^ "bc"`
62+
to `"ab"`. Somebody must have updated the phrases, but then forgot to update
63+
the evaluation.
64+
65+
That's exactly why MDX is here!
66+
67+
If you enable MDX for this file and then ran `dune runtest`, this would be the
68+
result:
69+
70+
````
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+
```
98+
dune promote
99+
```
100+
101+
Now the documentation is up-to-date and running `dune runtest` again should be
102+
successful!
103+
21104
### Supported Extensions
22105

23106
#### Labels
24107

25-
The blocks in markdown files can be parameterized by `mdx`-specific labels, that
108+
The blocks can be parameterized by `mdx`-specific labels, that
26109
will change the way `mdx` interprets the block.
27110

28-
The syntax is: `<!-- $MDX LABELS -->`, where `LABELS` is a list of valid labels
29-
separated by a comma. This line has to immediately precede the block it is
30-
attached to.
111+
The markdown syntax is: `<!-- $MDX LABELS -->`, where `LABELS` is a list of
112+
valid labels separated by a comma. This line has to immediately precede the
113+
block it is attached to.
31114

32115
<!-- $MDX LABELS -->
33116
```ocaml
34117
```
35118

36-
This syntax is the recommended way to define labels since `mdx` 1.7.0, to use
37-
the previous syntax please refer to the
38-
[mdx 1.6.0 README](https://github.com/realworldocaml/mdx/blob/1.6.0/README.md).
39-
40-
It is also possible to use labels in OCaml interface files (`mli`), the syntax
41-
for this is is slightly different to match the conventions of OCaml
42-
documentation comments:
119+
The `.mli` syntax for this is is slightly different to match the conventions of
120+
OCaml documentation comments:
43121

44122
(** This is an documentation comment with an ocaml block
45123
{@ocaml LABELS [

0 commit comments

Comments
 (0)