add: use pulldown-cmark instead
This commit is contained in:
parent
3f70a8f465
commit
fe2e61118a
5 changed files with 68 additions and 56 deletions
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "tetratto-shared"
|
||||
description = "Shared stuff for Tetratto"
|
||||
version = "12.0.1"
|
||||
version = "12.0.2"
|
||||
edition = "2024"
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
|
@ -10,8 +10,8 @@ license.workspace = true
|
|||
[dependencies]
|
||||
ammonia = "4.1.1"
|
||||
chrono = "0.4.41"
|
||||
markdown = "1.0.0"
|
||||
hex_fmt = "0.3.0"
|
||||
pulldown-cmark = "0.13.0"
|
||||
rand = "0.9.1"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
sha2 = "0.10.9"
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue