mirror of
https://github.com/zyllian/zyllian.github.io.git
synced 2025-01-18 03:32:30 -08:00
Remove referrer information from external links
This commit is contained in:
parent
c67fcd19a7
commit
de4c97c74b
1 changed files with 32 additions and 16 deletions
20
src/lib.rs
20
src/lib.rs
|
@ -7,6 +7,7 @@ pub mod serving;
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
|
@ -16,6 +17,7 @@ use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings};
|
|||
use pulldown_cmark::{Options, Parser};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use walkdir::WalkDir;
|
||||
use warp::hyper::Uri;
|
||||
|
||||
const PAGES_PATH: &str = "pages";
|
||||
const TEMPLATES_PATH: &str = "templates";
|
||||
|
@ -247,7 +249,8 @@ impl<'a> SiteBuilder<'a> {
|
|||
let mut output = Vec::new();
|
||||
let mut rewriter = HtmlRewriter::new(
|
||||
Settings {
|
||||
element_content_handlers: vec![element!("head", |el| {
|
||||
element_content_handlers: vec![
|
||||
element!("head", |el| {
|
||||
el.prepend(r#"<meta charset="utf-8">"#, ContentType::Html);
|
||||
el.append(&format!("<title>{}</title>", title), ContentType::Html);
|
||||
if self.serving {
|
||||
|
@ -263,7 +266,20 @@ impl<'a> SiteBuilder<'a> {
|
|||
}
|
||||
|
||||
Ok(())
|
||||
})],
|
||||
}),
|
||||
element!("a", |el| {
|
||||
if let Some(href) = el.get_attribute("href") {
|
||||
if let Ok(uri) = Uri::from_str(&href) {
|
||||
if uri.host().is_some() {
|
||||
el.set_attribute("rel", "noopener noreferrer")?;
|
||||
el.set_attribute("target", "_blank")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
],
|
||||
..Default::default()
|
||||
},
|
||||
|c: &[u8]| output.extend_from_slice(c),
|
||||
|
|
Loading…
Add table
Reference in a new issue