Skip to content

Auto-Link URL Labels #769

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

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions app/src/components/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { monacoMarkerErrorSeverity } from "../lib/constants";
import { cytoscape } from "../lib/cytoscape";
import { getElements } from "../lib/getElements";
import { DEFAULT_GRAPH_PADDING } from "../lib/graphOptions";
import { isError } from "../lib/helpers";
import { isError, isUrl } from "../lib/helpers";
import { useCanEdit } from "../lib/hooks";
import {
preprocessStyle,
Expand Down Expand Up @@ -224,9 +224,13 @@ function initializeGraph({
});
// on node tap, if has a href, open it
cyCurrent.on("tap", "node", function handleTap(this: NodeSingular) {
const { href } = this.data();
if (href) {
window.open(href, "_blank");
const { href, label } = this.data();
const url =
href || (typeof label === "string" && isUrl(label) ? label : null);
if (url) {
// Add https:// if no protocol is specified
const fullUrl = url.startsWith("http") ? url : `https://${url}`;
window.open(fullUrl, "_blank");
}
});

Expand Down
16 changes: 16 additions & 0 deletions app/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,19 @@ export function hasOwnProperty<
// eslint-disable-next-line no-prototype-builtins
return obj.hasOwnProperty(prop);
}

export function isUrl(str: string): boolean {
try {
// Check if it starts with a protocol
if (str.startsWith("http://") || str.startsWith("https://")) {
return true;
}
// Check if it looks like a domain (e.g., example.com, www.example.com)
if (/^(www\.)?[a-zA-Z0-9-]+\.[a-zA-Z]{2,}/.test(str)) {
return true;
}
return false;
} catch {
return false;
}
}
Loading