Accept displaymath and math environments

This commit is contained in:
gloamxun committed 2026-10-25 02:21:47 +08:00
1 parent 4f59f6e98c
commit 81e6a2befc
4 files changed
+33 -5

No files matched your search

+29 -1
View File
@@ -4033,7 +4033,9 @@ fn split_inline_math(text: &str) -> Vec<(bool, String)> {
index += 2;
continue;
}
let opener = if rest.starts_with("\\(") {
let opener = if rest.starts_with("\\begin{math}") {
Some(("\\begin{math}".len(), "\\end{math}"))
} else if rest.starts_with("\\(") {
Some((2, "\\)"))
} else if rest.starts_with('$') {
Some((1, "$"))
@@ -4618,6 +4620,12 @@ fn heading_command(raw: &str) -> Option<(u8, bool, String, Option<String>)> {
}
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some(inner) = raw
.strip_prefix("\\begin{displaymath}")
.and_then(|text| text.strip_suffix("\\end{displaymath}"))
{
return parse_paragraph(&format!("$$\n{}\n$$", inner));
}
for (environment, starred, centered) in [
("align", false, false),
("align*", true, false),
@@ -8664,6 +8672,26 @@ A definition.
);
}
#[test]
fn displaymath_environment_becomes_a_math_block() {
let blocks =
paragraphs("\\begin{displaymath}\nx = 1\n\\end{displaymath}").expect("paragraphs");
assert_eq!(blocks.len(), 1);
assert!(blocks[0].math);
assert_eq!(blocks[0].text, "x = 1");
assert_eq!(blocks[0].equation_number, None);
}
#[test]
fn math_environment_becomes_inline_math() {
let svg = preview_json("Body \\begin{math}x\\end{math}.").expect("preview");
assert!(
svg.contains("font-style=\\\"italic\\\""),
"missing math: {svg}"
);
assert!(!svg.contains("begin{math}"), "environment leaked: {svg}");
}
#[test]
fn inline_math_stays_literal_in_sized_paragraphs() {
let blocks = paragraphs("\\large Value $x$ tail.").expect("paragraphs");