Skip to content

Commit

Permalink
Merge pull request #28 from Portevent/master
Browse files Browse the repository at this point in the history
Update link to make them Obsidian compatible
  • Loading branch information
gavin-ts authored Nov 28, 2023
2 parents ca41eb2 + 897a959 commit d4470fd
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ 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 <a> tags to make them Obsidian compatible :
const links = svgEl.querySelectorAll("a");
links.forEach((link: HTMLElement) => {
const href = link.getAttribute("href") ?? "";
// Check for internal link
if (!this.isValidUrl(href)) {
link.classList.add("internal-link");
link.setAttribute("data-href", href);
link.setAttribute("target", "_blank");
link.setAttribute("rel", "noopener");
}
});
};

sanitizeSVGIDs = (svgEl: HTMLElement, docID: string): string => {
// append docId to <marker> || <mask> || <filter> id's so that they're unique across different panels & edit/view mode
const overrides = svgEl.querySelectorAll("marker, mask, filter");
Expand All @@ -91,6 +116,7 @@ export class D2Processor {
svgEl.style.height = "fit-content";
svgEl.style.width = "fit-content";

this.formatLinks(svgEl);
containerEl.innerHTML = this.sanitizeSVGIDs(svgEl, ctx.docId);
}

Expand Down

0 comments on commit d4470fd

Please sign in to comment.