UNPKG

@backstage/plugin-techdocs

Version:

The Backstage plugin that renders technical documentation for your components

53 lines (49 loc) 2.04 kB
import { replaceGithubUrlType } from '@backstage/integration'; import FeedbackOutlinedIcon from '@material-ui/icons/FeedbackOutlined'; import { createElement } from 'react'; import parseGitUrl from 'git-url-parse'; import { renderReactElement } from './renderReactElement.esm.js'; const addGitFeedbackLink = (scmIntegrationsApi) => { return (dom) => { const sourceAnchor = dom.querySelector( '[title="Edit this page"]' ); if (!sourceAnchor || !sourceAnchor.href) { return dom; } const sourceURL = new URL(sourceAnchor.href); const integration = scmIntegrationsApi.byUrl(sourceURL); if (integration?.type !== "github" && integration?.type !== "gitlab") { return dom; } const title = dom.querySelector("article>h1")?.childNodes[0].textContent || ""; const issueTitle = encodeURIComponent(`Documentation Feedback: ${title}`); const issueDesc = encodeURIComponent( `Page source: ${sourceAnchor.href} Feedback:` ); const gitUrl = integration?.type === "github" ? replaceGithubUrlType(sourceURL.href, "blob") : sourceURL.href; const gitInfo = parseGitUrl(gitUrl); const repoPath = `/${gitInfo.organization}/${gitInfo.name}`; const feedbackLink = sourceAnchor.cloneNode(); switch (integration?.type) { case "gitlab": feedbackLink.href = `${sourceURL.origin}${repoPath}/issues/new?issue[title]=${issueTitle}&issue[description]=${issueDesc}`; break; case "github": feedbackLink.href = `${sourceURL.origin}${repoPath}/issues/new?title=${issueTitle}&body=${issueDesc}`; break; default: return dom; } renderReactElement(createElement(FeedbackOutlinedIcon), feedbackLink); feedbackLink.style.paddingLeft = "5px"; feedbackLink.title = "Leave feedback for this page"; feedbackLink.id = "git-feedback-link"; sourceAnchor?.insertAdjacentElement("beforebegin", feedbackLink); return dom; }; }; export { addGitFeedbackLink }; //# sourceMappingURL=addGitFeedbackLink.esm.js.map