Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle fields' multi-line comments #53

Merged
merged 2 commits into from
Oct 13, 2023
Merged
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
16 changes: 13 additions & 3 deletions renderer/asciidoctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,17 @@ func (adr *AsciidoctorRenderer) RenderAnchorID(id string) string {
}

func (adr *AsciidoctorRenderer) RenderFieldDoc(text string) string {
// escape the pipe character, which has special meaning for asciidoc as a way to format tables,
// so that including | in a comment does not result in wonky tables
return strings.ReplaceAll(text, "|", "\\|")
// Escape the pipe character, which has special meaning for asciidoc as a way to format tables,
// so that including | in a comment does not result in wonky tables.
out := strings.ReplaceAll(text, "|", "\\|")

// Trim any leading and trailing whitespace from each line.
lines := strings.Split(out, "\n")
for i := range lines {
lines[i] = strings.TrimSpace(lines[i])
}

// Replace newlines with hard line breaks so that newlines are rendered as expected.
// See: https://docs.asciidoctor.org/asciidoc/latest/blocks/hard-line-breaks
return strings.Join(lines, " +\n\n")
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions renderer/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (m *MarkdownRenderer) ToFuncMap() template.FuncMap {
"SafeID": m.SafeID,
"ShouldRenderType": m.ShouldRenderType,
"TypeID": m.TypeID,
"RenderFieldDoc": m.RenderFieldDoc,
}
}

Expand Down Expand Up @@ -140,3 +141,12 @@ func (m *MarkdownRenderer) RenderExternalLink(link, text string) string {
func (m *MarkdownRenderer) RenderGVLink(gv types.GroupVersionDetails) string {
return m.RenderLocalLink(gv.GroupVersionString())
}

func (m *MarkdownRenderer) RenderFieldDoc(text string) string {
// Escape the pipe character, which has special meaning for Markdown as a way to format tables
// so that including | in a comment does not result in wonky tables.
out := strings.ReplaceAll(text, "|", "\\|")

// Replace newlines with 2 line breaks so that they don't break the Markdown table formatting.
return strings.ReplaceAll(out, "\n", "<br /><br />")
}
2 changes: 1 addition & 1 deletion templates/markdown/type_members.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
{{- if eq $field.Name "metadata" -}}
Refer to Kubernetes API documentation for fields of `metadata`.
{{- else -}}
{{ $field.Doc }}
{{ markdownRenderFieldDoc $field.Doc }}
{{- end -}}
{{- end -}}
4 changes: 3 additions & 1 deletion test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ type GuestbookEntry struct {
Name string `json:"name,omitempty"`
// Time of entry
Time metav1.Time `json:"time,omitempty"`
// Comment by guest
// Comment by guest. This can be a multi-line comment.
//
// Just like this one.
Comment string `json:"comment,omitempty"`
// Rating provided by the guest
Rating Rating `json:"rating,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ GuestbookEntry defines an entry in a guest book.
| Field | Description
| *`name`* __string__ | Name of the guest (pipe \| should be escaped)
| *`time`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#time-v1-meta[$$Time$$]__ | Time of entry
| *`comment`* __string__ | Comment by guest
| *`comment`* __string__ | Comment by guest. This can be a multi-line comment. +

Just like this one.
| *`rating`* __xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-rating[$$Rating$$]__ | Rating provided by the guest
|===

Expand Down
4 changes: 2 additions & 2 deletions test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ _Appears in:_

| Field | Description |
| --- | --- |
| `name` _string_ | Name of the guest (pipe | should be escaped) |
| `name` _string_ | Name of the guest (pipe \| should be escaped) |
| `time` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#time-v1-meta)_ | Time of entry |
| `comment` _string_ | Comment by guest |
| `comment` _string_ | Comment by guest. This can be a multi-line comment. <br /><br /> Just like this one. |
| `rating` _[Rating](#rating)_ | Rating provided by the guest |


Expand Down