25 lines
631 B
TypeScript
25 lines
631 B
TypeScript
export interface LinkMetadata {
|
|
url: string;
|
|
title: string | null;
|
|
description: string | null;
|
|
image: string | null;
|
|
favicon: string | null;
|
|
domain: string;
|
|
}
|
|
|
|
const URL_REGEX = /https?:\/\/[^\s<>"')\]]+/g;
|
|
|
|
export function extractUrls(text: string): string[] {
|
|
// Dedupe: a URL repeated in the text (e.g. a `[url](url)` markdown link)
|
|
// should only yield a single preview card.
|
|
return Array.from(new Set(Array.from(text.matchAll(URL_REGEX), (m) => m[0])));
|
|
}
|
|
|
|
export function domainFromUrl(url: string): string {
|
|
try {
|
|
return new URL(url).hostname.replace(/^www\./, '');
|
|
} catch {
|
|
return url;
|
|
}
|
|
}
|