Skip to content
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

Add KaTeX support for documentation generation #1070

Closed
wants to merge 2 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
1 change: 1 addition & 0 deletions crates/aiken-project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 36 additions & 3 deletions crates/aiken-project/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -268,17 +270,48 @@ fn generate_module(
timestamp: timestamp.as_secs().to_string(),
};

let rendered_content = convert_latex_markers(
module.render().expect("Module documentation template rendering"),
);

(
search_indexes,
DocFile {
path: PathBuf::from(format!("{}.html", module.module_name)),
content: module
.render()
.expect("Module documentation template rendering"),
content: rendered_content,
},
)
}


fn convert_latex_markers(input: String) -> String {
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();

let opts_inline = Opts::builder()
.display_mode(false) // Inline math
.output_type(OutputType::Mathml)
.build()
.unwrap();

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> {
let mut assets: Vec<DocFile> = vec![];

Expand Down