Skip to content

Commit 2e0c975

Browse files
committed
Makes input more flexible
1 parent 325a7c1 commit 2e0c975

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

processors/external_links_icon.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import type { Page } from "../types/lume.ts";
22

3-
export default function externalLinksIcon(siteUrl?: URL) {
4-
const fallbackUrl = new URL("https://blog.esolia.pro");
5-
const baseUrl = siteUrl instanceof URL ? siteUrl : fallbackUrl;
3+
/**
4+
* Adds an external link icon to <a target="_blank"> elements that point to external sites.
5+
* Skips links inside elements with the class `.no-external-icon`.
6+
*
7+
* @param siteUrlInput - The base URL of the site, as a string or URL object.
8+
*/
9+
export default function externalLinksIcon(siteUrlInput?: string | URL) {
10+
const fallbackUrl = new URL("https://example.com");
11+
const siteUrl = siteUrlInput
12+
? siteUrlInput instanceof URL
13+
? siteUrlInput
14+
: new URL(siteUrlInput)
15+
: fallbackUrl;
616

717
return (pages: Page[]) => {
818
for (const page of pages) {
@@ -18,13 +28,13 @@ export default function externalLinksIcon(siteUrl?: URL) {
1828
if (!href || href.startsWith("#")) continue;
1929

2030
try {
21-
const linkUrl = new URL(href, baseUrl);
31+
const linkUrl = new URL(href, siteUrl);
2232

2333
const isExternal =
2434
(linkUrl.protocol === "http:" || linkUrl.protocol === "https:") &&
25-
(linkUrl.protocol !== baseUrl.protocol ||
26-
linkUrl.hostname !== baseUrl.hostname ||
27-
linkUrl.port !== baseUrl.port);
35+
(linkUrl.protocol !== siteUrl.protocol ||
36+
linkUrl.hostname !== siteUrl.hostname ||
37+
linkUrl.port !== siteUrl.port);
2838

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

0 commit comments

Comments
 (0)