Skip to content

Commit

Permalink
clipboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
trashhalo committed Apr 10, 2021
1 parent 7490a0e commit 676fc92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ everything below the --- will be replaced when calling extract

# Commands
- __Extract__: Replace url or document with readable markdown extracted from the sites html content
- __Title Only__: Replace url or document with a markdown anchor with the title extracted from the page content
- __Title Only__: Replace url or document with a markdown anchor with the title extracted from the page content
- __Import from Clipboard__: Extract content from url that is found in your clipboard and dump it at your cursor
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "extract-url",
"name": "Extract url content",
"version": "0.6.0",
"version": "0.7.0",
"description": "Extract url converting content into markdown",
"author": "Stephen Solka",
"authorUrl": "https://github.com/trashhalo",
Expand Down
31 changes: 24 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod fetch;
mod obsidian;
mod transform;
mod electron;
use js_sys::{Error, JsString, Promise};
use std::rc::Rc;
use thiserror::Error;
Expand All @@ -14,6 +15,7 @@ use yaml_rust::scanner::ScanError;
#[wasm_bindgen]
pub struct ExtractCommand {
title_only: bool,
use_clipboard: bool,
id: JsString,
name: JsString,
plugin: Rc<obsidian::Plugin>,
Expand Down Expand Up @@ -45,8 +47,9 @@ impl ExtractCommand {
pub fn callback(&self) -> Promise {
let plugin = self.plugin.clone();
let title_only = self.title_only;
let use_clipboard = self.use_clipboard;
future_to_promise(async move {
let res = extract_url(&plugin, title_only).await;
let res = extract_url(&plugin, title_only, use_clipboard).await;
if let Err(e) = res {
let msg = format!("error: {}", e);
obsidian::Notice::new(&msg);
Expand All @@ -61,20 +64,30 @@ impl ExtractCommand {
#[wasm_bindgen]
pub fn onload(plugin: obsidian::Plugin) {
let p = Rc::new(plugin);
let cmd = ExtractCommand {
let cmd1 = ExtractCommand {
id: JsString::from("extract-url"),
name: JsString::from("Extract"),
plugin: p.clone(),
title_only: false,
use_clipboard: false,
};
p.addCommand(JsValue::from(cmd));
let cmd = ExtractCommand {
p.addCommand(JsValue::from(cmd1));
let cmd2 = ExtractCommand {
id: JsString::from("extract-title-from-url"),
name: JsString::from("Title Only"),
plugin: p.clone(),
title_only: true,
use_clipboard: false,
};
p.addCommand(JsValue::from(cmd))
p.addCommand(JsValue::from(cmd2));
let cmd3 = ExtractCommand {
id: JsString::from("extract-url"),
name: JsString::from("Import From Clipboard"),
plugin: p.clone(),
title_only: false,
use_clipboard: true,
};
p.addCommand(JsValue::from(cmd3))
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -117,15 +130,19 @@ impl std::convert::From<obsidian::View> for ExtractError {
}
}

async fn extract_url(plugin: &obsidian::Plugin, title_only: bool) -> Result<(), ExtractError> {
async fn extract_url(plugin: &obsidian::Plugin, title_only: bool, use_clipboard: bool) -> Result<(), ExtractError> {
if let Some(md_view) = plugin
.app()
.workspace()
.get_active_view_of_type(&obsidian::MARKDOWN_VIEW)
{
let view: obsidian::MarkdownView = md_view.dyn_into()?;
let editor = view.source_mode().cm_editor();
let url_str = editor.get_selection();
let url_str = if use_clipboard {
electron::clipboard_read_text()
} else {
editor.get_selection()
};
if url_str == "" {
let (url_str, content) = extract_link_from_yaml(&view.get_view_data())?;
let md = convert_url_to_markdown(title_only, url_str).await?;
Expand Down

0 comments on commit 676fc92

Please sign in to comment.