Skip to content
File

Blob: src/client/lib/external-link.ts

typescript25 lines
1function normalizeExternalLinkTarget(value: string) {
2 return value.trim().replace(/^['"]|['"]$/g, "");
3}
4 
5export function buildExternalLinkRedirectHref(value: string) {
6 return `/link?url=${encodeURIComponent(normalizeExternalLinkTarget(value))}`;
7}
8 
9export function parseExternalLinkTarget(rawTarget: string | null) {
10 if (!rawTarget) {
11 return null;
12 }
13 
14 try {
15 const target = new URL(rawTarget, window.location.origin);
16 if (target.protocol !== "http:" && target.protocol !== "https:") {
17 return null;
18 }
19 
20 return target;
21 } catch {
22 return null;
23 }
24}