Force editor not to expand

This commit is contained in:
Iaphetes 2024-12-31 13:20:06 +01:00
parent 869157c08c
commit 0d41825928
5 changed files with 120 additions and 139 deletions

View file

@ -2,27 +2,19 @@ use comrak::{format_html, nodes::NodeValue, parse_document, Arena, Options};
use html_tag::HtmlTag;
use shellexpand;
use std::{
collections::BTreeMap,
env,
ffi::{OsStr, OsString},
fs,
path::{absolute, Path},
};
use tauri::{ipc::IpcResponse, Manager};
use tauri_plugin_fs::FsExt;
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
#[tauri::command]
fn parse_markdown(document: &str, pathtemplate: &str, basepath: &str) -> String {
let mut rendered_markdown: String = String::new();
let rendered_markdown: String;
let path = "/foo/bar.txt";
println!("{:?}", shellexpand::full(path));
// let webview = app.get_webview_window("main").unwrap();
let arena = Arena::new();
// Parse the document into a root `AstNode`
@ -32,23 +24,31 @@ fn parse_markdown(document: &str, pathtemplate: &str, basepath: &str) -> String
let root = parse_document(&arena, document, &options);
// Iterate over all the descendants of root.
env::set_current_dir(basepath);
if let Ok(resolved_basepath) = shellexpand::full(&basepath) {
println!(
"{:?}",
env::set_current_dir(&resolved_basepath.into_owned())
);
}
for node in root.descendants() {
if let NodeValue::Image(ref mut image_node) = node.data.borrow_mut().value {
let image_path = Path::new(&image_node.url).to_path_buf();
let absolute_image_path = absolute(image_node.url.clone());
let absolute_image_path =
.unwrap_or(image_path)
.as_path()
.to_string_lossy()
if let Ok(resolved_path) = shellexpand::full(&absolute_image_path) {
let absolute_image_path_res = absolute(image_node.url.clone());
let absolute_image_path = absolute_image_path_res.unwrap_or(image_path.clone());
let absolute_image_path_str = absolute_image_path.as_path().to_string_lossy();
if let Ok(resolved_path) = shellexpand::full(&absolute_image_path_str) {
image_node.url = pathtemplate.replace("FILEPATH", &resolved_path);
}
println!("{}", image_node.url);
}
}
let mut html = vec![];
format_html(root, &options, &mut html).unwrap();
println!("{}", String::from_utf8(html.clone()).unwrap());
// println!("{}", String::from_utf8(html.clone()).unwrap());
rendered_markdown = String::from_utf8(html).unwrap();
return rendered_markdown.to_owned();
// String::from_str("lololo").unwrap()