OCaml notebooks as a WebComponent
  • OCaml 92.4%
  • CSS 5.8%
  • Dune 1.8%
Find a file
2026-05-04 11:17:16 +02:00
.github/workflows fix CI 2025-05-13 21:27:27 +02:00
bin add X_ocaml_lib.output_html 2025-07-20 19:58:24 +02:00
jsoo-code-mirror@0fc94befd9 prototype 2025-05-12 02:57:16 +02:00
merlin-js@5c12214d7e prototype 2025-05-12 02:57:16 +02:00
ppxlib_register prototype 2025-05-12 02:57:16 +02:00
protocol ocamlformat: custom config attribute 2025-07-21 20:45:18 +02:00
src update ocamlformat.0.29.0 2026-05-04 11:17:16 +02:00
worker add quiet, loud, loud_once directives (#15) 2026-05-04 11:15:58 +02:00
worker-lib add X_ocaml_lib.output_html 2025-07-20 19:58:24 +02:00
.gitignore prototype 2025-05-12 02:57:16 +02:00
.gitmodules prototype 2025-05-12 02:57:16 +02:00
.ocamlformat update ocamlformat.0.29.0 2026-05-04 11:17:16 +02:00
dune add integrity check 2025-07-26 13:57:49 +02:00
dune-project fix opam install 2025-06-29 19:32:29 +02:00
LICENSE fix CI 2025-05-13 21:27:27 +02:00
ppxlib_register.opam fix CI 2025-05-13 21:27:27 +02:00
README.md add integrity check 2025-07-26 13:57:49 +02:00
x-ocaml.opam fix CI 2025-05-13 21:27:27 +02:00

Embed OCaml notebooks in any web page thanks to WebComponents! Just copy and paste the following script in your html page source to load the integration:

<script async
  src="https://cdn.jsdelivr.net/gh/art-w/x-ocaml.js@6/x-ocaml.js"
  src-worker="https://cdn.jsdelivr.net/gh/art-w/x-ocaml.js@6/x-ocaml.worker+effects.js"
  integrity="sha256-3ITn2LRgP/8Rz6oqP5ZQTysesNaSi6/iEdbDvBfyCSE="
  crossorigin="anonymous"
></script>

This will introduce a new html tag <x-ocaml> to present OCaml code, for example:

<x-ocaml>let x = 42</x-ocaml>

The script will initialize a CodeMirror editor integrated with the OCaml interpreter, Merlin and OCamlformat (all running in a web worker). Check out the online demo for more details, including how to load additional OCaml libraries and ppx in your page.

For an even easier integration, @patricoferris made a command-line tool xocmd to convert markdown files to use <x-ocaml>!

Compilation

To avoid relying on a public CDN and host your own copy of the x-ocaml scripts, you can reproduce the javascript files with:

$ git clone --recursive https://github.com/art-w/x-ocaml
$ cd x-ocaml

# Install the dependencies with either dune:
x-ocaml/ $ dune pkg lock
# Or with opam:
x-ocaml/ $ opam update && opam install . --deps-only

# Make sure to use the release profile to optimize the js file size
x-ocaml/ $ dune build --profile=release

x-ocaml/ $ ls *.js
x-ocaml.js  x-ocaml.worker+effects.js  x-ocaml.worker.js

Acknowledgments

This project was heavily inspired by the amazing sketch.sh, @jonludlam's notebooks in Odoc, blogaml by @panglesd, and all the wonderful people who made Try OCaml and other online playgrounds! It was made possible thanks to the invaluable js_of_ocaml-toplevel library, the magical merlin-js by @voodoos, the excellent CodeMirror bindings by @patricoferris, the guidance of @Julow on ocamlformat and the javascript expertise of @xvw.