export function repositoryUrlFor(repositoryResource: string): string { const trimmed = repositoryResource.trim().replace(/\.git$/i, "").replace(/\/+$/, ""); if (/^git@tangled\.org:/i.test(trimmed)) { return `https://tangled.org/${trimmed.replace(/^git@tangled\.org:/i, "")}`; } if (/^https?:\/\/(?:www\.)?tangled\.org\//i.test(trimmed)) { return trimmed.replace(/^https?:\/\/(?:www\.)?tangled\.org\//i, "https://tangled.org/"); } if (/^tangled\.org\//i.test(trimmed)) { return `https://tangled.org/${trimmed.replace(/^tangled\.org\//i, "")}`; } if (/^git@github\.com:/i.test(trimmed)) { return `https://github.com/${trimmed.replace(/^git@github\.com:/i, "")}`; } if (/^https?:\/\/(?:www\.)?github\.com\//i.test(trimmed)) { return trimmed.replace(/^https?:\/\/(?:www\.)?github\.com\//i, "https://github.com/"); } if (/^github\.com\//i.test(trimmed)) { return `https://github.com/${trimmed.replace(/^github\.com\//i, "")}`; } return `https://github.com/${trimmed}`; } export function createGitHubResourceLink(githubResource: string, optionalText?: string): DocumentFragment { const newLink = new DocumentFragment(); const linkElement = document.createElement("a"); linkElement.textContent = githubResource; linkElement.href = repositoryUrlFor(githubResource); linkElement.target = "_blank"; newLink.appendChild(linkElement); if (optionalText) { const textNode = document.createTextNode(optionalText); newLink.appendChild(textNode); } return newLink; } export function createLink({ prependText, url, text, appendText }: { prependText?: string; url: string; text: string; appendText?: string; }): DocumentFragment { const newLink = new DocumentFragment(); const linkElement = document.createElement("a"); linkElement.textContent = text; linkElement.href = url; if (prependText) { const textNode = document.createTextNode(prependText); newLink.appendChild(textNode); } newLink.appendChild(linkElement); if (appendText) { const textNode = document.createTextNode(appendText); newLink.appendChild(textNode); } return newLink; }