Basic cours support #44
3 changed files with 25 additions and 13 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -1169,6 +1169,7 @@ dependencies = [
|
|||
"serde_json",
|
||||
"serde_yml",
|
||||
"toml",
|
||||
"urlencoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3536,6 +3537,12 @@ dependencies = [
|
|||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "urlencoding"
|
||||
version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.1"
|
||||
|
|
|
@ -29,3 +29,4 @@ rss = { version = "2.0", features = ["atom"] }
|
|||
lol_html = "1.2"
|
||||
base64 = "0.22.0"
|
||||
mime_guess = "2.0.4"
|
||||
urlencoding = "2.1.3"
|
||||
|
|
|
@ -3,6 +3,7 @@ use base64::engine::general_purpose;
|
|||
use base64::Engine;
|
||||
use comrak::nodes::{AstNode, NodeValue};
|
||||
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 ramhorns::Content;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
@ -196,28 +197,31 @@ fn custom_img_size(html: String) -> String {
|
|||
}
|
||||
|
||||
/// Fix local images
|
||||
fn fix_local_img(path: &str, html: String) -> String {
|
||||
fn fix_images(path: &str, html: String) -> String {
|
||||
rewrite_str(
|
||||
&html,
|
||||
RewriteStrSettings {
|
||||
element_content_handlers: vec![element!("img", |el| {
|
||||
if let Some(src) = el.get_attribute("src") {
|
||||
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) {
|
||||
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);
|
||||
|
||||
el.set_attribute(
|
||||
"src",
|
||||
&format!(
|
||||
"data:{};base64,{}",
|
||||
mime_guess::from_path(img_path).first_or_octet_stream(),
|
||||
image
|
||||
),
|
||||
)
|
||||
el.set_attribute("src", &format!("data:{};base64,{}", mime, image))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
html_content = fix_images(path, html_content);
|
||||
html_content = custom_img_size(html_content);
|
||||
html_content = fix_local_img(path, html_content);
|
||||
|
||||
File {
|
||||
metadata: Metadata {
|
||||
|
|
Loading…
Reference in a new issue