Skip to content

Commit

Permalink
Update to Philomena comrak v0.29.0
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Nov 2, 2024
1 parent a08510f commit 580d19b
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 74 deletions.
96 changes: 53 additions & 43 deletions native/philomena/Cargo.lock

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

8 changes: 5 additions & 3 deletions native/philomena/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ path = "src/lib.rs"
crate-type = ["dylib"]

[dependencies]
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "main", default-features = false }
base64 = "0.21"
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.0", default-features = false }
http = "0.2"
jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] }
rustler = "0.28"
regex = "1"
ring = "0.16"
base64 = "0.21"
rustler = "0.28"
url = "2.3"

[profile.release]
Expand Down
20 changes: 12 additions & 8 deletions native/philomena/src/camo.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use ring::hmac;
use std::env;
use url::Url;
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;

fn trusted_host(mut url: Url) -> Option<String> {
url.set_port(Some(443)).ok()?;
Expand All @@ -27,20 +27,24 @@ fn untrusted_host(url: Url, camo_host: String, camo_key: String) -> Option<Strin
Some(camo_uri.to_string())
}

pub fn image_url(uri: String) -> Option<String> {
pub fn image_url(uri: &str) -> Option<String> {
let cdn_host = env::var("CDN_HOST").ok()?;
let camo_host = env::var("CAMO_HOST").unwrap_or_else(|_| String::from(""));
let camo_key = env::var("CAMO_KEY").unwrap_or_else(|_| String::from(""));
let camo_host = env::var("CAMO_HOST").unwrap_or_else(|_| "".into());
let camo_key = env::var("CAMO_KEY").unwrap_or_else(|_| "".into());

if camo_key.is_empty() {
return Some(uri);
return Some(uri.into());
}

let url = Url::parse(&uri).ok()?;
let url = Url::parse(uri).ok()?;

match url.host_str() {
Some(hostname) if hostname == cdn_host || hostname == camo_host => trusted_host(url),
Some(_) => untrusted_host(url, camo_host, camo_key),
None => Some(String::from("")),
None => Some("".into()),
}
}

pub fn image_url_careful(uri: &str) -> String {
image_url(uri).unwrap_or_else(|| "".into())
}
34 changes: 34 additions & 0 deletions native/philomena/src/domains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use http::Uri;
use regex::Regex;
use std::env;

pub fn get() -> Option<Vec<String>> {
if let Ok(domains) = env::var("SITE_DOMAINS") {
return Some(
domains
.split(',')
.map(|s| s.to_string())
.collect::<Vec<String>>(),
);
}

None
}

pub fn relativize(domains: &[String], url: &str) -> Option<String> {
let uri = url.parse::<Uri>().ok()?;

if let Some(a) = uri.authority() {
if domains.contains(&a.host().to_string()) {
if let Ok(re) = Regex::new(&format!(r#"^http(s)?://({})"#, regex::escape(a.host()))) {
return Some(re.replace(url, "").into());
}
}
}

Some(url.into())
}

pub fn relativize_careful(domains: &[String], url: &str) -> String {
relativize(domains, url).unwrap_or_else(|| url.into())
}
5 changes: 4 additions & 1 deletion native/philomena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use jemallocator::Jemalloc;
use rustler::Term;

mod camo;
mod domains;
mod markdown;
#[cfg(test)]
mod tests;

#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
Expand All @@ -28,5 +31,5 @@ fn markdown_to_html_unsafe(input: String, reps: Term) -> String {

#[rustler::nif]
fn camo_image_url(input: String) -> String {
camo::image_url(input).unwrap_or_else(|| String::from(""))
camo::image_url_careful(&input)
}
53 changes: 34 additions & 19 deletions native/philomena/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
use comrak::ComrakOptions;
use crate::camo;
use crate::{camo, domains};
use comrak::Options;
use rustler::{MapIterator, Term};
use std::collections::HashMap;
use std::env;
use std::sync::Arc;

fn common_options() -> ComrakOptions {
let mut options = ComrakOptions::default();
pub fn common_options() -> Options<'static> {
let mut options = Options::default();

// Upstream options
options.extension.autolink = true;
options.extension.table = true;
options.extension.description_lists = true;
options.extension.superscript = true;
options.extension.strikethrough = true;
options.extension.philomena = true;
options.parse.smart = true;
options.render.hardbreaks = true;
options.render.github_pre_lang = true;
options.render.escape = true;

// Philomena options
options.extension.underline = true;
options.extension.spoiler = true;
options.extension.greentext = true;
options.extension.subscript = true;
options.extension.philomena = true;
options.render.ignore_empty_links = true;
options.render.ignore_setext = true;

options.extension.camoifier = Some(|s| camo::image_url(s).unwrap_or_else(|| String::from("")));
options.extension.image_url_rewriter = Some(Arc::new(|url: &str| camo::image_url_careful(url)));

if let Ok(domains) = env::var("SITE_DOMAINS") {
options.extension.philomena_domains = Some(domains.split(',').map(|s| s.to_string()).collect::<Vec<String>>());
if let Some(domains) = domains::get() {
options.extension.link_url_rewriter = Some(Arc::new(move |url: &str| {
domains::relativize_careful(&domains, url)
}));
}

options
}

fn map_to_hashmap(map: Term) -> Option<HashMap<String, String>> {
Some(MapIterator::new(map)?.map(|(key, value)| {
let key: String = key.decode().unwrap_or_else(|_| String::from(""));
let value: String = value.decode().unwrap_or_else(|_| String::from(""));
Some(
MapIterator::new(map)?
.map(|(key, value)| {
let key: String = key.decode().unwrap_or_else(|_| "".into());
let value: String = value.decode().unwrap_or_else(|_| "".into());

(key, value)
}).collect())
(key, value)
})
.collect(),
)
}

pub fn to_html(input: String, reps: Term) -> String {
let mut options = common_options();
options.render.escape = true;

options.extension.philomena_replacements = map_to_hashmap(reps);
options.extension.replacements = map_to_hashmap(reps);

comrak::markdown_to_html(&input, &options)
}

pub fn to_html_unsafe(input: String, reps: Term) -> String {
let mut options = common_options();
options.render.escape = false;
options.render.unsafe_ = true;

options.extension.philomena_replacements = map_to_hashmap(reps);
options.extension.replacements = map_to_hashmap(reps);

comrak::markdown_to_html(&input, &options)
}
Loading

0 comments on commit 580d19b

Please sign in to comment.