Skip to content

Commit 325a7c1

Browse files
committed
Adds safety
Ensuring siteUrl is always a URL Adding a fallback if it’s not passed
1 parent 9ad7999 commit 325a7c1

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

processors/external_links_icon.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
// external_links_icon.ts adds an icon to external links in HTML pages
21
import type { Page } from "../types/lume.ts";
3-
export default function externalLinksIcon(siteUrl: URL) {
2+
3+
export default function externalLinksIcon(siteUrl?: URL) {
4+
const fallbackUrl = new URL("https://blog.esolia.pro");
5+
const baseUrl = siteUrl instanceof URL ? siteUrl : fallbackUrl;
6+
47
return (pages: Page[]) => {
58
for (const page of pages) {
69
const document = page.document;
@@ -15,14 +18,13 @@ export default function externalLinksIcon(siteUrl: URL) {
1518
if (!href || href.startsWith("#")) continue;
1619

1720
try {
18-
//const linkUrl = new URL(href, page.data.url || siteUrlStr);
19-
const linkUrl = new URL(href, siteUrl);
21+
const linkUrl = new URL(href, baseUrl);
2022

2123
const isExternal =
2224
(linkUrl.protocol === "http:" || linkUrl.protocol === "https:") &&
23-
(linkUrl.protocol !== siteUrl.protocol ||
24-
linkUrl.hostname !== siteUrl.hostname ||
25-
linkUrl.port !== siteUrl.port);
25+
(linkUrl.protocol !== baseUrl.protocol ||
26+
linkUrl.hostname !== baseUrl.hostname ||
27+
linkUrl.port !== baseUrl.port);
2628

2729
if (isExternal) {
2830
link.classList.add("after:content-['_↗']");

0 commit comments

Comments
 (0)