diff --git a/packages/starlight-openapi/libs/env.d.ts b/packages/starlight-openapi/libs/env.d.ts new file mode 100644 index 0000000..ed64f0f --- /dev/null +++ b/packages/starlight-openapi/libs/env.d.ts @@ -0,0 +1,7 @@ +interface ImportMetaEnv { + readonly BASE_URL: string +} + +interface ImportMeta { + readonly env: ImportMetaEnv +} diff --git a/packages/starlight-openapi/libs/path.ts b/packages/starlight-openapi/libs/path.ts index 9bee38d..65508cc 100644 --- a/packages/starlight-openapi/libs/path.ts +++ b/packages/starlight-openapi/libs/path.ts @@ -4,7 +4,13 @@ import type { StarlightOpenAPISchemaConfig } from './schema' export { slug } from 'github-slugger' -export function getBaseLink(config: StarlightOpenAPISchemaConfig) { +const base = stripTrailingSlash(import.meta.env.BASE_URL) + +/** + * Does not take the Astro `base` configuration option into account. + * @see {@link getBaseLink} for a link that does. + */ +export function getBasePath(config: StarlightOpenAPISchemaConfig) { const path = config.base .split('/') .map((part) => slug(part)) @@ -13,6 +19,16 @@ export function getBaseLink(config: StarlightOpenAPISchemaConfig) { return `/${path}/` } +/** + * Takes the Astro `base` configuration option into account. + * @see {@link getBasePath} for a slug that does not. + */ +export function getBaseLink(config: StarlightOpenAPISchemaConfig) { + const path = stripLeadingSlash(getBasePath(config)) + + return path ? `${base}/${path}` : `${base}/` +} + export function stripLeadingAndTrailingSlashes(path: string): string { return stripLeadingSlash(stripTrailingSlash(path)) } diff --git a/packages/starlight-openapi/libs/pathItem.ts b/packages/starlight-openapi/libs/pathItem.ts index 97d2432..be809bc 100644 --- a/packages/starlight-openapi/libs/pathItem.ts +++ b/packages/starlight-openapi/libs/pathItem.ts @@ -1,10 +1,10 @@ import { getOperationsByTag, getWebhooksOperations } from './operation' -import { getBaseLink } from './path' +import { getBasePath } from './path' import type { Schema } from './schema' import { makeSidebarGroup, makeSidebarLink, type SidebarManualGroup } from './starlight' export function getPathItemSidebarGroups({ config, document }: Schema): SidebarManualGroup['items'] { - const baseLink = getBaseLink(config) + const baseLink = getBasePath(config) const operations = getOperationsByTag(document) return [...operations.entries()].map(([tag, operations]) => @@ -17,7 +17,7 @@ export function getPathItemSidebarGroups({ config, document }: Schema): SidebarM } export function getWebhooksSidebarGroups({ config, document }: Schema): SidebarManualGroup['items'] { - const baseLink = getBaseLink(config) + const baseLink = getBasePath(config) const operations = getWebhooksOperations(document) if (operations.length === 0) { diff --git a/packages/starlight-openapi/libs/route.ts b/packages/starlight-openapi/libs/route.ts index 7078bb6..e6b11d0 100644 --- a/packages/starlight-openapi/libs/route.ts +++ b/packages/starlight-openapi/libs/route.ts @@ -1,14 +1,14 @@ import schemas from 'virtual:starlight-openapi-schemas' import { getOperationsByTag, getWebhooksOperations, type PathItemOperation } from './operation' -import { getBaseLink, stripLeadingAndTrailingSlashes } from './path' +import { getBasePath, stripLeadingAndTrailingSlashes } from './path' import type { Schema } from './schema' export function getSchemaStaticPaths(): StarlighOpenAPIRoute[] { return Object.values(schemas).flatMap((schema) => [ { params: { - openAPISlug: stripLeadingAndTrailingSlashes(getBaseLink(schema.config)), + openAPISlug: stripLeadingAndTrailingSlashes(getBasePath(schema.config)), }, props: { schema, @@ -21,7 +21,7 @@ export function getSchemaStaticPaths(): StarlighOpenAPIRoute[] { } function getPathItemStaticPaths(schema: Schema): StarlighOpenAPIRoute[] { - const baseLink = getBaseLink(schema.config) + const baseLink = getBasePath(schema.config) const operations = getOperationsByTag(schema.document) return [...operations.entries()].flatMap(([, operations]) => @@ -39,7 +39,7 @@ function getPathItemStaticPaths(schema: Schema): StarlighOpenAPIRoute[] { } function getWebhooksStaticPaths(schema: Schema): StarlighOpenAPIRoute[] { - const baseLink = getBaseLink(schema.config) + const baseLink = getBasePath(schema.config) const operations = getWebhooksOperations(schema.document) return operations.map((operation) => ({ diff --git a/packages/starlight-openapi/libs/schema.ts b/packages/starlight-openapi/libs/schema.ts index 115c73f..5cbbb86 100644 --- a/packages/starlight-openapi/libs/schema.ts +++ b/packages/starlight-openapi/libs/schema.ts @@ -1,7 +1,7 @@ import { z } from 'astro/zod' import type { OpenAPI } from 'openapi-types' -import { getBaseLink, stripLeadingAndTrailingSlashes } from './path' +import { getBasePath, stripLeadingAndTrailingSlashes } from './path' import { getPathItemSidebarGroups, getWebhooksSidebarGroups } from './pathItem' import { makeSidebarGroup, makeSidebarLink, type SidebarManualGroup } from './starlight' @@ -32,7 +32,7 @@ export function getSchemaSidebarGroups(schema: Schema): SidebarManualGroup { return makeSidebarGroup( config.label ?? document.info.title, [ - makeSidebarLink('Overview', getBaseLink(config)), + makeSidebarLink('Overview', getBasePath(config)), ...getPathItemSidebarGroups(schema), ...getWebhooksSidebarGroups(schema), ],