Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions scripts/build-docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const baseConfig = {
wrapDefault: true,
collapseDefault: false,
hideTitleDefault: false,
deprecatedDefault: false,
},
flags: {
skipGit: true,
Expand Down Expand Up @@ -459,6 +460,7 @@ title: Simple Test
wrapDefault: false,
collapseDefault: false,
hideTitleDefault: false,
deprecatedDefault: false,
},
}),
)
Expand Down Expand Up @@ -4346,3 +4348,159 @@ describe('API Errors Generation', () => {
expect(fapi).toContain('Status Code: 400')
})
})

describe('File deprecation', () => {
test('Should include the deprecation frontmatter in the dist file', async () => {
const { tempDir, readFile } = await createTempFiles([
{
path: './docs/manifest.json',
content: JSON.stringify({
navigation: [[{ title: 'API Doc', href: '/docs/api-doc' }]],
}),
},
{
path: './docs/api-doc.mdx',
content: `---
title: API Documentation
description: Generated API docs
deprecated: true
---

# API Documentation`,
},
])

await build(
await createConfig({
...baseConfig,
basePath: tempDir,
validSdks: ['react'],
}),
)

expect(await readFile('dist/api-doc.mdx')).toContain(`---
title: API Documentation
description: Generated API docs
deprecated: true
---

# API Documentation`)
})

test('Should include the deprecation field in the manifest', async () => {
const { tempDir, readFile } = await createTempFiles([
{
path: './docs/manifest.json',
content: JSON.stringify({
navigation: [[{ title: 'API Doc', href: '/docs/api-doc' }]],
}),
},
{
path: './docs/api-doc.mdx',
content: `---
title: API Documentation
description: Generated API docs
deprecated: true
---

# API Documentation`,
},
])

await build(
await createConfig({
...baseConfig,
basePath: tempDir,
validSdks: ['react'],
}),
)

expect(JSON.parse(await readFile('dist/manifest.json'))).toEqual({
navigation: [
[
{
href: '/docs/api-doc',
title: 'API Doc',
deprecated: true,
},
],
],
})
})

test('Should include the deprecation field in the group above', async () => {
const { tempDir, readFile } = await createTempFiles([
{
path: './docs/manifest.json',
content: JSON.stringify({
navigation: [
[
{
title: 'API',
items: [
[
{ title: 'API Doc', href: '/docs/api-doc' },
{ title: 'API Doc 2', href: '/docs/api-doc-2' },
],
],
},
],
],
}),
},
{
path: './docs/api-doc.mdx',
content: `---
title: API Documentation
description: Generated API docs
deprecated: true
---

# API Documentation`,
},
{
path: './docs/api-doc-2.mdx',
content: `---
title: API Documentation 2
description: Generated API docs 2
deprecated: true
---

# API Documentation 2`,
},
])

await build(
await createConfig({
...baseConfig,
basePath: tempDir,
validSdks: ['react'],
}),
)

expect(JSON.parse(await readFile('dist/manifest.json'))).toEqual({
navigation: [
[
{
title: 'API',
deprecated: true,
items: [
[
{
title: 'API Doc',
href: '/docs/api-doc',
deprecated: true,
},
{
title: 'API Doc 2',
href: '/docs/api-doc-2',
deprecated: true,
},
],
],
},
],
],
})
})
})
20 changes: 16 additions & 4 deletions scripts/build-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ async function main() {
wrapDefault: true,
collapseDefault: false,
hideTitleDefault: false,
deprecatedDefault: false,
},
flags: {
watch: args.includes('--watch'),
Expand Down Expand Up @@ -261,7 +262,7 @@ export async function build(config: BuildConfig, store: Store = createBlankStore

// Goes through and grabs the sdk scoping out of the manifest
const sdkScopedManifestFirstPass = await traverseTree(
{ items: userManifest, sdk: undefined as undefined | SDK[] },
{ items: userManifest, sdk: undefined as undefined | SDK[], deprecated: undefined as undefined | true },
async (item, tree) => {
if (!item.href?.startsWith(config.baseDocsLink)) {
return {
Expand Down Expand Up @@ -305,6 +306,7 @@ export async function build(config: BuildConfig, store: Store = createBlankStore
return {
...item,
sdk,
deprecated: doc.frontmatter.deprecated,
}
},
async ({ items, ...details }, tree) => {
Expand Down Expand Up @@ -350,7 +352,11 @@ export async function build(config: BuildConfig, store: Store = createBlankStore
)

const sdkScopedManifest = await traverseTreeItemsFirst(
{ items: sdkScopedManifestFirstPass, sdk: undefined as undefined | SDK[] },
{
items: sdkScopedManifestFirstPass,
sdk: undefined as undefined | SDK[],
deprecated: undefined as undefined | true,
},
async (item, tree) => item,
async ({ items, ...details }, tree) => {
// This takes all the children items, grabs the sdks out of them, and combines that in to a list
Expand All @@ -363,6 +369,8 @@ export async function build(config: BuildConfig, store: Store = createBlankStore
return uniqueSDKs
})()

const deprecated = items?.every((item) => item.every((item) => item.deprecated))

// This is the sdk of the group
const groupSDK = details.sdk

Expand All @@ -371,19 +379,20 @@ export async function build(config: BuildConfig, store: Store = createBlankStore

// If there are no children items, then we either use the group we are looking at sdks if its defined, or its parent group
if (groupsItemsCombinedSDKs.length === 0) {
return { ...details, sdk: groupSDK ?? parentSDK, items } as ManifestGroup
return { ...details, sdk: groupSDK ?? parentSDK, items, deprecated } as ManifestGroup
}

// If all the children items have the same sdk as the group, then we don't need to set the sdk on the group
if (groupsItemsCombinedSDKs.length === config.validSdks.length) {
return { ...details, sdk: undefined, items } as ManifestGroup
return { ...details, sdk: undefined, items, deprecated } as ManifestGroup
}

if (groupSDK !== undefined && groupSDK.length > 0) {
return {
...details,
sdk: groupSDK,
items,
deprecated,
} as ManifestGroup
}

Expand All @@ -394,6 +403,7 @@ export async function build(config: BuildConfig, store: Store = createBlankStore
// If there are children items, then we combine the sdks of the group and the children items sdks
sdk: combinedSDKs,
items,
deprecated,
} as ManifestGroup
},
(item, error) => {
Expand Down Expand Up @@ -505,6 +515,7 @@ export async function build(config: BuildConfig, store: Store = createBlankStore
icon: item.icon,
target: item.target,
sdk: item.sdk,
deprecated: item.deprecated === config.manifestOptions.deprecatedDefault ? undefined : item.deprecated,
}),
// @ts-expect-error - This traverseTree function might just be the death of me
async (group) => ({
Expand All @@ -516,6 +527,7 @@ export async function build(config: BuildConfig, store: Store = createBlankStore
hideTitle: group.hideTitle === config.manifestOptions.hideTitleDefault ? undefined : group.hideTitle,
sdk: group.sdk,
items: group.items,
deprecated: group.deprecated === config.manifestOptions.deprecatedDefault ? undefined : group.deprecated,
}),
),
}),
Expand Down
2 changes: 2 additions & 0 deletions scripts/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BuildConfigOptions = {
wrapDefault: boolean
collapseDefault: boolean
hideTitleDefault: boolean
deprecatedDefault: boolean
}
redirects?: {
static: {
Expand Down Expand Up @@ -97,6 +98,7 @@ export async function createConfig(config: BuildConfigOptions) {
wrapDefault: true,
collapseDefault: false,
hideTitleDefault: false,
deprecatedDefault: false,
},

redirects: config.redirects
Expand Down
4 changes: 4 additions & 0 deletions scripts/lib/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type ManifestItem = {
icon?: Icon
target?: '_blank'
sdk?: SDK[]
deprecated?: boolean
}

export type ManifestGroup = {
Expand All @@ -51,6 +52,7 @@ export type ManifestGroup = {
icon?: Icon
hideTitle?: boolean
sdk?: SDK[]
deprecated?: boolean
}

type Manifest = (ManifestItem | ManifestGroup)[][]
Expand All @@ -66,6 +68,7 @@ const createManifestSchema = (config: BuildConfig) => {
icon: icon.optional(),
target: z.enum(['_blank']).optional(),
sdk: z.array(sdk).optional(),
deprecated: z.boolean().default(config.manifestOptions.deprecatedDefault),
})
.strict()

Expand All @@ -79,6 +82,7 @@ const createManifestSchema = (config: BuildConfig) => {
icon: icon.optional(),
hideTitle: z.boolean().default(config.manifestOptions.hideTitleDefault),
sdk: z.array(sdk).optional(),
deprecated: z.boolean().default(config.manifestOptions.deprecatedDefault),
})
.strict()

Expand Down
4 changes: 3 additions & 1 deletion scripts/lib/plugins/extractFrontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Frontmatter = {
title: string
description?: string
sdk?: SDK[]
deprecated?: boolean
}

export const extractFrontmatter =
Expand All @@ -33,7 +34,7 @@ export const extractFrontmatter =
if (!('value' in node)) return
if (typeof node.value !== 'string') return

const frontmatterYaml: Record<'title' | 'description' | 'sdk', string | undefined> = yaml.parse(node.value)
const frontmatterYaml = yaml.parse(node.value)

const frontmatterSDKs = frontmatterYaml.sdk?.split(', ')

Expand Down Expand Up @@ -64,6 +65,7 @@ export const extractFrontmatter =
title: frontmatterYaml.title,
description: frontmatterYaml.description,
sdk: frontmatterSDKs,
deprecated: frontmatterYaml.deprecated === true,
}
},
)
Expand Down