mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-03 16:17:59 +01:00
Use lazy for domain set
This commit is contained in:
parent
4d9edf6364
commit
0a0bcd15bf
5 changed files with 24 additions and 18 deletions
5
native/philomena/Cargo.lock
generated
5
native/philomena/Cargo.lock
generated
|
@ -357,9 +357,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.19.0"
|
version = "1.20.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
|
@ -375,6 +375,7 @@ dependencies = [
|
||||||
"comrak",
|
"comrak",
|
||||||
"http",
|
"http",
|
||||||
"jemallocator",
|
"jemallocator",
|
||||||
|
"once_cell",
|
||||||
"regex",
|
"regex",
|
||||||
"ring",
|
"ring",
|
||||||
"rustler",
|
"rustler",
|
||||||
|
|
|
@ -14,6 +14,7 @@ base64 = "0.21"
|
||||||
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.2", default-features = false }
|
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.2", default-features = false }
|
||||||
http = "0.2"
|
http = "0.2"
|
||||||
jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] }
|
jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] }
|
||||||
|
once_cell = "1.20"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
ring = "0.16"
|
ring = "0.16"
|
||||||
rustler = "0.35"
|
rustler = "0.35"
|
||||||
|
|
|
@ -1,25 +1,29 @@
|
||||||
use http::Uri;
|
use std::collections::BTreeSet;
|
||||||
use regex::Regex;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
pub fn get() -> Option<Vec<String>> {
|
use http::Uri;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
pub type DomainSet = BTreeSet<String>;
|
||||||
|
|
||||||
|
static DOMAINS: Lazy<Option<DomainSet>> = Lazy::new(|| {
|
||||||
if let Ok(domains) = env::var("SITE_DOMAINS") {
|
if let Ok(domains) = env::var("SITE_DOMAINS") {
|
||||||
return Some(
|
return Some(domains.split(',').map(|s| s.to_string()).collect());
|
||||||
domains
|
|
||||||
.split(',')
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect::<Vec<String>>(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
});
|
||||||
|
|
||||||
|
pub fn get() -> &'static Option<DomainSet> {
|
||||||
|
&DOMAINS
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn relativize(domains: &[String], url: &str) -> Option<String> {
|
pub fn relativize(domains: &DomainSet, url: &str) -> Option<String> {
|
||||||
let uri = url.parse::<Uri>().ok()?;
|
let uri = url.parse::<Uri>().ok()?;
|
||||||
|
|
||||||
if let Some(a) = uri.authority() {
|
if let Some(a) = uri.authority() {
|
||||||
if domains.contains(&a.host().to_string()) {
|
if domains.contains(a.host()) {
|
||||||
if let Ok(re) = Regex::new(&format!(r#"^http(s)?://({})"#, regex::escape(a.host()))) {
|
if let Ok(re) = Regex::new(&format!(r#"^http(s)?://({})"#, regex::escape(a.host()))) {
|
||||||
return Some(re.replace(url, "").into());
|
return Some(re.replace(url, "").into());
|
||||||
}
|
}
|
||||||
|
@ -29,6 +33,6 @@ pub fn relativize(domains: &[String], url: &str) -> Option<String> {
|
||||||
Some(url.into())
|
Some(url.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn relativize_careful(domains: &[String], url: &str) -> String {
|
pub fn relativize_careful(domains: &DomainSet, url: &str) -> String {
|
||||||
relativize(domains, url).unwrap_or_else(|| url.into())
|
relativize(domains, url).unwrap_or_else(|| url.into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,8 +29,8 @@ pub fn common_options() -> Options {
|
||||||
options.extension.image_url_rewriter = Some(Arc::new(|url: &str| camo::image_url_careful(url)));
|
options.extension.image_url_rewriter = Some(Arc::new(|url: &str| camo::image_url_careful(url)));
|
||||||
|
|
||||||
if let Some(domains) = domains::get() {
|
if let Some(domains) = domains::get() {
|
||||||
options.extension.link_url_rewriter = Some(Arc::new(move |url: &str| {
|
options.extension.link_url_rewriter = Some(Arc::new(|url: &str| {
|
||||||
domains::relativize_careful(&domains, url)
|
domains::relativize_careful(domains, url)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -236,8 +236,8 @@ fn image_mention_line_start() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn auto_relative_links() {
|
fn auto_relative_links() {
|
||||||
let domains: Vec<String> = vec!["example.com".into()];
|
let domains = Arc::new(vec!["example.com".into()].into_iter().collect());
|
||||||
let f = Arc::new(move |url: &str| domains::relativize_careful(&domains, url));
|
let f = Arc::new(move |url: &str| domains::relativize_careful(&*domains, url));
|
||||||
|
|
||||||
html_opts_i(
|
html_opts_i(
|
||||||
"[some link text](https://example.com/some/path)",
|
"[some link text](https://example.com/some/path)",
|
||||||
|
|
Loading…
Reference in a new issue