From 533d2c10441a631ba007bc7d135a329ae817cf20 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 28 Nov 2024 00:18:53 +0100 Subject: [PATCH] fix stack overflow --- src/utils/markdown.rs | 24 +++++++++++++++++++----- src/utils/misc.rs | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/utils/markdown.rs b/src/utils/markdown.rs index 1f99ec1..cdfd12d 100644 --- a/src/utils/markdown.rs +++ b/src/utils/markdown.rs @@ -104,7 +104,12 @@ fn custom_img_size(html: &str) -> String { } /// Fix local images to base64 and integration of markdown files -fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { +fn fix_images_and_integration( + path: &str, + html: &str, + metadata_type: MType, + recursive: bool, +) -> (String, Metadata) { let mut metadata = Metadata { info: MFile::default(), math: false, @@ -125,12 +130,14 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { 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" { + if recursive && mime == "text/markdown" { let mut options = get_options(); options.extension.footnotes = false; - let data = read_md(&img_path, &file, MType::Generic, Some(options)); + let data = + read_md(&img_path, &file, metadata_type, Some(options), false); el.replace(&data.content, ContentType::Html); // Store the metadata for later merging @@ -158,7 +165,13 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { } /// Transform markdown string to File structure -pub fn read_md(path: &str, raw_text: &str, metadata_type: MType, options: Option) -> File { +pub fn read_md( + path: &str, + raw_text: &str, + metadata_type: MType, + options: Option, + recursive: bool, +) -> File { let arena = Arena::new(); let mut opt = options.map_or_else(get_options, |specific_opt| specific_opt); @@ -181,7 +194,8 @@ pub fn read_md(path: &str, raw_text: &str, metadata_type: MType, options: Option let children_metadata; let mail_obfsucated; - (html_content, children_metadata) = fix_images_and_integration(path, &html_content); + (html_content, children_metadata) = + fix_images_and_integration(path, &html_content, metadata_type, recursive); html_content = custom_img_size(&html_content); (html_content, mail_obfsucated) = mail_obfuscation(&html_content); diff --git a/src/utils/misc.rs b/src/utils/misc.rs index 693940c..c9885fd 100644 --- a/src/utils/misc.rs +++ b/src/utils/misc.rs @@ -63,7 +63,7 @@ pub fn read_file(filename: String, expected_file: MType) -> Option { .and_then(|ext| match ext.to_str().unwrap() { "pdf" => fs::read(filename).map_or(None, |bytes| Some(read_pdf(bytes))), _ => fs::read_to_string(&filename).map_or(None, |text| { - Some(read_md(&filename, &text, expected_file, None)) + Some(read_md(&filename, &text, expected_file, None, true)) }), }) }