Skip to content

Preliminary {math } #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ ocaml-re/
result/
tyxml/
uutf/

# Mac things
.DS_Store
4 changes: 4 additions & 0 deletions src/html/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ and raw_markup (t : Raw_markup.t) =
the HTML tree from there.
*)
[ Html.Unsafe.data content ]
| "math" ->
[
Html.span ~a:[ Html.a_class [ "odoc-katex-math" ] ] [ Html.txt content ];
]
| _ -> []

and source k ?a (t : Source.t) =
Expand Down
42 changes: 42 additions & 0 deletions src/html/tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,29 @@ let page_creator ?(theme_uri = Relative None) ?(support_uri = Relative None)

let odoc_css_uri = file_uri theme_uri "odoc.css" in
let highlight_js_uri = file_uri support_uri "highlight.pack.js" in
let katex_css_uri =
"https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css"
in
let katex_css_integrity =
"sha384-MlJdn/WNKDGXveldHDdyRP1R4CTHr3FeuDNfhsLPYrq2t0UBkUdK2jyTnXPEK1NQ"
in
let katex_js_uri =
"https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.js"
in
let katex_js_integrity =
"sha384-VQ8d8WVFw0yHhCk5E8I86oOhv48xLpnDZx5T9GogA/Y84DcCKWXDmSDfn13bzFZY"
in

Html.head
(Html.title (Html.txt title_string))
[
Html.link ~rel:[ `Stylesheet ] ~href:odoc_css_uri ();
Html.link ~rel:[ `Stylesheet ] ~href:katex_css_uri
~a:
[
Html.a_integrity katex_css_integrity; Html.a_crossorigin `Anonymous;
]
();
Html.meta ~a:[ Html.a_charset "utf-8" ] ();
Html.meta
~a:[ Html.a_name "generator"; Html.a_content "odoc %%VERSION%%" ]
Expand All @@ -55,6 +73,30 @@ let page_creator ?(theme_uri = Relative None) ?(support_uri = Relative None)
();
Html.script ~a:[ Html.a_src highlight_js_uri ] (Html.txt "");
Html.script (Html.txt "hljs.initHighlightingOnLoad();");
Html.script
~a:
[
Html.a_src katex_js_uri;
Html.a_integrity katex_js_integrity;
Html.a_crossorigin `Anonymous;
]
(Html.txt "");
(* Unsafe is necessary to avoid escaping parts of the following code. *)
Html.script
(Html.Unsafe.data
{|
document.addEventListener("DOMContentLoaded", function () {
var elements = document.getElementsByClassName("odoc-katex-math");
for (var i = 0; i < elements.length; i++) {
var el = elements[i];
var content = el.textContent;
var new_el = document.createElement("span");
new_el.setAttribute("class", "odoc-katex-math-rendered");
katex.render(content, new_el, { throwOnError: false });
el.replaceWith(new_el);
}
});
|});
]
in

Expand Down
1 change: 1 addition & 0 deletions src/latex/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ let raw_markup (t : Raw_markup.t) =
let target, content = t in
match Astring.String.Ascii.lowercase target with
| "latex" | "tex" -> [ Raw content ]
| "math" -> [ Raw (Printf.sprintf {|$%s$|} content) ]
| _ -> []

let source k (t : Source.t) =
Expand Down