Skip to content

Commit

Permalink
Merge pull request #13 from trashhalo/mobile-support
Browse files Browse the repository at this point in the history
mobile support
  • Loading branch information
trashhalo authored Sep 4, 2021
2 parents 61deb71 + c108c3e commit a1dda6f
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 106 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ edition = "2018"
[lib]
crate-type = ["cdylib"]

[profile.release]
lto = true
opt-level = 'z'

[dependencies]
wasm-bindgen = { version = "^0.2", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.22"
Expand All @@ -29,4 +33,5 @@ default-features = false
version = "0.3.49"
features = [
'Window',
'console'
]
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# Obsidian Plugin: Convert a URL into markdown

![Demo](images/demo-extract-url.gif)

Transforms a URL to markdown view if the website allows it.

# Installation

Available in the community plugin store in options.

# Modes

Operates in 2 modes.
1. __Selection__ - If you select a URL in the document and execute these commands it will replace the selection with the markdown content.
2. __Document__ - If you add front mater with the key of `link` to your document then it is treated as a linked document. Then calling extract will look for the link and replace the content of the document with the extracted content.

1. **Selection** - If you select a URL in the document and execute these commands it will replace the selection with the markdown content.
2. **Document** - If you add front mater with the key of `link` to your document then it is treated as a linked document. Then calling extract will look for the link and replace the content of the document with the extracted content.

## Document mode example

```markdown
---
link: "https://bart.degoe.de/building-a-full-text-search-engine-150-lines-of-code/"
Expand All @@ -21,11 +26,13 @@ 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
- __Import from Clipboard__: Extract content from url that is found in your clipboard and dump it at your cursor

- **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
- **Import from Clipboard**: Extract content from url that is found in your clipboard and dump it at your cursor. Desktop only

# Youtube

If your system has `youtube-dl` installed extra details like channel name and description will be extracted for youtube urls.

![youtube](images/youtube.png)
5 changes: 3 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"id": "extract-url",
"name": "Extract url content",
"version": "0.8.0",
"version": "0.9.0",
"description": "Extract url converting content into markdown",
"author": "Stephen Solka",
"authorUrl": "https://github.com/trashhalo",
"isDesktopOnly": true
"isDesktopOnly": false,
"minAppVersion": "0.12.15"
}
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
"description": "Extract url converting content into markdown",
"main": "main.js",
"scripts": {
"build": "wasm-pack build --target web && rollup --config rollup.config.js",
"dev": "wasm-pack build --dev --target web && rollup --config rollup.config.js"
"esbuild-dev": "esbuild index.js --platform=node --external:obsidian --external:electron --loader:.wasm=base64 --bundle --outfile=main.js",
"esbuild-release": "esbuild index.js --platform=node --external:obsidian --external:electron --loader:.wasm=base64 --bundle --outfile=main.js --minify",
"build": "wasm-pack build --target web && yarn esbuild-release",
"dev": "wasm-pack build --dev --target web && yarn esbuild-dev"
},
"keywords": [],
"author": "",
"license": "GPL",
"devDependencies": {
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"rollup": "^2.32.1",
"rollup-plugin-base64": "^1.0.1"
"esbuild": "^0.12.25"
},
"dependencies": {
"hasbin": "^1.2.3",
"node-fetch": "2.6.1"
"hasbin": "^1.2.3"
}
}
19 changes: 0 additions & 19 deletions rollup.config.js

This file was deleted.

34 changes: 34 additions & 0 deletions shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { Platform } = require('obsidian');

function hasBin(bin) {
if(Platform.isDesktop) {
const hasBin = require('hasbin');
return hasBin.sync(bin);
} else {
return false;
}
}

function nodeExec(bin, cb) {
if(Platform.isDesktop) {
const childProcess = require('child_process');
return childProcess.exec(bin, cb);
} else {
throw new Error('platform not supported');
}
}

function clipboardReadText() {
if(Platform.isDesktop) {
const electron = require('electron');
return electron.clipboard.readText();
} else {
throw new Error('platform not supported');
}
}

module.exports = {
hasBin,
nodeExec,
clipboardReadText
}
9 changes: 0 additions & 9 deletions src/electron.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/fetch.rs

This file was deleted.

44 changes: 28 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
mod fetch;
mod obsidian;
mod request;
mod shim;
mod transform;
mod electron;
use js_sys::{Error, JsString, Promise};
use std::rc::Rc;
use thiserror::Error;
use url::Url;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::{future_to_promise, JsFuture};
use web_sys::console;
use yaml_rust::emitter::{EmitError, YamlEmitter};
use yaml_rust::scanner::ScanError;

Expand Down Expand Up @@ -80,14 +81,17 @@ pub fn onload(plugin: obsidian::Plugin) {
use_clipboard: false,
};
p.addCommand(JsValue::from(cmd2));
let cmd3 = ExtractCommand {
id: JsString::from("import-url"),
name: JsString::from("Import From Clipboard"),
plugin: p.clone(),
title_only: false,
use_clipboard: true,
};
p.addCommand(JsValue::from(cmd3))

if *obsidian::DESKTOP {
let cmd3 = ExtractCommand {
id: JsString::from("import-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 @@ -130,7 +134,11 @@ impl std::convert::From<obsidian::View> for ExtractError {
}
}

async fn extract_url(plugin: &obsidian::Plugin, title_only: bool, use_clipboard: 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()
Expand All @@ -139,7 +147,7 @@ async fn extract_url(plugin: &obsidian::Plugin, title_only: bool, use_clipboard:
let view: obsidian::MarkdownView = md_view.dyn_into()?;
let editor = view.source_mode().cm_editor();
let url_str = if use_clipboard {
electron::clipboard_read_text()
shim::clipboard_read_text()
} else {
editor.get_selection()
};
Expand All @@ -165,13 +173,17 @@ async fn convert_url_to_markdown(
title_only: bool,
url_str: String,
) -> Result<String, ExtractError> {
let ref url = Url::parse(&url_str)?;
let resp_value = JsFuture::from(fetch::with_url(&url_str)).await?;
let resp: fetch::Response = resp_value.dyn_into()?;
let body = JsFuture::from(resp.text()?)
let params = request::request_params(&url_str);
let body: String = JsFuture::from(request::request(params)?)
.await?
.as_string()
.ok_or_else(|| ExtractError::NoContent)?;

if cfg!(debug_assertions) {
console::log_2(&"body".into(), &JsValue::from_str(&body));
}

let ref url = Url::parse(&url_str)?;
Ok(transform::transform_url(url, title_only, body).await?)
}

Expand Down
3 changes: 3 additions & 0 deletions src/obsidian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ extern "C" {
#[wasm_bindgen(js_name=MarkdownView)]
pub static MARKDOWN_VIEW: Function;

#[wasm_bindgen(js_namespace=Platform, js_name=isDesktop)]
pub static DESKTOP: bool;

#[wasm_bindgen(method)]
pub fn addCommand(this: &Plugin, command: JsValue);

Expand Down
14 changes: 14 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use js_sys::Promise;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(catch)]
pub fn request(params: JsValue) -> Result<Promise, JsValue>;
}

pub fn request_params(url: &str) -> JsValue {
let obj = js_sys::Object::new();
js_sys::Reflect::set(&obj, &"url".into(), &JsValue::from_str(url)).unwrap();
obj.into()
}
14 changes: 14 additions & 0 deletions src/shim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use js_sys::Error;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "/shim.js")]
extern "C" {
#[wasm_bindgen(js_name=hasBin)]
pub fn has_bin(app: &str) -> bool;

#[wasm_bindgen(js_name=nodeExec)]
pub fn node_exec(cmd: &str, f: &Closure<dyn FnMut(Option<Error>, String, String)>) -> JsValue;

#[wasm_bindgen(js_name = clipboardReadText)]
pub fn clipboard_read_text() -> String;
}
11 changes: 9 additions & 2 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use html2md::parse_html;
use readability::extractor::extract;
use thiserror::Error;
use url::Url;
use web_sys::console;

#[derive(Error, Debug)]
pub enum TransformError {
Expand Down Expand Up @@ -40,12 +41,18 @@ pub async fn transform_url(
if title_only {
match oembed::oembed_title(body.clone(), url).await {
Ok(o) => Ok(o),
Err(_) => readable_title(body.clone(), url).await,
Err(e) => {
console::log_2(&"oembed error".into(), &format!("{:?}", e).into());
readable_title(body.clone(), url).await
}
}
} else {
match oembed::oembed_content(body.clone(), url).await {
Ok(o) => Ok(o),
Err(_) => readable_content(body.clone(), url).await,
Err(e) => {
console::log_2(&"oembed error".into(), &format!("{:?}", e).into());
readable_content(body.clone(), url).await
}
}
}
}
Loading

0 comments on commit a1dda6f

Please sign in to comment.