-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
399 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.