add: use pulldown-cmark instead

This commit is contained in:
trisua 2025-07-20 15:28:44 -04:00
parent 3f70a8f465
commit fe2e61118a
5 changed files with 68 additions and 56 deletions

View file

@ -1,37 +1,24 @@
use ammonia::Builder;
use markdown::{to_html_with_options, Options, CompileOptions, ParseOptions, Constructs};
use pulldown_cmark::{Parser, Options, html::push_html};
use std::collections::HashSet;
/// Render markdown input into HTML
pub fn render_markdown(input: &str) -> String {
pub fn render_markdown(input: &str, proxy_images: bool) -> String {
let input = &parse_alignment(input);
let options = Options {
compile: CompileOptions {
allow_any_img_src: false,
allow_dangerous_html: true,
allow_dangerous_protocol: true,
gfm_task_list_item_checkable: false,
gfm_tagfilter: false,
..Default::default()
},
parse: ParseOptions {
constructs: Constructs {
math_flow: true,
math_text: true,
..Constructs::gfm()
},
gfm_strikethrough_single_tilde: false,
math_text_single_dollar: false,
mdx_expression_parse: None,
mdx_esm_parse: None,
..Default::default()
},
};
let html = match to_html_with_options(input, &options) {
Ok(h) => h,
Err(e) => e.to_string(),
};
let mut options = Options::empty();
options.insert(Options::ENABLE_STRIKETHROUGH);
options.insert(Options::ENABLE_GFM);
options.insert(Options::ENABLE_FOOTNOTES);
options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_HEADING_ATTRIBUTES);
options.insert(Options::ENABLE_SUBSCRIPT);
options.insert(Options::ENABLE_SUPERSCRIPT);
let parser = Parser::new_ext(input, options);
let mut html = String::new();
push_html(&mut html, parser);
let mut allowed_attributes = HashSet::new();
allowed_attributes.insert("id");
@ -43,7 +30,7 @@ pub fn render_markdown(input: &str) -> String {
allowed_attributes.insert("align");
allowed_attributes.insert("src");
Builder::default()
let output = Builder::default()
.generic_attributes(allowed_attributes)
.add_tags(&[
"video", "source", "img", "b", "span", "p", "i", "strong", "em", "a", "align",
@ -53,11 +40,16 @@ pub fn render_markdown(input: &str) -> String {
.add_url_schemes(&["atto"])
.clean(&html)
.to_string()
.replace(
.replace("<video loading=", "<video controls loading=");
if proxy_images {
output.replace(
"src=\"http",
"loading=\"lazy\" src=\"/api/v1/util/proxy?url=http",
)
.replace("<video loading=", "<video controls loading=")
} else {
output
}
}
fn parse_alignment_line(line: &str, output: &mut String, buffer: &mut String, is_in_pre: bool) {