[add] markup: markdown syntax support

This commit is contained in:
hkau 2024-03-30 12:29:59 -04:00
parent 947f0a0006
commit 971c202f53
2 changed files with 14 additions and 14 deletions

View File

@ -20,6 +20,7 @@ actix-files = "0.6.5"
actix-web = "4.5.1"
askama = "0.12.1"
awc = { version = "3.4.0", features = ["rustls"] }
comrak = "0.22.0"
dotenv = "0.15.0"
env_logger = "0.11.3"
handlebars = "5.1.2"

View File

@ -1,7 +1,19 @@
use comrak::{markdown_to_html, Options};
use regex::RegexBuilder;
pub fn render(input: &str) -> String {
let mut out = input.to_string();
let mut options = Options::default();
options.extension.table = true;
options.extension.superscript = true;
options.extension.strikethrough = true;
options.extension.autolink = true;
options.extension.header_ids = Option::Some(String::new());
// options.render.unsafe_ = true;
options.render.escape = true;
options.parse.smart = false;
let mut out = markdown_to_html(&input, &options);
// allowed elements
let allowed_elements: Vec<&str> = Vec::from([
@ -64,16 +76,6 @@ pub fn render(input: &str) -> String {
"<div class=\"mdnote note-$2\"><b class=\"mdnote-title\">$3</b></div>\n",
);
// markdown images (normal)
out = regex_replace(
&out,
r"!\[(.*?)\]\((.*?)\)",
"<img alt=\"$1\" title=\"$1\" src=\"$2\" />",
);
// markdown links
out = regex_replace(&out, r"\[(.*?)\]\((.*?)\)", "<a href=\"$2\">$1</a>");
// some bbcode stuff
out = regex_replace(&out, r"\[b\](.*?)\[/b\]", "<strong>$1</strong>"); // bold
out = regex_replace(&out, r"\[i\](.*?)\[/i\]", "<em>$1</em>"); // italic
@ -158,9 +160,6 @@ pub fn render(input: &str) -> String {
out = regex_replace(&out, "(<link.*>)", "");
out = regex_replace(&out, "(<meta.*>)", "");
// auto paragraph
out = regex_replace_dmnl(&out, "^(.*?)\n{2,}", "<p>\n$1\n</p>");
// return
out
}