UNPKG

@backstage/plugin-techdocs-module-addons-contrib

Version:

Plugin module for contributed TechDocs Addons

74 lines (67 loc) 2.74 kB
import parseGitUrl from 'git-url-parse'; import { useApi, configApiRef } from '@backstage/core-plugin-api'; import { replaceGithubUrlType, replaceGitLabUrlType } from '@backstage/integration'; import { scmIntegrationsApiRef } from '@backstage/integration-react'; import { useShadowRootElements, useShadowRootSelection } from '@backstage/plugin-techdocs-react'; import { PAGE_EDIT_LINK_SELECTOR } from './constants.esm.js'; const resolveBlobUrl = (url, type) => { if (type === "github") { return replaceGithubUrlType(url, "blob"); } else if (type === "gitlab") { return replaceGitLabUrlType(url, "blob"); } console.error( `Invalid SCM type ${type} found in ReportIssue addon for URL ${url}!` ); return url; }; const getTitle = (selection) => { const text = selection.toString().substring(0, 70); const ellipsis = text.length === 70 ? "..." : ""; return `Documentation feedback: ${text}${ellipsis}`; }; const getBody = (selection, markdownUrl, appTitle) => { const title = "## Documentation Feedback \u{1F4DD}"; const subheading = "#### The highlighted text:"; const commentHeading = "#### The comment on the text:"; const commentPlaceholder = "_>replace this line with your comment<_"; const highlightedTextAsQuote = selection.toString().trim().split("\n").map((line) => `> ${line.trim()}`).join("\n"); const facts = [ `${appTitle} URL: <${window.location.href}> Markdown URL: <${markdownUrl}>` ]; return `${title} ${subheading} ${highlightedTextAsQuote} ${commentHeading} ${commentPlaceholder} ___ ${facts}`; }; const useGitTemplate = (debounceTime) => { const initialTemplate = { title: "", body: "" }; const selection = useShadowRootSelection(debounceTime); const [editLink] = useShadowRootElements([PAGE_EDIT_LINK_SELECTOR]); const url = editLink?.href ?? ""; const scmIntegrationsApi = useApi(scmIntegrationsApiRef); const configApi = useApi(configApiRef); const appTitle = configApi.getOptionalString("app.title") || "Backstage"; if (!selection || !url) return initialTemplate; const type = scmIntegrationsApi.byUrl(url)?.type; if (!type) return initialTemplate; return { title: getTitle(selection), body: getBody(selection, resolveBlobUrl(url, type), appTitle) }; }; const useGitRepository = () => { const scmIntegrationsApi = useApi(scmIntegrationsApiRef); const [editLink] = useShadowRootElements([PAGE_EDIT_LINK_SELECTOR]); const url = editLink?.href ?? ""; if (!url) return null; const type = scmIntegrationsApi.byUrl(url)?.type; if (!type) return null; return { ...parseGitUrl(resolveBlobUrl(url, type)), type }; }; export { getBody, getTitle, useGitRepository, useGitTemplate }; //# sourceMappingURL=hooks.esm.js.map