Skip to content

Commit 0007194

Browse files
committed
feat(Markdown): display Response Parameters in JSON
1 parent e7a1172 commit 0007194

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/utils/format-single-api-endpoint-as-markdown.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { CustomPathDiffItem } from '../utils/generate-markdown-diff'
1+
import { CustomPathDiffItem } from './generate-markdown-diff'
2+
import { jsonToMarkdown } from './json-to-markdown'
23

34
type FormatSingleApiEndpointAsMarkdown = (
45
endpoint: CustomPathDiffItem
@@ -23,6 +24,14 @@ const symbolByMethod: Record<CustomPathDiffItem['method'], string> = {
2324
export const formatSingleApiEndpointAsMarkdown: FormatSingleApiEndpointAsMarkdown =
2425
endpoint => {
2526
const { url, method, endpointDetailData } = endpoint
27+
const { responses } = endpointDetailData
28+
const successResponse = responses['200'] ?? responses['201']
29+
const successResponseContent = isReferenceObject(successResponse)
30+
? {}
31+
: successResponse?.content?.['application/json'] ??
32+
successResponse?.content?.['text/plain']
33+
34+
const responseMarkdown = jsonToMarkdown(successResponseContent)
2635

2736
const parameterMarkdownArray = Object.entries(
2837
endpointDetailData.parameters ?? {}
@@ -53,6 +62,11 @@ ${parameterMarkdownArray.join('\n')}
5362
`
5463
: ''
5564
}
65+
66+
### Response Parameters
67+
68+
${responseMarkdown}
69+
5670
`
5771

5872
console.log(generatedMarkdown)

src/utils/json-to-markdown.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Utility function to format JSON as Markdown code block
2+
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
export function jsonToMarkdown(obj: any, depth = 0): string {
5+
let md = depth === 0 ? '```markdown\n' : '' // Start code block only at the top level
6+
const indent = ' '.repeat(depth)
7+
8+
for (const key in obj) {
9+
if (
10+
typeof obj[key] === 'object' &&
11+
obj[key] !== null &&
12+
!Array.isArray(obj[key])
13+
) {
14+
md += `${indent} ${key} :\n`
15+
md += jsonToMarkdown(obj[key], depth + 1) // Recursively append sub-objects
16+
} else {
17+
md += `${indent}- ${key}: ${JSON.stringify(obj[key], null, 2)}\n`
18+
}
19+
}
20+
21+
if (depth === 0) {
22+
md += '```\n' // End code block only at the top level
23+
}
24+
return md
25+
}

0 commit comments

Comments
 (0)