Skip to content

Commit dad5f33

Browse files
committed
TMP: Add an MDX manual using odoc .mld files
Signed-off-by: Nathan Rebours <[email protected]>
1 parent 426bd17 commit dad5f33

File tree

9 files changed

+247
-0
lines changed

9 files changed

+247
-0
lines changed

doc/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(documentation
2+
(package mdx))

doc/dune_stanza.mld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{0 Dune Stanza}
2+

doc/index.mld

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

doc/labels.mld

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{0 Labels}
2+
3+
MDX blocks behaviour can be customised through the use of labels.
4+
5+
This section documents in detail what each existing label does and how to use
6+
it.
7+
8+
{1:dir Dir}
9+
10+
{2 Description}
11+
12+
{2 Syntax}
13+
14+
{2 Applies to}
15+
16+
{2 Default}
17+
18+
{1:source_tree Source Tree}
19+
20+
{2 Description}
21+
22+
{2 Syntax}
23+
24+
{2 Applies to}
25+
26+
{2 Default}
27+
28+
{1:file File}
29+
30+
{2 Description}
31+
32+
{2 Syntax}
33+
34+
{2 Applies to}
35+
36+
{2 Default}
37+
38+
{1:part Part}
39+
40+
{2 Description}
41+
42+
{2 Syntax}
43+
44+
{2 Applies to}
45+
46+
{2 Default}
47+
48+
{1:env Env}
49+
50+
{2 Description}
51+
52+
This label allows you to assign an environment to an OCaml block. That means
53+
you benefit from whole the code that has been previously evaluated in that
54+
environment, be it from other code blocks or {{!page-preludes}preludes}.
55+
56+
{2 Syntax}
57+
58+
[env=<value>]
59+
60+
The environement label expects a single string value which corresponds to the
61+
name of the environment to use.
62+
63+
[.mli] example:
64+
{v
65+
(** Here is how to use this function:
66+
{@ocaml env=foo[
67+
# f 0;;
68+
- : int = 0
69+
]} *)
70+
v}
71+
72+
[.md] example:
73+
{@markdown[
74+
Here is how to use this function:
75+
<!-- $MDX env=foo -->
76+
```ocaml
77+
# f 0;;
78+
- : int = 0
79+
```
80+
]}
81+
82+
{2 Applies to}
83+
84+
- {{!page-types_of_blocks.ocaml_toplevel} OCaml Toplevel Blocks}
85+
- {{!page-types_of_blocks.ocaml} OCaml Blocks}
86+
87+
{2 Default}
88+
89+
When absent, the block will be evaluated in the default environment, e.g.
90+
the environment shared by all blocks without an [env] label.

doc/markdown_syntax.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

doc/mli_syntax.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

doc/preludes.mld

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{0 Preludes}
2+
3+
Preludes are fragments of code that can be evaluated ahead of running the
4+
code in OCaml and OCaml toplevel MDX blocks. They can be used to globally
5+
open modules, register toplevel printers, set some globals like
6+
[Printexc.record_backtrace], you name it!
7+
8+
They are especially useful if you want to hide or share this kind of setup
9+
phase, e.g. if you are writing polished documentation you want to publish
10+
or you are using MDX to add tests in your mli files directly.
11+
12+
TODO

doc/types_of_blocks.mld

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{0 Types of Blocks}
2+
3+
MDX searches for code blocks within your documentation to make sure they are up
4+
to date.
5+
It supports different type of block contents, each have their own behaviour.
6+
7+
If a code block does not belong to one of the categories described below, MDX
8+
will simply ignore it.
9+
10+
The type of a block can either be set explicitly using a label, otherwise MDX
11+
will try to infer the type of the block based on its language tag, labels and
12+
content.
13+
Setting the block type explicitly allows MDX to better report syntax errors
14+
or invalid labels.
15+
16+
{1:ocaml_toplevel OCaml Toplevel Blocks}
17+
18+
OCaml Toplevel Blocks are composed of a sequence of toplevel phrases, starting
19+
with a [#] and a space and ending with [;;] followed by the output of the
20+
toplevel evaluation of the phrase.
21+
22+
23+
{1:ocaml OCaml Blocks}
24+
25+
{1:file_include File Include Blocks}
26+
27+
{1:shell Shell Blocks}

doc/using_libs.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

0 commit comments

Comments
 (0)