add markdown files integration
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval

This commit is contained in:
Mylloon 2024-04-01 00:06:04 +02:00
parent 3bbfc656bb
commit f45bc1346c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -102,6 +102,15 @@ pub struct Metadata {
pub syntax_highlight: bool, pub syntax_highlight: bool,
} }
impl Metadata {
/// Update current metadata boolean fields, keeping true ones
fn merge(&mut self, other: Metadata) {
self.math = self.math || other.math;
self.mermaid = self.mermaid || other.mermaid;
self.syntax_highlight = self.syntax_highlight || other.syntax_highlight;
}
}
/// File description /// File description
#[derive(Content, Debug)] #[derive(Content, Debug)]
pub struct File { pub struct File {
@ -196,8 +205,16 @@ fn custom_img_size(html: String) -> String {
.unwrap() .unwrap()
} }
/// Fix local images /// Fix local images to base64 and integration of markdown files
fn fix_images(path: &str, html: String) -> String { fn fix_images_and_integration(path: &str, html: String) -> (String, Metadata) {
let mut metadata = Metadata {
info: FileMetadata::default(),
math: false,
mermaid: false,
syntax_highlight: false,
};
(
rewrite_str( rewrite_str(
&html, &html,
RewriteStrSettings { RewriteStrSettings {
@ -210,10 +227,9 @@ fn fix_images(path: &str, html: String) -> 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(); let mime = mime_guess::from_path(&img_path).first_or_octet_stream();
if mime == "text/markdown" { if mime == "text/markdown" {
el.replace( let data = read_md(&img_path, &file, TypeFileMetadata::Generic);
&read_md(&img_path, &file, TypeFileMetadata::Generic).content, el.replace(&data.content, ContentType::Html);
ContentType::Html, metadata.merge(data.metadata);
)
} else { } else {
let image = general_purpose::STANDARD.encode(file); let image = general_purpose::STANDARD.encode(file);
@ -228,7 +244,9 @@ fn fix_images(path: &str, html: String) -> String {
..RewriteStrSettings::default() ..RewriteStrSettings::default()
}, },
) )
.unwrap() .unwrap(),
metadata,
)
} }
/// Transform markdown string to File structure /// Transform markdown string to File structure
@ -250,16 +268,20 @@ 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); let children_metadata;
(html_content, children_metadata) = fix_images_and_integration(path, html_content);
html_content = custom_img_size(html_content); html_content = custom_img_size(html_content);
File { let mut final_metadata = Metadata {
metadata: Metadata {
info: metadata, info: metadata,
mermaid: check_mermaid(root, mermaid_name), mermaid: check_mermaid(root, mermaid_name),
syntax_highlight: check_code(root, &[mermaid_name.into()]), syntax_highlight: check_code(root, &[mermaid_name.into()]),
math: check_math(&html_content), math: check_math(&html_content),
}, };
final_metadata.merge(children_metadata);
File {
metadata: final_metadata,
content: html_content, content: html_content,
} }
} }