From 897a9590b7a7dea919e6b0afb040a6d18c24d1c0 Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Fri, 31 Mar 2023 10:47:23 -0700 Subject: [PATCH] prettier, fix, updated version --- src/processor.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/processor.ts b/src/processor.ts index 5079303..c691bfc 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -65,13 +65,23 @@ export class D2Processor { await debouncedFunc(source, el, ctx, newAbortController.signal); }; + isValidUrl = (urlString: string) => { + let url; + try { + url = new URL(urlString); + } catch (e) { + return false; + } + return url.protocol === "http:" || url.protocol === "https:"; + }; + formatLinks = (svgEl: HTMLElement) => { // Add attributes to tags to make them Obsidian compatible : const links = svgEl.querySelectorAll("a"); links.forEach((link: HTMLElement) => { - const href = link.getAttribute("href")??"" + const href = link.getAttribute("href") ?? ""; // Check for internal link - if (!href.match(/:\/\//g)){ + if (!this.isValidUrl(href)) { link.classList.add("internal-link"); link.setAttribute("data-href", href); link.setAttribute("target", "_blank");