diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 35ff5b2..07faac0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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() diff --git a/src/filesystem.js b/src/filesystem.js index f0c4995..99293cc 100644 --- a/src/filesystem.js +++ b/src/filesystem.js @@ -1,52 +1,51 @@ const { convertFileSrc, invoke } = window.__TAURI__.core; const { homeDir, join } = window.__TAURI__.path; const { readTextFile } = window.__TAURI__.fs; - -function handle_file_select(filename){ - if (filename.endsWith("md")){ - readTextFile(convertFileSrc(filename) ).then( - (ret)=>{ - var tag_id = document.getElementById('markdown_input'); - tag_id.innerHTML = "
".concat("", ret).concat("", "");
- }
- );
- var tag_id = document.getElementById('rendered_markdown');
- tag_id.dispatchEvent(new Event("input"));
- }
+import { render_markdown } from "./main.js";
+function handle_file_select(filename) {
+ if (filename.endsWith("md")) {
+ readTextFile(convertFileSrc(filename)).then(
+ (ret) => {
+ var tag_id = document.getElementById('markdown_input');
+ tag_id.innerHTML = "".concat("", ret).concat("", "");
+ render_markdown();
+ }
+ );
+ }
}
function dropdown(id) {
- var dropdown_element = document.getElementById(id);
- var dropdown_children = dropdown_element.children;
- console.log(dropdown_element.getAttribute("expanded"));
- if (dropdown_element.getAttribute("expanded") == "false") {
- dropdown_element.setAttribute("expanded", "true");
- }
- else{
- dropdown_element.setAttribute("expanded", "false");
- }
- for (var i = 0; i < dropdown_children.length; i++) {
- var child = dropdown_children[i];
- console.log(child.id);
- if (child.className === "filetree-node") {
- if (document.getElementById(id).getAttribute("expanded") == "true") {
- document.getElementById(child.id).style.visibility = "visible";
- document.getElementById(child.id).style.height = "auto";
- } else {
- document.getElementById(child.id).style.visibility = "hidden";
- document.getElementById(child.id).style.height = 0;
- }
- }
+ var dropdown_element = document.getElementById(id);
+ var dropdown_children = dropdown_element.children;
+ console.log(dropdown_element.getAttribute("expanded"));
+ if (dropdown_element.getAttribute("expanded") == "false") {
+ dropdown_element.setAttribute("expanded", "true");
+ }
+ else {
+ dropdown_element.setAttribute("expanded", "false");
+ }
+ for (var i = 0; i < dropdown_children.length; i++) {
+ var child = dropdown_children[i];
+ console.log(child.id);
+ if (child.className === "filetree-node") {
+ if (document.getElementById(id).getAttribute("expanded") == "true") {
+ document.getElementById(child.id).style.visibility = "visible";
+ document.getElementById(child.id).style.height = "auto";
+ } else {
+ document.getElementById(child.id).style.visibility = "hidden";
+ document.getElementById(child.id).style.height = 0;
+ }
}
+ }
}
-window.onload = function () {
- invoke("dir_tree_html", { basepath: "~/Documents/Knowledgebase", filter: ["*"]}).then(
- (ret)=>{
- var tag_id = document.getElementById('filetree');
- tag_id.innerHTML = ret;
- }
- )
+window.onload = function() {
+ invoke("dir_tree_html", { basepath: "~/Documents/Knowledgebase", filter: ["*"] }).then(
+ (ret) => {
+ var tag_id = document.getElementById('filetree');
+ tag_id.innerHTML = ret;
+ }
+ )
}
let filetree = document.getElementById('filetree');
// Options for the observer (which mutations to observe)
@@ -54,20 +53,20 @@ const config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = (mutationList, observer) => {
- var anchors = document.getElementsByClassName("filetree-directory-button");
- for (var i = 0; i < anchors.length; i++) {
- var anchor = anchors[i];
- anchor.onclick = function () {
- dropdown(this.parentElement.id);
- };
+ var anchors = document.getElementsByClassName("filetree-directory-button");
+ for (var i = 0; i < anchors.length; i++) {
+ var anchor = anchors[i];
+ anchor.onclick = function() {
+ dropdown(this.parentElement.id);
};
- var anchors = document.getElementsByClassName("filetree-file-button");
- for (var i = 0; i < anchors.length; i++) {
- var anchor = anchors[i];
- anchor.onclick = function () {
- handle_file_select(this.parentElement.id);
- };
+ };
+ var anchors = document.getElementsByClassName("filetree-file-button");
+ for (var i = 0; i < anchors.length; i++) {
+ var anchor = anchors[i];
+ anchor.onclick = function() {
+ handle_file_select(this.parentElement.id);
};
+ };
};
diff --git a/src/index.html b/src/index.html
index 6617c79..9f60d6f 100644
--- a/src/index.html
+++ b/src/index.html
@@ -12,6 +12,9 @@
+
+
+
@@ -35,26 +38,6 @@