move helpers to techdocs-react
Signed-off-by: Chris Suich <csuich2@gmail.com>
This commit is contained in:
@@ -20,12 +20,12 @@ import {
|
||||
parseEntityRef,
|
||||
} from '@backstage/catalog-model';
|
||||
import { TECHDOCS_EXTERNAL_ANNOTATION } from '@backstage/plugin-techdocs-common';
|
||||
import { getEntityRootTechDocsPath } from '@backstage/plugin-techdocs-react';
|
||||
|
||||
import { TechDocsReaderPage } from './plugin';
|
||||
import { TechDocsReaderPageContent } from './reader/components/TechDocsReaderPageContent';
|
||||
import { TechDocsReaderPageSubheader } from './reader/components/TechDocsReaderPageSubheader';
|
||||
import { useEntityPageTechDocsRedirect } from './search/hooks/useTechDocsLocation';
|
||||
import { getEntityRootTechDocsPath } from './helpers';
|
||||
|
||||
type EntityPageDocsProps = {
|
||||
entity: Entity;
|
||||
|
||||
@@ -14,23 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
DEFAULT_NAMESPACE,
|
||||
Entity,
|
||||
parseEntityRef,
|
||||
} from '@backstage/catalog-model';
|
||||
import { Config } from '@backstage/config';
|
||||
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.
|
||||
export function toLowerMaybe(str: string, config: Config) {
|
||||
@@ -40,52 +24,3 @@ export function toLowerMaybe(str: string, config: Config) {
|
||||
? str
|
||||
: str.toLocaleLowerCase('en-US');
|
||||
}
|
||||
|
||||
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}`;
|
||||
};
|
||||
|
||||
@@ -46,7 +46,6 @@ export {
|
||||
LegacyEmbeddedDocsRouter as EmbeddedDocsRouter,
|
||||
Router,
|
||||
} from './Router';
|
||||
export { buildTechDocsURL, getEntityRootTechDocsPath } from './helpers';
|
||||
|
||||
export type { TechDocsSearchResultListItemProps } from './search/components/TechDocsSearchResultListItem';
|
||||
|
||||
@@ -70,5 +69,3 @@ export type {
|
||||
};
|
||||
|
||||
export * from './overridableComponents';
|
||||
|
||||
export type { TechDocsRouteFunc } from './helpers';
|
||||
|
||||
Reference in New Issue
Block a user