-
Notifications
You must be signed in to change notification settings - Fork 68k
Expand file tree
/
Copy pathgraphql-changelog-transformer.ts
More file actions
90 lines (76 loc) · 3.09 KB
/
Copy pathgraphql-changelog-transformer.ts
File metadata and controls
90 lines (76 loc) · 3.09 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
import type { Context, Page } from '@/types'
import type { PageTransformer } from './types'
import type { ChangelogItemT } from '@/graphql/components/types'
import { renderContent } from '@/content-render/index'
import { loadTemplate } from '@/article-api/lib/load-template'
import { fastTextOnly } from '@/content-render/unified/text-only'
import { extractManualContent } from '@/article-api/lib/graphql-helpers'
/**
* Transformer for GraphQL changelog page
* Renders the changelog with schema changes, preview changes, and upcoming changes
*/
export class GraphQLChangelogTransformer implements PageTransformer {
templateName = 'graphql-changelog.template.md'
canTransform(page: Page): boolean {
if (page.autogenerated !== 'graphql') return false
return page.relativePath.includes('graphql/overview/changelog')
}
async transform(page: Page, _pathname: string, context: Context): Promise<string> {
const currentVersion = context.currentVersion!
const { getGraphqlChangelogByYear, getGraphqlChangelogYears } =
await import('@/graphql/lib/index')
// Determine if this is a year-specific page
const yearMatch = page.relativePath.match(/changelog\/(\d{4})\.md$/)
const year = yearMatch ? Number(yearMatch[1]) : null
const years = getGraphqlChangelogYears(currentVersion)
let schema: ChangelogItemT[]
if (year) {
schema = getGraphqlChangelogByYear(currentVersion, year) as ChangelogItemT[]
} else {
// Index page: show only the latest year
const latestYear = years[0]
schema = getGraphqlChangelogByYear(currentVersion, latestYear) as ChangelogItemT[]
}
const intro = page.intro ? await page.renderProp('intro', context, { textOnly: true }) : ''
const manualContent = await extractManualContent(page, context)
// Process changelog items
const changelogItems = schema.map((item) => {
const processChanges = (changes: Array<{ title: string; changes: string[] }>) =>
changes.map((change) => ({
title: change.title,
changes: change.changes.map((html: string) => {
// Remove wrapping <p> tags if present
if (html.startsWith('<p>') && html.endsWith('</p>')) {
return fastTextOnly(html.slice(3, -4))
}
return fastTextOnly(html)
}),
}))
return {
date: item.date,
schemaChanges: processChanges(item.schemaChanges || []),
previewChanges: processChanges(item.previewChanges || []),
upcomingChanges: processChanges(item.upcomingChanges || []),
}
})
// Build year navigation links
const displayYear = year || years[0]
const yearNavItems = years.map((y) => ({
year: y,
isCurrent: y === displayYear,
}))
const templateData: Record<string, unknown> = {
pageTitle: page.title,
pageIntro: intro,
manualContent,
changelogItems,
yearNavItems,
}
const templateContent = loadTemplate(this.templateName)
return await renderContent(templateContent, {
...context,
...templateData,
markdownRequested: true,
})
}
}