backstage-plugin-techdocs-addon-sourcegraph
Version:
A module that extends Sourcegraph capabilities into Backstage TechDocs.
60 lines (56 loc) • 2.18 kB
JavaScript
import { createPlugin } from '@backstage/core-plugin-api';
import { useShadowRootElements, createTechDocsAddonExtension, TechDocsAddonLocations } from '@backstage/plugin-techdocs-react';
import { useEffect } from 'react';
const SourcegraphNotebookAddon = (props) => {
const highlightTables = useShadowRootElements([".highlighttable"]);
useEffect(() => {
var _a, _b;
for (const highlightTable of highlightTables) {
if (highlightTable.style.display === "none") {
continue;
}
if (!highlightTable.classList.contains("language-text")) {
continue;
}
const code = highlightTable.querySelector("code");
if (!code) {
continue;
}
const text = (_a = code.textContent) == null ? void 0 : _a.trim();
if (!text) {
continue;
}
const matches = text.match(new RegExp(`^https://${props.domain}/notebooks/(.+?)$`, "i"));
if (!matches) {
continue;
}
highlightTable.style.display = "none";
const [_, id] = matches;
const iframe = document.createElement("iframe");
iframe.setAttribute("src", `https://${props.domain}/embed/notebooks/${id}`);
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-popups");
const container = document.createElement("div");
container.dataset.notebookId = id;
if (props.callback) {
props.callback({ container, iframe, id, url: `https://${props.domain}/notebooks/${id}` });
} else {
container.append(iframe);
}
(_b = highlightTable.parentNode) == null ? void 0 : _b.insertBefore(container, highlightTable.nextSibling);
}
}, [highlightTables, props]);
return null;
};
const techdocsAddonSourcegraphPlugin = createPlugin({
id: "techdocs-addon-sourcegraph"
});
const SourcegraphNotebook = techdocsAddonSourcegraphPlugin.provide(
createTechDocsAddonExtension({
name: "SourcegraphNotebook",
location: TechDocsAddonLocations.Content,
component: SourcegraphNotebookAddon
})
);
export { SourcegraphNotebook, techdocsAddonSourcegraphPlugin };
//# sourceMappingURL=index.esm.js.map