Skip to content

Commit 81a3ee4

Browse files
committed
fix: api description
1 parent 5bbe77c commit 81a3ee4

4 files changed

Lines changed: 46 additions & 38 deletions

File tree

pkg/cmd/generate/clients/clients.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func getAPIData(
158158
return nil, err
159159
}
160160

161-
short, long := splitDescription(op.Description)
161+
short, long := utils.SplitDescription(op.Description)
162162

163163
data := OperationData{
164164
ACL: utils.AclToString(acl),
@@ -223,36 +223,6 @@ func writeAPIData(data []OperationData, template *template.Template) error {
223223
return nil
224224
}
225225

226-
func splitDescription(p string) (string, string) {
227-
p = strings.TrimSpace(p)
228-
229-
// Split by empty line
230-
parts := strings.SplitN(p, "\n\n", 2)
231-
if len(parts) > 1 && strings.TrimSpace(parts[0]) != "" {
232-
short := strings.TrimSpace(parts[0])
233-
long := strings.TrimSpace(parts[1])
234-
235-
// No extra newline characters in between
236-
short = strings.ReplaceAll(short, "\n", "")
237-
238-
return short, long
239-
}
240-
241-
// No empty line: find first period
242-
if idx := strings.Index(p, "."); idx != -1 {
243-
short := strings.TrimSpace(p[:idx+1])
244-
long := strings.TrimSpace(p[idx+1:])
245-
246-
// No extra newline characters in between
247-
short = strings.ReplaceAll(short, "\n", "")
248-
249-
return short, long
250-
}
251-
252-
// No period: entire paragraph is the shortDescription
253-
return p, ""
254-
}
255-
256226
func getCodeSamples(op *v3.Operation) []CodeSample {
257227
node, ok := op.Extensions.Get("x-codeSamples")
258228
// Operations can be without code samples

pkg/cmd/generate/openapi/openapi.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ type ExternalDocs struct {
3131

3232
// OverviewData holds relevant data for the entire spec.
3333
type OverviewData struct {
34-
OutputFilename string
35-
OutputPath string
36-
Title string
34+
Description string
35+
OutputFilename string
36+
OutputPath string
37+
ShortDescription string
38+
Title string
3739
}
3840

3941
// OperationData holds data relevant to a single API operation stub file.
@@ -134,10 +136,14 @@ func getAPIOverviewData(
134136
doc *libopenapi.DocumentModel[v3.Document],
135137
opts *Options,
136138
) (OverviewData, error) {
139+
short, long := utils.SplitDescription(doc.Model.Info.Description)
140+
137141
result := OverviewData{
138-
OutputFilename: fmt.Sprintf("%s.mdx", opts.APIName),
139-
OutputPath: opts.OutputDirectory,
140-
Title: doc.Model.Info.Title,
142+
OutputFilename: fmt.Sprintf("%s.mdx", opts.APIName),
143+
OutputPath: opts.OutputDirectory,
144+
Title: doc.Model.Info.Title,
145+
ShortDescription: short,
146+
Description: long,
141147
}
142148

143149
return result, nil
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: {{ .Title }}
33
sidebarTitle: Overview
4+
description: {{ .ShortDescription }}
45
---
56

6-
Placeholder.
7+
{{ .Description }}

pkg/cmd/generate/utils/utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,34 @@ func GetLanguageName(lang string) string {
122122

123123
return Capitalize(lang)
124124
}
125+
126+
// SplitDescription splits a description into the first sentence and the rest.
127+
func SplitDescription(p string) (string, string) {
128+
p = strings.TrimSpace(p)
129+
130+
// Split by empty line
131+
parts := strings.SplitN(p, "\n\n", 2)
132+
if len(parts) > 1 && strings.TrimSpace(parts[0]) != "" {
133+
short := strings.TrimSpace(parts[0])
134+
long := strings.TrimSpace(parts[1])
135+
136+
// No extra newline characters in between
137+
short = strings.ReplaceAll(short, "\n", "")
138+
139+
return short, long
140+
}
141+
142+
// No empty line: find first period
143+
if idx := strings.Index(p, "."); idx != -1 {
144+
short := strings.TrimSpace(p[:idx+1])
145+
long := strings.TrimSpace(p[idx+1:])
146+
147+
// No extra newline characters in between
148+
short = strings.ReplaceAll(short, "\n", "")
149+
150+
return short, long
151+
}
152+
153+
// No period: entire paragraph is the shortDescription
154+
return p, ""
155+
}

0 commit comments

Comments
 (0)