Skip to content

Commit

Permalink
Support page.title and page.description
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Oct 15, 2024
1 parent 8ae8b73 commit 27765c2
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"execa": "^9.3.1",
"jstransformer-handlebars": "^1.2.0",
"metalsmith": "^2.6.3",
"prettier": "^3.0.0"
"prettier": "^3.0.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/command-exists": "^1.2.3",
Expand Down
19 changes: 18 additions & 1 deletion src/data/paths.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
---
/acs/systems:
description: Access Control Systems
page:
title: Systems
description: Access Control Systems

/acs/systems/get:
page:
title: Get an ACS System
description: Get an ACS System

/acs/systems/list:
page:
title: List ACS Systems
description: List ACS Systems

/acs/systems/list_compatible_credential_manager_acs_systems:
page:
title: List Compatible Credential Manager ACS Systems
description: List Compatible Credential Manager ACS Systems
3 changes: 3 additions & 0 deletions src/layouts/api-endpoint.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
description: {{page.description}}
---
# {{title}}

```
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/api-route.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: {{description}}
description: {{page.description}}
---
{{#each resources}}
# `{{name}}`
Expand Down
18 changes: 17 additions & 1 deletion src/lib/blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createBlueprint, TypesModuleSchema } from '@seamapi/blueprint'
import * as types from '@seamapi/types/connect'
import type Metalsmith from 'metalsmith'
import { z } from 'zod'

import { formatCode } from './format-code.js'

Expand All @@ -14,7 +15,10 @@ export const blueprint = async (
'codeSampleDefinitions' in metadata ? metadata.codeSampleDefinitions : []

// UPSTREAM: Ideally, path metadata would be unnecessary and contained inside the blueprint.
const pathMetadata = 'pathMetadata' in metadata ? metadata.pathMetadata : {}
const pathMetadata =
'pathMetadata' in metadata
? PathMetadataSchema.parse(metadata.pathMetadata)
: {}

const typesModule = TypesModuleSchema.parse({
...types,
Expand All @@ -24,3 +28,15 @@ export const blueprint = async (
const blueprint = await createBlueprint(typesModule, { formatCode })
Object.assign(metadata, { ...blueprint, pathMetadata })
}

const PathMetadataSchema = z.record(
z.string(),
z.object({
page: z.object({
title: z.string().trim().min(1),
description: z.string().trim().min(1),
}),
}),
)

export type PathMetadata = z.infer<typeof PathMetadataSchema>
25 changes: 19 additions & 6 deletions src/lib/layout-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from '@seamapi/blueprint'
import { pascalCase } from 'change-case'

import type { PathMetadata } from './reference.js'
import type { PathMetadata } from './blueprint.js'

const supportedSdks: CodeSampleSdk[] = [
'javascript',
Expand All @@ -21,6 +21,10 @@ export interface EndpointLayoutContext {
description: string
title: string
path: string
page: {
title: string
description: string
}
request: {
preferredMethod: string
parameters: Array<{
Expand Down Expand Up @@ -54,10 +58,16 @@ export interface EndpointLayoutContext {
export function setEndpointLayoutContext(
file: Partial<EndpointLayoutContext>,
endpoint: Endpoint,
pathMetadata: PathMetadata,
): void {
file.description = endpoint.description
file.title = endpoint.title
file.path = endpoint.path
const page = pathMetadata[endpoint.path]?.page
if (page == null) {
throw new Error(`Missing page metadata for ${endpoint.path}`)
}
file.page = page

file.request = {
preferredMethod: endpoint.request?.preferredMethod ?? '',
Expand Down Expand Up @@ -115,6 +125,10 @@ interface ContextResource {
type ContextEndpoint = Pick<Endpoint, 'path' | 'description'>

export interface RouteLayoutContext {
page: {
title: string
description: string
}
description: string
resources: ContextResource[]
endpoints: ContextEndpoint[]
Expand All @@ -126,12 +140,11 @@ export function setApiRouteLayoutContext(
blueprint: Blueprint,
pathMetadata: PathMetadata,
): void {
const description = pathMetadata[route.path]?.description
if (description == null || description.trim() === '') {
throw new Error(`Missing description for ${route.path}`)
const page = pathMetadata[route.path]?.page
if (page == null) {
throw new Error(`Missing page metadata for ${route.path}`)
}

file.description = description
file.page = page
file.endpoints = route.endpoints.map(({ path, name, description }) => ({
path,
name,
Expand Down
12 changes: 3 additions & 9 deletions src/lib/reference.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Blueprint } from '@seamapi/blueprint'
import type Metalsmith from 'metalsmith'

import type { PathMetadata } from './blueprint.js'
import {
type EndpointLayoutContext,
type RouteLayoutContext,
Expand All @@ -14,13 +15,6 @@ type Metadata = Partial<Pick<Blueprint, 'routes' | 'resources'>>

type File = EndpointLayoutContext & RouteLayoutContext & { layout: string }

export type PathMetadata = Record<
string,
{
description?: string | null
}
>

export const reference = (
files: Metalsmith.Files,
metalsmith: Metalsmith,
Expand Down Expand Up @@ -59,7 +53,7 @@ export const reference = (
}
const file = files[k] as unknown as File
file.layout = 'api-endpoint.hbs'
setEndpointLayoutContext(file, endpoint)
setEndpointLayoutContext(file, endpoint, pathMetadata)

for (const sdk of sdks) {
const k = `sdk/${sdk}${endpoint.path}.md`
Expand All @@ -68,7 +62,7 @@ export const reference = (
}
const file = files[k] as unknown as File
file.layout = 'sdk-reference.hbs'
setEndpointLayoutContext(file, endpoint)
setEndpointLayoutContext(file, endpoint, pathMetadata)
}
}
}
Expand Down

0 comments on commit 27765c2

Please sign in to comment.