Switched to KaTeX for LaTeX rendering and used cmark-provided delimiters

This commit is contained in:
adrian052 2024-12-12 14:57:38 -06:00 committed by KtorZ
parent 5925dd3bfa
commit 51af046fcb
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 26 additions and 15 deletions

View File

@ -53,6 +53,7 @@ uplc = { path = '../uplc', version = "1.1.7" }
vec1 = "1.10.1"
walkdir.workspace = true
zip = "0.6.4"
katex = "0.4"
[dev-dependencies]
blst = "0.3.11"

View File

@ -13,7 +13,9 @@ use aiken_lang::{
};
use askama::Template;
use itertools::Itertools;
use katex::{Opts, OutputType};
use pulldown_cmark as markdown;
use regex::Regex;
use serde::Serialize;
use serde_json as json;
use std::{
@ -269,9 +271,7 @@ fn generate_module(
};
let rendered_content = convert_latex_markers(
inject_math_library(
module.render().expect("Module documentation template rendering"),
)
);
(
@ -285,21 +285,31 @@ fn generate_module(
fn convert_latex_markers(input: String) -> String {
input.replace("#[", "\\(")
.replace("]#", "\\)")
}
let re_inline = Regex::new(r#"<span class="math math-inline">\s*(.+?)\s*</span>"#).unwrap();
let re_block = Regex::new(r#"<span class="math math-display">\s*(.+?)\s*</span>"#).unwrap();
fn inject_math_library(html: String) -> String {
let mathjax_script = r#"
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
<script>
MathJax.typeset();
</script>
"#;
let opts_inline = Opts::builder()
.display_mode(false) // Inline math
.output_type(OutputType::Mathml)
.build()
.unwrap();
html.replace("</head>", &format!("{}\n</head>", mathjax_script))
let opts_block = katex::Opts::builder()
.display_mode(true) // Block math
.output_type(OutputType::Mathml)
.build()
.unwrap();
let input = re_inline.replace_all(&input, |caps: &regex::Captures| {
let formula = &caps[1];
katex::render_with_opts(formula, &opts_inline).unwrap_or_else(|_| formula.to_string())
});
re_block.replace_all(&input, |caps: &regex::Captures| {
let formula = &caps[1];
katex::render_with_opts(formula, &opts_block).unwrap_or_else(|_| formula.to_string())
})
.to_string()
}
fn generate_static_assets(search_indexes: Vec<SearchIndex>) -> Vec<DocFile> {