From bd5af5e2e1c4bf34e8de09b040f3f9e0e53b873b Mon Sep 17 00:00:00 2001 From: Kuba Suder Date: Fri, 17 May 2024 20:20:37 +0200 Subject: [PATCH] fall back to text link if link has invalid URL --- embed_component.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/embed_component.js b/embed_component.js index aaa8bb5..4151b33 100644 --- a/embed_component.js +++ b/embed_component.js @@ -90,10 +90,23 @@ class EmbedComponent { /** @returns {AnyElement} */ buildLinkComponent() { + let hostname; + + try { + hostname = new URL(this.embed.url).hostname; + } catch (error) { + console.log("Invalid URL:" + error); + + let a = $tag('a', { href: this.embed.url, text: this.embed.title || this.embed.url }); + let p = $tag('p'); + p.append('[Link: ', a, ']'); + return p; + } + let a = $tag('a.link-card', { href: this.embed.url, target: '_blank' }); let box = $tag('div'); - let domain = $tag('p.domain', { text: new URL(this.embed.url).hostname }); + let domain = $tag('p.domain', { text: hostname }); let title = $tag('h2', { text: this.embed.title }); box.append(domain, title); -- 2.51.2