change how me rel works

This commit is contained in:
zyl 2023-11-13 14:56:59 -08:00
parent 9c09ae9817
commit 94ba05daf2
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
2 changed files with 23 additions and 12 deletions

View file

@ -6,5 +6,5 @@ if you want to see mostly stuff of my cat and some short game clips (most are un
if you're an adult i have a couple socials floating around: if you're an adult i have a couple socials floating around:
- [cohost](https://cohost.org/zyl) - [cohost](me$https://cohost.org/zyl)
- [tumblr](https://www.tumblr.com/zyllian) - [tumblr](me$https://www.tumblr.com/zyllian)

View file

@ -12,9 +12,6 @@ use url::Url;
use crate::{util, PageMetadata, Site, ROOT_PATH, SASS_PATH}; use crate::{util, PageMetadata, Site, ROOT_PATH, SASS_PATH};
/// URLs which need to have a "me" rel attribute.
const ME_URLS: &[&str] = &["https://mas.to/@zyl"];
/// Struct containing data to be sent to templates when rendering them. /// Struct containing data to be sent to templates when rendering them.
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct TemplateData<'a, T> { struct TemplateData<'a, T> {
@ -146,16 +143,30 @@ impl<'a> SiteBuilder<'a> {
Ok(()) Ok(())
}), }),
element!("a", |el| { element!("a", |el| {
if let Some(href) = el.get_attribute("href") { if let Some(mut href) = el.get_attribute("href") {
if let Some((command, new_href)) = href.split_once('$') {
#[allow(clippy::single_match)]
match command {
"me" => {
el.set_attribute(
"rel",
&(el.get_attribute("rel").unwrap_or_default() + " me"),
)?;
}
_ => {}
}
href = new_href.to_string();
el.set_attribute("href", &href)?;
}
if let Ok(url) = Url::parse(&href) { if let Ok(url) = Url::parse(&href) {
if url.host().is_some() { if url.host().is_some() {
// Make external links open in new tabs without referral information // Make external links open in new tabs without referral information
let mut rel = String::from("noopener noreferrer"); el.set_attribute(
if ME_URLS.contains(&href.as_str()) { "rel",
// Needed for Mastodon link verification (el.get_attribute("rel").unwrap_or_default()
rel.push_str(" me"); + " noopener noreferrer")
} .trim(),
el.set_attribute("rel", &rel)?; )?;
el.set_attribute("target", "_blank")?; el.set_attribute("target", "_blank")?;
} }
} }