mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 00:49:02 +00:00
feat(docs): add changelog section to mintlify documentation (#422)
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { join, resolve } from 'node:path'
|
||||
import { tmpdir } from 'node:os'
|
||||
import {
|
||||
formatChangelogDate,
|
||||
generateMdxChangelog,
|
||||
parseChangelogReleases,
|
||||
syncDocsChangelog,
|
||||
} from './generate-docs-changelog'
|
||||
|
||||
describe('generate-docs-changelog', () => {
|
||||
describe('formatChangelogDate', () => {
|
||||
it('formats YYYY-MM-DD into Month D, YYYY', () => {
|
||||
expect(formatChangelogDate('2026-09-19')).toBe('September 19, 2026')
|
||||
expect(formatChangelogDate('2026-01-05')).toBe('January 5, 2026')
|
||||
expect(formatChangelogDate('2025-12-31')).toBe('December 31, 2025')
|
||||
})
|
||||
|
||||
it('returns empty string for undefined or empty date', () => {
|
||||
expect(formatChangelogDate(undefined)).toBe('')
|
||||
expect(formatChangelogDate('')).toBe('')
|
||||
})
|
||||
|
||||
it('returns original string if format is not YYYY-MM-DD', () => {
|
||||
expect(formatChangelogDate('September 2026')).toBe('September 2026')
|
||||
})
|
||||
})
|
||||
|
||||
describe('parseChangelogReleases', () => {
|
||||
const sampleChangelog = `# Changelog
|
||||
|
||||
All notable changes documented here.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- In progress feature
|
||||
|
||||
## [0.4.7] - 2026-09-19
|
||||
|
||||
### Added
|
||||
- Feature 1
|
||||
- Feature 2
|
||||
|
||||
### Fixed
|
||||
- Bug 1
|
||||
|
||||
## [0.4.6] - 2026-09-17
|
||||
|
||||
### Added
|
||||
- Feature 3
|
||||
|
||||
## [0.0.1]
|
||||
|
||||
### Added
|
||||
- Initial release
|
||||
`
|
||||
|
||||
it('parses releases and skips [Unreleased]', () => {
|
||||
const releases = parseChangelogReleases(sampleChangelog)
|
||||
expect(releases).toHaveLength(3)
|
||||
|
||||
expect(releases[0]).toEqual({
|
||||
version: '0.4.7',
|
||||
date: '2026-09-19',
|
||||
content: '### Added\n- Feature 1\n- Feature 2\n\n### Fixed\n- Bug 1',
|
||||
})
|
||||
|
||||
expect(releases[1]).toEqual({
|
||||
version: '0.4.6',
|
||||
date: '2026-09-17',
|
||||
content: '### Added\n- Feature 3',
|
||||
})
|
||||
|
||||
expect(releases[2]).toEqual({
|
||||
version: '0.0.1',
|
||||
date: undefined,
|
||||
content: '### Added\n- Initial release',
|
||||
})
|
||||
})
|
||||
|
||||
it('returns empty array for empty changelog', () => {
|
||||
expect(parseChangelogReleases('')).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('generateMdxChangelog', () => {
|
||||
it('generates valid Mintlify MDX with <Update> tags', () => {
|
||||
const releases = [
|
||||
{
|
||||
version: '0.4.7',
|
||||
date: '2026-09-19',
|
||||
content: '### Added\n- Feature A',
|
||||
},
|
||||
{
|
||||
version: '0.4.6',
|
||||
content: '### Fixed\n- Bug B',
|
||||
},
|
||||
]
|
||||
|
||||
const mdx = generateMdxChangelog(releases)
|
||||
|
||||
expect(mdx).toContain('title: "Changelog"')
|
||||
expect(mdx).toContain(
|
||||
'description: "Recent updates, new features, and improvements in Shumai"',
|
||||
)
|
||||
expect(mdx).toContain('<Update label="v0.4.7" description="September 19, 2026">')
|
||||
expect(mdx).toContain('### Added\n- Feature A')
|
||||
expect(mdx).toContain('<Update label="v0.4.6">')
|
||||
expect(mdx).toContain('### Fixed\n- Bug B')
|
||||
expect(mdx).toContain('</Update>')
|
||||
})
|
||||
})
|
||||
|
||||
describe('syncDocsChangelog', () => {
|
||||
const tempDir = join(tmpdir(), `test-sync-docs-changelog-${Date.now()}`)
|
||||
const changelogPath = join(tempDir, 'CHANGELOG.md')
|
||||
const outputPath = join(tempDir, 'changelog.mdx')
|
||||
|
||||
it('generates MDX and supports check mode', () => {
|
||||
const sample = `## [0.1.0] - 2026-01-01\n\n- First version`
|
||||
// Create temp dir
|
||||
mkdirSync(tempDir, { recursive: true })
|
||||
writeFileSync(changelogPath, sample, 'utf-8')
|
||||
|
||||
// Check before generating should return false
|
||||
expect(syncDocsChangelog({ changelogPath, outputPath, check: true })).toBe(false)
|
||||
|
||||
// Sync should generate output
|
||||
syncDocsChangelog({ changelogPath, outputPath })
|
||||
expect(existsSync(outputPath)).toBe(true)
|
||||
|
||||
const content = readFileSync(outputPath, 'utf-8')
|
||||
expect(content).toContain('<Update label="v0.1.0" description="January 1, 2026">')
|
||||
|
||||
// Check after generating should return true
|
||||
expect(syncDocsChangelog({ changelogPath, outputPath, check: true })).toBe(true)
|
||||
|
||||
// Clean up
|
||||
rmSync(tempDir, { recursive: true, force: true })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Integration with repository CHANGELOG.md', () => {
|
||||
it('successfully parses the actual repository CHANGELOG.md', () => {
|
||||
const changelogPath = resolve(__dirname, '../CHANGELOG.md')
|
||||
const content = readFileSync(changelogPath, 'utf-8')
|
||||
const releases = parseChangelogReleases(content)
|
||||
|
||||
expect(releases.length).toBeGreaterThan(40)
|
||||
expect(releases[0].version).toBe('0.4.7')
|
||||
expect(releases[0].date).toBe('2026-09-19')
|
||||
expect(releases[0].content).toContain('Introduce Keep a Changelog support')
|
||||
|
||||
const mdx = generateMdxChangelog(releases)
|
||||
expect(mdx).toContain('<Update label="v0.4.7" description="September 19, 2026">')
|
||||
expect(mdx).toContain('<Update label="v0.0.1" description="June 12, 2026">')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,200 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
export interface ChangelogRelease {
|
||||
version: string
|
||||
date?: string
|
||||
content: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an ISO date string (YYYY-MM-DD) into a human-readable string (Month D, YYYY).
|
||||
* Returns the original string if parsing fails.
|
||||
*/
|
||||
export function formatChangelogDate(dateStr?: string): string {
|
||||
if (!dateStr) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dateStr.trim())
|
||||
if (!match) {
|
||||
return dateStr.trim()
|
||||
}
|
||||
|
||||
const year = parseInt(match[1], 10)
|
||||
const monthIndex = parseInt(match[2], 10) - 1
|
||||
const day = parseInt(match[3], 10)
|
||||
|
||||
const date = new Date(Date.UTC(year, monthIndex, day))
|
||||
return date.toLocaleDateString('en-US', {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
year: 'numeric',
|
||||
timeZone: 'UTC',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses all released versions from a Keep a Changelog markdown string.
|
||||
* Skips the ## [Unreleased] section.
|
||||
*/
|
||||
export function parseChangelogReleases(changelogContent: string): ChangelogRelease[] {
|
||||
if (!changelogContent) {
|
||||
return []
|
||||
}
|
||||
|
||||
const releases: ChangelogRelease[] = []
|
||||
// Matches "## [0.4.7] - 2026-09-19" or "## [v0.4.7]" or "## [Unreleased]"
|
||||
const headerRegex = /^## \[(?:v)?([^\]]+)\](?:\s*-\s*([^\n\r]+))?$/gm
|
||||
|
||||
const matches: Array<{ version: string; date?: string; index: number; length: number }> = []
|
||||
let match: RegExpExecArray | null
|
||||
|
||||
while ((match = headerRegex.exec(changelogContent)) !== null) {
|
||||
matches.push({
|
||||
version: match[1].trim(),
|
||||
date: match[2]?.trim(),
|
||||
index: match.index,
|
||||
length: match[0].length,
|
||||
})
|
||||
}
|
||||
|
||||
for (let i = 0; i < matches.length; i++) {
|
||||
const current = matches[i]
|
||||
if (current.version.toLowerCase() === 'unreleased') {
|
||||
continue
|
||||
}
|
||||
|
||||
const startIndex = current.index + current.length
|
||||
const endIndex = i + 1 < matches.length ? matches[i + 1].index : changelogContent.length
|
||||
const content = changelogContent.slice(startIndex, endIndex).trim()
|
||||
|
||||
releases.push({
|
||||
version: current.version,
|
||||
date: current.date,
|
||||
content,
|
||||
})
|
||||
}
|
||||
|
||||
return releases
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the MDX content for docs/changelog.mdx using Mintlify's <Update> component.
|
||||
*/
|
||||
export function generateMdxChangelog(releases: ChangelogRelease[]): string {
|
||||
const lines: string[] = [
|
||||
'---',
|
||||
'title: "Changelog"',
|
||||
'description: "Recent updates, new features, and improvements in Shumai"',
|
||||
'---',
|
||||
'',
|
||||
]
|
||||
|
||||
for (const release of releases) {
|
||||
const cleanVersion = release.version.replace(/^v/i, '')
|
||||
const label = `v${cleanVersion}`
|
||||
const formattedDate = formatChangelogDate(release.date)
|
||||
|
||||
const descAttr = formattedDate ? ` description="${formattedDate}"` : ''
|
||||
lines.push(`<Update label="${label}"${descAttr}>`)
|
||||
lines.push('')
|
||||
if (release.content) {
|
||||
lines.push(release.content)
|
||||
lines.push('')
|
||||
}
|
||||
lines.push('</Update>')
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
export interface SyncOptions {
|
||||
changelogPath?: string
|
||||
outputPath?: string
|
||||
check?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes docs/changelog.mdx with CHANGELOG.md.
|
||||
* If check is true, returns whether the output file already matches the generated content.
|
||||
*/
|
||||
export function syncDocsChangelog(options: SyncOptions = {}): boolean {
|
||||
const changelogPath = resolve(options.changelogPath ?? 'CHANGELOG.md')
|
||||
const outputPath = resolve(options.outputPath ?? 'docs/changelog.mdx')
|
||||
|
||||
if (!existsSync(changelogPath)) {
|
||||
throw new Error(`Changelog not found at ${changelogPath}`)
|
||||
}
|
||||
|
||||
const changelogContent = readFileSync(changelogPath, 'utf-8')
|
||||
const releases = parseChangelogReleases(changelogContent)
|
||||
const mdxContent = generateMdxChangelog(releases)
|
||||
|
||||
if (options.check) {
|
||||
if (!existsSync(outputPath)) {
|
||||
return false
|
||||
}
|
||||
const currentMdx = readFileSync(outputPath, 'utf-8')
|
||||
return currentMdx === mdxContent
|
||||
}
|
||||
|
||||
writeFileSync(outputPath, mdxContent, 'utf-8')
|
||||
return true
|
||||
}
|
||||
|
||||
function parseCliArgs(args: string[]): {
|
||||
check: boolean
|
||||
changelogPath?: string
|
||||
outputPath?: string
|
||||
} {
|
||||
let check = false
|
||||
let changelogPath: string | undefined
|
||||
let outputPath: string | undefined
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i]
|
||||
if (arg === '--check') {
|
||||
check = true
|
||||
} else if (arg === '--changelog' || arg === '-c') {
|
||||
changelogPath = args[++i]
|
||||
} else if (arg === '--output' || arg === '-o') {
|
||||
outputPath = args[++i]
|
||||
}
|
||||
}
|
||||
|
||||
return { check, changelogPath, outputPath }
|
||||
}
|
||||
|
||||
export function runCli(argv: string[]): void {
|
||||
const { check, changelogPath, outputPath } = parseCliArgs(argv)
|
||||
|
||||
try {
|
||||
const targetOut = outputPath ?? 'docs/changelog.mdx'
|
||||
if (check) {
|
||||
const isUpToDate = syncDocsChangelog({ changelogPath, outputPath, check: true })
|
||||
if (!isUpToDate) {
|
||||
console.error(
|
||||
`Error: ${targetOut} is out of date with changelog. Run: bun run docs:changelog`,
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(`✓ ${targetOut} is up to date.`)
|
||||
} else {
|
||||
syncDocsChangelog({ changelogPath, outputPath })
|
||||
console.log(`✓ Generated ${targetOut} successfully.`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to sync docs changelog:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
const isDirectlyExecuted =
|
||||
Boolean(import.meta.main) ||
|
||||
(process.argv[1] ? resolve(process.argv[1]) === resolve(import.meta.filename ?? '') : false)
|
||||
|
||||
if (isDirectlyExecuted) {
|
||||
runCli(process.argv.slice(2))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { execSync } from 'node:child_process'
|
||||
import { readFileSync, writeFileSync, readdirSync, existsSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { syncDocsChangelog } from './generate-docs-changelog'
|
||||
|
||||
const releaseTarget = process.argv[2]
|
||||
const bumpTypes = new Set(['major', 'minor', 'patch'])
|
||||
@@ -136,6 +137,13 @@ function updateChangelog(newVersion: string) {
|
||||
const updatedContent = content.replace(unreleasedRegex, replacement)
|
||||
writeFileSync(changelogFile, updatedContent, 'utf-8')
|
||||
console.log(` Updated ${changelogFile} for v${newVersion} (${dateStr})`)
|
||||
|
||||
try {
|
||||
syncDocsChangelog()
|
||||
console.log(' Updated docs/changelog.mdx')
|
||||
} catch (error) {
|
||||
console.warn(' Warning: Failed to update docs/changelog.mdx:', error)
|
||||
}
|
||||
}
|
||||
|
||||
function shellQuote(value: string) {
|
||||
|
||||
Reference in New Issue
Block a user