Skip to content

Commit

Permalink
Escape pipe characters in field documentation for asciidoc (elastic#17)
Browse files Browse the repository at this point in the history
Escape the pipe character, which has special meaning for asciidoc as a way to format tables,
so that including | in a field comment does not result in wonky tables.
  • Loading branch information
margocrawf committed Jan 24, 2022
1 parent a642cb2 commit efa87ff
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions renderer/asciidoctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (adr *AsciidoctorRenderer) ToFuncMap() template.FuncMap {
"SafeID": adr.SafeID,
"ShouldRenderType": adr.ShouldRenderType,
"TypeID": adr.TypeID,
"RenderFieldDoc": adr.RenderFieldDoc,
}
}

Expand Down Expand Up @@ -140,3 +141,9 @@ func (adr *AsciidoctorRenderer) RenderGVLink(gv types.GroupVersionDetails) strin
func (adr *AsciidoctorRenderer) RenderAnchorID(id string) string {
return fmt.Sprintf("%s%s", asciidocAnchorPrefix, adr.SafeID(id))
}

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, "|", "\\|")
}
2 changes: 1 addition & 1 deletion templates/asciidoctor/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 }}
{{ asciidocRenderFieldDoc $field.Doc }}
{{- end -}}
{{- end -}}
2 changes: 1 addition & 1 deletion test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type GuestbookSpec struct {

// GuestbookEntry defines an entry in a guest book.
type GuestbookEntry struct {
// Name of the guest
// Name of the guest (pipe | should be escaped)
Name string `json:"name,omitempty"`
// Time of entry
Time metav1.Time `json:"time,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ GuestbookEntry defines an entry in a guest book.
[cols="25a,75a", options="header"]
|===
| Field | Description
| *`name`* __string__ | Name of the guest
| *`name`* __string__ | Name of the guest (pipe \| should be escaped)
| *`time`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#time-v1-meta[$$Time$$]__ | Time of entry
| *`comment`* __string__ | Comment by guest
| *`rating`* __xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-rating[$$Rating$$]__ | Rating provided by the guest
Expand Down
2 changes: 1 addition & 1 deletion test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ _Appears in:_

| Field | Description |
| --- | --- |
| `name` _string_ | Name of the guest |
| `name` _string_ | Name of the guest (pipe | should be escaped) |
| `time` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#time-v1-meta)_ | Time of entry |
| `comment` _string_ | Comment by guest |
| `rating` _[Rating](#rating)_ | Rating provided by the guest |
Expand Down

0 comments on commit efa87ff

Please sign in to comment.