move helpers to techdocs-react

Signed-off-by: Chris Suich <csuich2@gmail.com>
This commit is contained in:
Chris Suich
2025-04-28 12:42:35 -04:00
parent ec7b35d77e
commit ae717aa54c
10 changed files with 79 additions and 74 deletions
+66 -1
View File
@@ -15,7 +15,23 @@
*/
import { Config } from '@backstage/config';
import { CompoundEntityRef } from '@backstage/catalog-model';
import {
CompoundEntityRef,
DEFAULT_NAMESPACE,
Entity,
parseEntityRef,
} from '@backstage/catalog-model';
import {
TECHDOCS_EXTERNAL_ANNOTATION,
TECHDOCS_EXTERNAL_PATH_ANNOTATION,
} from '@backstage/plugin-techdocs-common';
import { RouteFunc } from '@backstage/core-plugin-api';
export type TechDocsRouteFunc = RouteFunc<{
namespace: string;
kind: string;
name: string;
}>;
/**
* Lower-case entity triplets by default, but allow override.
@@ -35,3 +51,52 @@ export function toLowercaseEntityRefMaybe(
return entityRef;
}
export function getEntityRootTechDocsPath(entity: Entity): string {
let path = entity.metadata.annotations?.[TECHDOCS_EXTERNAL_PATH_ANNOTATION];
if (!path) {
return '';
}
if (!path.startsWith('/')) {
path = `/${path}`;
}
return path;
}
export const buildTechDocsURL = (
entity: Entity,
routeFunc: TechDocsRouteFunc | undefined,
) => {
if (!routeFunc) {
return undefined;
}
let namespace = entity.metadata.namespace || DEFAULT_NAMESPACE;
let kind = entity.kind;
let name = entity.metadata.name;
if (entity.metadata.annotations?.[TECHDOCS_EXTERNAL_ANNOTATION]) {
try {
const techdocsRef = parseEntityRef(
entity.metadata.annotations?.[TECHDOCS_EXTERNAL_ANNOTATION],
);
namespace = techdocsRef.namespace;
kind = techdocsRef.kind;
name = techdocsRef.name;
} catch {
// not a fan of this but we don't care if the parseEntityRef fails
}
}
const url = routeFunc({
namespace,
kind,
name,
});
// Add on the external entity path to the url if one exists. This allows deep linking into another
// entities TechDocs.
const path = getEntityRootTechDocsPath(entity);
return `${url}${path}`;
};
+6 -1
View File
@@ -52,4 +52,9 @@ export {
useShadowRootElements,
useShadowRootSelection,
} from './hooks';
export { toLowercaseEntityRefMaybe } from './helpers';
export {
toLowercaseEntityRefMaybe,
getEntityRootTechDocsPath,
buildTechDocsURL,
} from './helpers';
export type { TechDocsRouteFunc } from './helpers';