mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-02 15:48:00 +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]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
version = "1.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
|
@ -375,6 +375,7 @@ dependencies = [
|
|||
"comrak",
|
||||
"http",
|
||||
"jemallocator",
|
||||
"once_cell",
|
||||
"regex",
|
||||
"ring",
|
||||
"rustler",
|
||||
|
|
|
@ -14,6 +14,7 @@ base64 = "0.21"
|
|||
comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.2", default-features = false }
|
||||
http = "0.2"
|
||||
jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] }
|
||||
once_cell = "1.20"
|
||||
regex = "1"
|
||||
ring = "0.16"
|
||||
rustler = "0.35"
|
||||
|
|
|
@ -1,25 +1,29 @@
|
|||
use http::Uri;
|
||||
use regex::Regex;
|
||||
use std::collections::BTreeSet;
|
||||
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") {
|
||||
return Some(
|
||||
domains
|
||||
.split(',')
|
||||
.map(|s| s.to_string())
|
||||
.collect::<Vec<String>>(),
|
||||
);
|
||||
return Some(domains.split(',').map(|s| s.to_string()).collect());
|
||||
}
|
||||
|
||||
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()?;
|
||||
|
||||
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()))) {
|
||||
return Some(re.replace(url, "").into());
|
||||
}
|
||||
|
@ -29,6 +33,6 @@ pub fn relativize(domains: &[String], url: &str) -> Option<String> {
|
|||
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())
|
||||
}
|
||||
|
|
|
@ -29,8 +29,8 @@ pub fn common_options() -> Options {
|
|||
options.extension.image_url_rewriter = Some(Arc::new(|url: &str| camo::image_url_careful(url)));
|
||||
|
||||
if let Some(domains) = domains::get() {
|
||||
options.extension.link_url_rewriter = Some(Arc::new(move |url: &str| {
|
||||
domains::relativize_careful(&domains, url)
|
||||
options.extension.link_url_rewriter = Some(Arc::new(|url: &str| {
|
||||
domains::relativize_careful(domains, url)
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
|
@ -236,8 +236,8 @@ fn image_mention_line_start() {
|
|||
|
||||
#[test]
|
||||
fn auto_relative_links() {
|
||||
let domains: Vec<String> = vec!["example.com".into()];
|
||||
let f = Arc::new(move |url: &str| domains::relativize_careful(&domains, url));
|
||||
let domains = Arc::new(vec!["example.com".into()].into_iter().collect());
|
||||
let f = Arc::new(move |url: &str| domains::relativize_careful(&*domains, url));
|
||||
|
||||
html_opts_i(
|
||||
"[some link text](https://example.com/some/path)",
|
||||
|
|
Loading…
Reference in a new issue