add: render_markdown_dirty
This commit is contained in:
parent
9aed5de097
commit
8786cb4781
3 changed files with 25 additions and 15 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -3353,7 +3353,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-shared"
|
name = "tetratto-shared"
|
||||||
version = "12.0.4"
|
version = "12.0.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ammonia",
|
"ammonia",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-shared"
|
name = "tetratto-shared"
|
||||||
description = "Shared stuff for Tetratto"
|
description = "Shared stuff for Tetratto"
|
||||||
version = "12.0.4"
|
version = "12.0.5"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
|
@ -2,8 +2,7 @@ use ammonia::Builder;
|
||||||
use pulldown_cmark::{Parser, Options, html::push_html};
|
use pulldown_cmark::{Parser, Options, html::push_html};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
/// Render markdown input into HTML
|
pub fn render_markdown_dirty(input: &str) -> String {
|
||||||
pub fn render_markdown(input: &str, proxy_images: bool) -> String {
|
|
||||||
let input = &autolinks(&parse_alignment(input));
|
let input = &autolinks(&parse_alignment(input));
|
||||||
|
|
||||||
let mut options = Options::empty();
|
let mut options = Options::empty();
|
||||||
|
@ -20,6 +19,27 @@ pub fn render_markdown(input: &str, proxy_images: bool) -> String {
|
||||||
let mut html = String::new();
|
let mut html = String::new();
|
||||||
push_html(&mut html, parser);
|
push_html(&mut html, parser);
|
||||||
|
|
||||||
|
html
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clean_html(html: String, allowed_attributes: HashSet<&str>) -> String {
|
||||||
|
Builder::default()
|
||||||
|
.generic_attributes(allowed_attributes)
|
||||||
|
.add_tags(&[
|
||||||
|
"video", "source", "img", "b", "span", "p", "i", "strong", "em", "a", "align",
|
||||||
|
])
|
||||||
|
.rm_tags(&["script", "style", "link", "canvas"])
|
||||||
|
.add_tag_attributes("a", &["href", "target"])
|
||||||
|
.add_url_schemes(&["atto"])
|
||||||
|
.clean(&html)
|
||||||
|
.to_string()
|
||||||
|
.replace("<video loading=", "<video controls loading=")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render markdown input into HTML
|
||||||
|
pub fn render_markdown(input: &str, proxy_images: bool) -> String {
|
||||||
|
let html = render_markdown_dirty(input);
|
||||||
|
|
||||||
let mut allowed_attributes = HashSet::new();
|
let mut allowed_attributes = HashSet::new();
|
||||||
allowed_attributes.insert("id");
|
allowed_attributes.insert("id");
|
||||||
allowed_attributes.insert("class");
|
allowed_attributes.insert("class");
|
||||||
|
@ -30,17 +50,7 @@ pub fn render_markdown(input: &str, proxy_images: bool) -> String {
|
||||||
allowed_attributes.insert("align");
|
allowed_attributes.insert("align");
|
||||||
allowed_attributes.insert("src");
|
allowed_attributes.insert("src");
|
||||||
|
|
||||||
let output = Builder::default()
|
let output = clean_html(html, allowed_attributes);
|
||||||
.generic_attributes(allowed_attributes)
|
|
||||||
.add_tags(&[
|
|
||||||
"video", "source", "img", "b", "span", "p", "i", "strong", "em", "a", "align",
|
|
||||||
])
|
|
||||||
.rm_tags(&["script", "style", "link", "canvas"])
|
|
||||||
.add_tag_attributes("a", &["href", "target"])
|
|
||||||
.add_url_schemes(&["atto"])
|
|
||||||
.clean(&html)
|
|
||||||
.to_string()
|
|
||||||
.replace("<video loading=", "<video controls loading=");
|
|
||||||
|
|
||||||
if proxy_images {
|
if proxy_images {
|
||||||
output.replace(
|
output.replace(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue