Skip to content

Commit

Permalink
feat: External URL redirect (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
kartva authored and Keats committed Feb 16, 2023
1 parent f4a1e99 commit 3e1f577
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
28 changes: 28 additions & 0 deletions components/content/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use config::Config;
use errors::{Context, Result};
use markdown::{render_content, RenderContext};
use utils::fs::read_file;
use utils::net::is_external_link;
use utils::table_of_contents::Heading;
use utils::templates::{render_template, ShortcodeDefinition};

Expand Down Expand Up @@ -168,7 +169,14 @@ impl Section {
.with_context(|| format!("Failed to render content of {}", self.file.path.display()))?;
self.content = res.body;
self.toc = res.toc;

self.external_links = res.external_links;
if let Some(ref redirect_to) = self.meta.redirect_to {
if is_external_link(redirect_to) {
self.external_links.push(redirect_to.to_owned());
}
}

self.internal_links = res.internal_links;

Ok(())
Expand Down Expand Up @@ -356,4 +364,24 @@ Bonjour le monde"#
assert_eq!(section.lang, "fr".to_string());
assert_eq!(section.permalink, "http://a-website.com/fr/subcontent/");
}

#[test]
fn can_redirect_to_external_site() {
let config = Config::default();
let content = r#"
+++
redirect_to = "https://bar.com/something"
+++
Example"#
.to_string();
let res = Section::parse(
Path::new("content/subcontent/_index.md"),
&content,
&config,
&PathBuf::new(),
);
assert!(res.is_ok());
let section = res.unwrap();
assert_eq!(section.meta.redirect_to, Some("https://bar.com/something".to_owned()));
}
}
6 changes: 1 addition & 5 deletions components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use libs::gh_emoji::Replacer as EmojiReplacer;
use libs::once_cell::sync::Lazy;
use libs::pulldown_cmark as cmark;
use libs::tera;
use utils::net::is_external_link;

use crate::context::RenderContext;
use errors::{Context, Error, Result};
Expand Down Expand Up @@ -133,11 +134,6 @@ fn find_anchor(anchors: &[String], name: String, level: u16) -> String {
find_anchor(anchors, name, level + 1)
}

/// Returns whether a link starts with an HTTP(s) scheme.
fn is_external_link(link: &str) -> bool {
link.starts_with("http:") || link.starts_with("https:")
}

fn fix_link(
link_type: LinkType,
link: &str,
Expand Down
9 changes: 7 additions & 2 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod sass;
pub mod sitemap;
pub mod tpls;

use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::fs::{remove_dir_all, remove_file};
use std::path::{Path, PathBuf};
Expand All @@ -25,7 +26,7 @@ use utils::fs::{
copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists,
is_dotfile,
};
use utils::net::get_available_port;
use utils::net::{get_available_port, is_external_link};
use utils::templates::{render_template, ShortcodeDefinition};
use utils::types::InsertAnchor;

Expand Down Expand Up @@ -1142,7 +1143,11 @@ impl Site {
}

if let Some(ref redirect_to) = section.meta.redirect_to {
let permalink = self.config.make_permalink(redirect_to);
let permalink: Cow<String> = if is_external_link(redirect_to) {
Cow::Borrowed(redirect_to)
} else {
Cow::Owned(self.config.make_permalink(redirect_to))
};
self.write_content(
&components,
"index.html",
Expand Down
5 changes: 5 additions & 0 deletions components/utils/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pub fn get_available_port(avoid: u16) -> Option<u16> {
pub fn port_is_available(port: u16) -> bool {
TcpListener::bind(("127.0.0.1", port)).is_ok()
}

/// Returns whether a link starts with an HTTP(s) scheme.
pub fn is_external_link(link: &str) -> bool {
link.starts_with("http:") || link.starts_with("https:")
}

0 comments on commit 3e1f577

Please sign in to comment.