Skip to content

Commit

Permalink
oembed support
Browse files Browse the repository at this point in the history
  • Loading branch information
trashhalo committed Apr 10, 2021
1 parent 8a41370 commit 7490a0e
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 72 deletions.
99 changes: 53 additions & 46 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ edition = "2018"
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
wasm-bindgen = { version = "^0.2", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.22"
js-sys = "0.3.49"
url = "1"
html2md = "0.2.10"
thiserror = "1.0.24"
frontmatter = "^0.4"
yaml-rust = "^0.4"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"

[dependencies.readability]
git = "https://github.com/trashhalo/readability.git"
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "extract-url",
"name": "Extract url content",
"version": "0.5.0",
"version": "0.6.0",
"description": "Extract url converting content into markdown",
"author": "Stephen Solka",
"authorUrl": "https://github.com/trashhalo",
"isDesktopOnly": true
}
}
3 changes: 3 additions & 0 deletions src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ extern "C" {

#[wasm_bindgen(catch, method)]
pub fn text(this: &Response) -> Result<Promise, JsValue>;

#[wasm_bindgen(catch, method)]
pub fn json(this: &Response) -> Result<Promise, JsValue>;
}
33 changes: 10 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
mod fetch;
mod obsidian;
use html2md::parse_html;
mod transform;
use js_sys::{Error, JsString, Promise};
use readability::extractor::extract;
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 yaml_rust::emitter::{YamlEmitter, EmitError};
use yaml_rust::emitter::{EmitError, YamlEmitter};
use yaml_rust::scanner::ScanError;

#[wasm_bindgen]
Expand Down Expand Up @@ -80,26 +79,26 @@ pub fn onload(plugin: obsidian::Plugin) {

#[derive(Error, Debug)]
pub enum ExtractError {
#[error("url did not parse")]
#[error("url did not parse. {0}")]
Parse(#[from] url::ParseError),

#[error("url not readable")]
Read(#[from] readability::error::Error),

#[error("url had not content")]
NoContent,

#[error("fetch error `{0}`")]
Fetch(String),

#[error("select a url to extract or add link to your frontmatter")]
#[error("select a url to extract or add link to your frontmatter. {0}")]
NoUrlFrontmatter(#[from] FrontmatterError),

#[error("expected view to be MarkdownView but was not")]
WrongView,

#[error("error serializing front matter")]
#[error("error serializing front matter. {0}")]
FrontmatterWrite(#[from] EmitError),

#[error("error transforming content. {0}")]
Transform(#[from] transform::TransformError),
}

impl std::convert::From<JsValue> for ExtractError {
Expand Down Expand Up @@ -149,26 +148,14 @@ async fn convert_url_to_markdown(
title_only: bool,
url_str: String,
) -> Result<String, ExtractError> {
let url = Url::parse(&url_str)?;
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()?)
.await?
.as_string()
.ok_or_else(|| ExtractError::NoContent)?;
let ref mut b = body.as_bytes();
let readable = extract(b, &url)?;

Ok(if title_only {
format!("[{}]({})", readable.title, url_str)
} else {
format!(
"# [{}]({})\n{}",
readable.title,
url_str,
parse_html(&readable.content)
)
})
Ok(transform::transform_url(url, title_only, body).await?)
}

#[derive(Error, Debug)]
Expand Down
Loading

0 comments on commit 7490a0e

Please sign in to comment.