Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ vite.config.ts.timestamp-*
test.md
test_image.png
src-tauri/output.txt
test_obsidian.md

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed titlebar icon color
- Added togglable status bar
- Added optional word count in status bar
- Added Obsidian `![[]]` embed support

### Released

Expand Down
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ comrak = "0.18"
serde_json = "1"
tauri-plugin-prevent-default = "2.0.0-rc.1"
notify = "6"
regex = "1"

directories = "5"
opener = { version = "0.7", features = ["reveal"] }
Expand Down
34 changes: 32 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::path::Path;
use std::sync::Mutex;
use tauri::{AppHandle, Emitter, Manager, State};
use tauri::menu::ContextMenu;
use regex::{Regex, Captures};
use std::borrow::Cow;


struct WatcherState {
Expand All @@ -21,8 +23,36 @@ async fn show_window(window: tauri::Window) {
window.show().unwrap();
}

fn process_obsidian_embeds(content: &str) -> Cow<'_, str> {
let re = Regex::new(r"!\[\[(.*?)\]\]").unwrap();

re.replace_all(content, |caps: &Captures| {
let inner = &caps[1];
let mut parts = inner.split('|');
let path = parts.next().unwrap_or("");
let size = parts.next();

let path_escaped = path.replace(" ", "%20");

if let Some(size_str) = size {
if size_str.contains('x') {
let mut dims = size_str.split('x');
let width = dims.next().unwrap_or("");
let height = dims.next().unwrap_or("");
format!("<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{}\" />", path_escaped, width, height, path)
} else {
format!("<img src=\"{}\" width=\"{}\" alt=\"{}\" />", path_escaped, size_str, path)
}
} else {
format!("<img src=\"{}\" alt=\"{}\" />", path_escaped, path)
}
})
}

#[tauri::command]
fn convert_markdown(content: &str) -> String {
let processed = process_obsidian_embeds(content);

let mut options = ComrakOptions {
extension: ComrakExtensionOptions {
strikethrough: true,
Expand All @@ -36,11 +66,11 @@ fn convert_markdown(content: &str) -> String {
},
..ComrakOptions::default()
};
options.render.unsafe_ = false;
options.render.unsafe_ = true;
options.render.hardbreaks = true;
options.render.sourcepos = true;

markdown_to_html(content, &options)
markdown_to_html(&processed, &options)
}

#[tauri::command]
Expand Down