Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions crates/core/src/data/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,16 @@ mod tests {
..Default::default()
}]),
blog_url: Some("https://blog.url".to_string()),
other_links: Some(vec![ItemLink {
name: "link".to_string(),
url: "https://link.url".to_string(),
}]),
other_links: Some(vec![
ItemLink {
name: "email".to_string(),
url: "mailto:team@example.com".to_string(),
},
ItemLink {
name: "link".to_string(),
url: "https://link.url".to_string(),
},
]),
..Default::default()
}),
..Default::default()
Expand Down
19 changes: 19 additions & 0 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub(crate) fn validate_url(kind: &str, url: Option<&String>) -> Result<()> {
};

// Check scheme
if kind == "other_link" && url.scheme() == "mailto" {
return Ok(());
}
if url.scheme() != "http" && url.scheme() != "https" {
return invalid_url("invalid scheme");
}
Expand Down Expand Up @@ -147,6 +150,11 @@ mod tests {
}
}

#[test]
fn validate_url_mailto_other_link_succeeds() {
validate_url("other_link", Some("mailto:team@example.com".to_string()).as_ref()).unwrap();
}

#[test]
#[should_panic(expected = "relative URL without a base")]
fn validate_url_error_parsing() {
Expand Down Expand Up @@ -188,4 +196,15 @@ mod tests {
assert!(error.starts_with(expected_error.as_str()));
}
}

#[test]
fn validate_url_mailto_other_field_fails() {
let error = validate_url(
"mailing_list",
Some("mailto:team@example.com".to_string()).as_ref(),
)
.unwrap_err()
.to_string();
assert_eq!(error, "invalid mailing_list url: invalid scheme");
}
}
3 changes: 2 additions & 1 deletion docs/config/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ categories:
# - # Name of the link (required).
# name: Link name
# # URL of the link (required).
# url: https://link.url
# # Supports `https://...` and `mailto:...`.
# url: mailto:team@example.com
other_links: []

# Package manager URL (optional). Link to the package manager where the item is
Expand Down
3 changes: 2 additions & 1 deletion docs/config/schema/data.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@
"type": "string",
"format": "uri",
"examples": [
"https://link.url"
"https://link.url",
"mailto:team@example.com"
]
}
},
Expand Down
14 changes: 11 additions & 3 deletions ui/common/src/components/ExternalLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const LinkClass = css`
`;

export const ExternalLink = (props: Props) => {
const isMailtoLink = () => /^mailto:/i.test(props.href);
const linkRel = () => (isMailtoLink() ? undefined : 'noopener noreferrer');
const linkTarget = () => (isMailtoLink() ? undefined : props.target || '_blank');

const getData = () => {
return (
<>
Expand Down Expand Up @@ -54,7 +58,11 @@ export const ExternalLink = (props: Props) => {
if (props.onClick) props.onClick();

if (isUndefined(props.disabled) || !props.disabled) {
window.open(props.href, props.target || '_blank');
if (isMailtoLink()) {
window.location.href = props.href;
} else {
window.open(props.href, linkTarget());
}
}
}}
aria-label={props.label || 'Open external link'}
Expand All @@ -68,8 +76,8 @@ export const ExternalLink = (props: Props) => {
title={props.title}
class={`link ${LinkClass} ${props.class}`}
href={props.href}
target={props.target || '_blank'}
rel="noopener noreferrer"
target={linkTarget()}
rel={linkRel()}
onClick={(e) => {
e.stopPropagation();

Expand Down