very basic md in md support
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval

This commit is contained in:
Mylloon 2024-03-31 23:20:17 +02:00
parent 6d1973ff6d
commit 3bbfc656bb
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 25 additions and 13 deletions

7
Cargo.lock generated
View file

@ -1169,6 +1169,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_yml", "serde_yml",
"toml", "toml",
"urlencoding",
] ]
[[package]] [[package]]
@ -3536,6 +3537,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]] [[package]]
name = "utf8parse" name = "utf8parse"
version = "0.2.1" version = "0.2.1"

View file

@ -29,3 +29,4 @@ rss = { version = "2.0", features = ["atom"] }
lol_html = "1.2" lol_html = "1.2"
base64 = "0.22.0" base64 = "0.22.0"
mime_guess = "2.0.4" mime_guess = "2.0.4"
urlencoding = "2.1.3"

View file

@ -3,6 +3,7 @@ use base64::engine::general_purpose;
use base64::Engine; use base64::Engine;
use comrak::nodes::{AstNode, NodeValue}; use comrak::nodes::{AstNode, NodeValue};
use comrak::{format_html, parse_document, Arena, ComrakOptions, ListStyleType}; use comrak::{format_html, parse_document, Arena, ComrakOptions, ListStyleType};
use lol_html::html_content::ContentType;
use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings}; use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings};
use ramhorns::Content; use ramhorns::Content;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
@ -196,28 +197,31 @@ fn custom_img_size(html: String) -> String {
} }
/// Fix local images /// Fix local images
fn fix_local_img(path: &str, html: String) -> String { fn fix_images(path: &str, html: String) -> String {
rewrite_str( rewrite_str(
&html, &html,
RewriteStrSettings { RewriteStrSettings {
element_content_handlers: vec![element!("img", |el| { element_content_handlers: vec![element!("img", |el| {
if let Some(src) = el.get_attribute("src") { if let Some(src) = el.get_attribute("src") {
let img_src = Path::new(path).parent().unwrap(); let img_src = Path::new(path).parent().unwrap();
let img_path = img_src.join(src); let img_path = urlencoding::decode(img_src.join(src).to_str().unwrap())
.unwrap()
.to_string();
if let Ok(file) = fs::read_to_string(&img_path) { if let Ok(file) = fs::read_to_string(&img_path) {
let mime = mime_guess::from_path(&img_path).first_or_octet_stream();
if mime == "text/markdown" {
el.replace(
&read_md(&img_path, &file, TypeFileMetadata::Generic).content,
ContentType::Html,
)
} else {
let image = general_purpose::STANDARD.encode(file); let image = general_purpose::STANDARD.encode(file);
el.set_attribute( el.set_attribute("src", &format!("data:{};base64,{}", mime, image))
"src",
&format!(
"data:{};base64,{}",
mime_guess::from_path(img_path).first_or_octet_stream(),
image
),
)
.unwrap(); .unwrap();
} }
} }
}
Ok(()) Ok(())
})], })],
@ -246,8 +250,8 @@ pub fn read_md(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> F
let mut html_content = String::from_utf8(html).unwrap(); let mut html_content = String::from_utf8(html).unwrap();
html_content = fix_images(path, html_content);
html_content = custom_img_size(html_content); html_content = custom_img_size(html_content);
html_content = fix_local_img(path, html_content);
File { File {
metadata: Metadata { metadata: Metadata {