Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update link to make them Obsidian compatible #28

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading