Started work on markdown image support

This commit is contained in:
Iaphetes 2024-12-21 11:49:11 +01:00
parent a1de20f0ec
commit 4f7836d296
4 changed files with 49 additions and 26 deletions

View file

@ -1,4 +1,5 @@
use comrak::{format_html, nodes::NodeValue, parse_document, Arena, Options};
use tauri::Manager;
use tauri_plugin_fs::FsExt;
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
@ -9,25 +10,31 @@ fn greet(name: &str) -> String {
#[tauri::command]
fn parse_markdown(document: &str) -> String {
let arena = Arena::new();
let mut rendered_markdown: String = String::new();
tauri::Builder::default().setup(move |app| {
let webview = app.get_webview_window("main").unwrap();
let arena = Arena::new();
// Parse the document into a root `AstNode`
let mut options = Options::default();
options.render.unsafe_ = true;
// options.render.hardbreaks = true;
let root = parse_document(&arena, document, &options);
// Parse the document into a root `AstNode`
let mut options = Options::default();
options.render.unsafe_ = true;
// options.render.hardbreaks = true;
let root = parse_document(&arena, document, &options);
// Iterate over all the descendants of root.
for node in root.descendants() {
if let NodeValue::Image(ref mut image_node) = node.data.borrow_mut().value {
// image_node.url = format!("file://{}", image_node.url);
println!("{:?}", image_node);
// Iterate over all the descendants of root.
for node in root.descendants() {
if let NodeValue::Image(ref mut image_node) = node.data.borrow_mut().value {
// image_node.url = format!("file://{}", image_node.url);
println!("{:?}", webview.eval(&image_node.url));
}
}
}
let mut html = vec![];
format_html(root, &options, &mut html).unwrap();
println!("{}", String::from_utf8(html.clone()).unwrap());
String::from_utf8(html).unwrap()
let mut html = vec![];
format_html(root, &options, &mut html).unwrap();
println!("{}", String::from_utf8(html.clone()).unwrap());
rendered_markdown = String::from_utf8(html).unwrap();
Ok(())
});
return "".to_owned();
// String::from_str("lololo").unwrap()
}