feat: improve worksheet
This commit is contained in:
@@ -113,6 +113,32 @@ pub fn to_plain(src: &str) -> String {
|
||||
out.trim().to_string()
|
||||
}
|
||||
|
||||
/// Converts authoring markup to Pandoc-flavoured Markdown, for a Quarto document.
|
||||
///
|
||||
/// Subscripts and superscripts become Pandoc's `~x~` and `^x^`, the symbol table
|
||||
/// renders as Unicode, and the bold, italic, and inline-code spans are already
|
||||
/// Markdown, so they pass through unchanged. Paragraph breaks are kept. Nothing is
|
||||
/// HTML-escaped, because the consumer is a Markdown renderer rather than a page.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `src` - the authoring source.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Pandoc Markdown, trimmed, with paragraph breaks preserved.
|
||||
pub fn to_markdown(src: &str) -> String {
|
||||
let symbolized = apply_symbols(src, false);
|
||||
let mut out = wrap_bracket(&symbolized, "#sub[", "~", "~");
|
||||
out = wrap_bracket(&out, "#sup[", "^", "^");
|
||||
out.lines()
|
||||
.map(|l| l.trim_end())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
.trim()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// Passes authoring markup through for Typst.
|
||||
///
|
||||
/// The markup is already a Typst subset, so this only normalizes whitespace and
|
||||
@@ -379,4 +405,21 @@ mod tests {
|
||||
assert_eq!(to_typst("a @ b"), "a \\@ b");
|
||||
assert_eq!(to_typst("x < y"), "x \\< y");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn markdown_uses_pandoc_scripts_and_unicode_symbols() {
|
||||
assert_eq!(to_markdown("H#sub[2]O"), "H~2~O");
|
||||
assert_eq!(to_markdown("x#sup[2]"), "x^2^");
|
||||
assert_eq!(
|
||||
to_markdown("K#sub[m] #sym.approx 5 mM"),
|
||||
"K~m~ \u{2248} 5 mM"
|
||||
);
|
||||
// Bold, italic, and code are already Markdown.
|
||||
assert_eq!(
|
||||
to_markdown("**bold** and *em* and `code`"),
|
||||
"**bold** and *em* and `code`"
|
||||
);
|
||||
// Paragraph breaks survive.
|
||||
assert_eq!(to_markdown("one\n\ntwo"), "one\n\ntwo");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user