feat: add optional pgn_to_tex_js entry point

This commit is contained in:
2026-10-23 16:38:24 +02:00
committed by owenrees
parent 135491e915
commit 786bad8956
6 changed files with 89 additions and 10 deletions
+36 -4
View File
@@ -111,17 +111,49 @@ game_tex goes here
## Compile a JavaScript version of the library
With the library installed, you can compile the code to JavaScript by running:
The JavaScript build is an optional target. Dune skips it when the JavaScript dependencies are not installed. Install them with:
```
opam install js_of_ocaml js_of_ocaml-ppx
```
Then compile the code to JavaScript by running:
```
make build
```
This will generate the JavaScript file at `_build/default/bin/main.bc.js`
or build just the artifact:
This JavaScript file is minified and in CJS format, working in both Node and in the browser.
```
dune build bin/pgn_to_tex_js.bc.js
```
A single function `to_tex` is exposed, accepting the same arguments as the main library.
This generates the JavaScript file at `_build/default/bin/pgn_to_tex_js.bc.js`.
This JavaScript file is minified and in CJS format, working in both Node and the browser.
A single global `to_tex` function is exposed, accepting the same three arguments as the main library:
```js
to_tex(pgnString, diagramJsonString, displayClock);
```
For example:
```js
require("./_build/default/bin/pgn_to_tex_js.bc.js");
const gameTex = globalThis.to_tex("1. e4 e5 2. Nf3", "{}", false);
```
Pass `"{}"` (or an empty string) when there are no diagrams, and `false` to omit clock times. In the browser, the function is available as `window.to_tex` after loading the artifact.
After building the artifact, you can run the Node smoke test with:
```
node test/js/smoke.js
```
## Dependencies
+3 -4
View File
@@ -1,8 +1,7 @@
(executable
(name main)
(name pgn_to_tex_js)
(modes js)
(optional)
(libraries pgn_to_tex js_of_ocaml yojson)
(preprocess
(pps js_of_ocaml-ppx))
(enabled_if
(= %{env:CI=false} "false")))
(pps js_of_ocaml-ppx)))
+6 -2
View File
@@ -15,5 +15,9 @@ let convert_js pgn_js diagram_json_js display_clock =
let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in
Js.string result
(* Exporting the module to the global JavaScript scope *)
let () = Js.export "to_tex" (Js.wrap_callback convert_js)
(* Exporting the module to the global JavaScript scope, including when the
artifact is loaded as a CommonJS module in Node. *)
let () =
let callback = Js.wrap_callback convert_js in
Js.export "to_tex" callback;
Js.Unsafe.set Js.Unsafe.global "to_tex" callback
+6
View File
@@ -38,6 +38,12 @@
(and
:with-test
(>= 2.2.7)))
(js_of_ocaml
(and
:with-test))
(js_of_ocaml-ppx
(and
:with-test))
("ocamlformat"
(and :with-dev-setup)))
(tags
+2
View File
@@ -18,6 +18,8 @@ depends: [
"menhir" {>= "20260209"}
"yojson" {>= "3.0"}
"ounit2" {with-test & >= "2.2.7"}
"js_of_ocaml" {with-test}
"js_of_ocaml-ppx" {with-test}
"ocamlformat" {with-dev-setup}
"odoc" {with-doc}
]
+36
View File
@@ -0,0 +1,36 @@
"use strict";
const assert = require("node:assert");
const path = require("node:path");
const artifact = path.resolve(
__dirname,
"../../_build/default/bin/pgn_to_tex_js.bc.js",
);
require(artifact);
const toTex = globalThis.to_tex;
assert.strictEqual(
typeof toTex,
"function",
"the generated artifact should export a global to_tex function",
);
const latex = toTex("1. e4 e5 2. Nf3", "{}", false);
assert.strictEqual(typeof latex, "string", "to_tex should return a string");
assert.ok(
latex.includes("\\textbf{1.}"),
"rendered LaTeX should contain the first move number",
);
assert.ok(
latex.includes("\\textbf{e4}"),
"rendered LaTeX should contain the first move",
);
assert.ok(
latex.includes("\\textbf{Nf3}"),
"rendered LaTeX should contain the third move",
);
console.log("js smoke test passed");