-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.ts
More file actions
108 lines (91 loc) · 3.12 KB
/
Copy pathreference.ts
File metadata and controls
108 lines (91 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import type { Blueprint, Endpoint } from '@seamapi/blueprint'
import type Metalsmith from 'metalsmith'
import { toCapitalCase } from 'lib/handlebars/helpers.js'
import { apiReferenceRoot } from './config.js'
import {
type ApiEndpointLayoutContext,
type ApiNamespaceLayoutContext,
type ApiRouteLayoutContext,
setApiRouteLayoutContext,
setEndpointLayoutContext,
setNamespaceLayoutContext,
} from './layout/index.js'
import {
type ApiSummaryLayoutContext,
setSummaryLayoutContext,
} from './layout/summary.js'
import { PathMetadataSchema } from './path-metadata.js'
interface Metadata {
blueprint: Blueprint
pathMetadata: unknown
}
type File = ApiEndpointLayoutContext &
ApiRouteLayoutContext &
ApiNamespaceLayoutContext &
ApiSummaryLayoutContext & { layout: string }
const rootPath = apiReferenceRoot
const indexFile = 'README.md'
export const reference = (
files: Metalsmith.Files,
metalsmith: Metalsmith,
): void => {
const metadata = metalsmith.metadata() as Metadata
// UPSTREAM: Ideally, path metadata would be unnecessary and contained inside the blueprint.
const pathMetadata =
'pathMetadata' in metadata
? PathMetadataSchema.parse(metadata.pathMetadata)
: {}
const { blueprint } = metadata
for (const { path, name } of [...blueprint.routes, ...blueprint.namespaces]) {
if (path in pathMetadata) continue
pathMetadata[path] = PathMetadataSchema.valueSchema.parse({
title: toCapitalCase(name),
})
}
const { resources } = blueprint
const namespaces = blueprint.namespaces.filter(
({ isUndocumented }) => !isUndocumented,
)
for (const namespace of namespaces) {
const { path } = namespace
const k = `${rootPath}${path}/${indexFile}`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'api-namespace.hbs'
setNamespaceLayoutContext(file, path, resources, pathMetadata)
}
const routes = blueprint.routes.filter(({ path, isUndocumented }) => {
if (isUndocumented) return false
if (pathMetadata[path]?.title == null) return false
return true
})
const endpoints: Endpoint[] = []
for (const route of routes) {
const k = `${rootPath}${route.path}/${indexFile}`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'api-route.hbs'
setApiRouteLayoutContext(file, route, blueprint, pathMetadata)
for (const endpoint of route.endpoints) {
if (endpoint.isUndocumented) continue
if (endpoint.title.length === 0) continue
const k = `${rootPath}${endpoint.path}.md`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'api-endpoint.hbs'
setEndpointLayoutContext(
file,
endpoint,
resources,
blueprint.actionAttempts,
pathMetadata,
)
endpoints.push(endpoint)
}
}
const k = `${rootPath}/_summary.md`
files[k] = { contents: Buffer.from('\n') }
const file = files[k] as unknown as File
file.layout = 'summary.hbs'
setSummaryLayoutContext(file, { routes, namespaces, endpoints, pathMetadata })
}