Skip to content

Commit 4b10fd2

Browse files
committed
add markdown rendering
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
1 parent 9e60517 commit 4b10fd2

7 files changed

Lines changed: 48 additions & 26 deletions

File tree

example-config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
"typeDisplayNamePrefixOverrides": {
2424
"k8s.io/api/": "Kubernetes ",
2525
"k8s.io/apimachinery/pkg/apis/": "Kubernetes "
26-
}
26+
},
27+
"markdownDisabled": false
2728
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module refdocs
22

33
require (
44
github.com/pkg/errors v0.8.0
5+
github.com/pmezard/go-difflib v1.0.0 // indirect
6+
github.com/russross/blackfriday/v2 v2.0.1
7+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
58
github.com/spf13/pflag v1.0.3 // indirect
69
golang.org/x/tools v0.0.0-20181221235234-d00ac6d27372 // indirect
710
k8s.io/gengo v0.0.0-20181113154421-fd15ee9cc2f7

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
22
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
6+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
7+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
8+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
39
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
410
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
511
golang.org/x/tools v0.0.0-20181221235234-d00ac6d27372 h1:zWPUEY/PjVHT+zO3L8OfkjrtIjf55joTxn/RQP/AjOI=

main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"unicode"
2222

2323
"github.com/pkg/errors"
24+
"github.com/russross/blackfriday/v2"
2425
"k8s.io/gengo/parser"
2526
"k8s.io/gengo/types"
2627
"k8s.io/klog"
@@ -52,6 +53,9 @@ type generatorConfig struct {
5253
// TypeDisplayNamePrefixOverrides is a mapping of how to override displayed
5354
// name for types with certain prefixes with what value.
5455
TypeDisplayNamePrefixOverrides map[string]string `json:"typeDisplayNamePrefixOverrides"`
56+
57+
// MarkdownDisabled controls markdown rendering for comment lines.
58+
MarkdownDisabled bool `json:"markdownDisabled"`
5559
}
5660

5761
type externalPackage struct {
@@ -252,15 +256,23 @@ func isLocalType(t *types.Type) bool {
252256
return false
253257
}
254258

255-
func showComments(s []string) string {
259+
func renderComments(s []string, markdown bool) string {
256260
s = filterCommentTags(s)
257-
return strings.Join(s, "\n")
261+
doc := strings.Join(s, "\n")
262+
263+
if markdown {
264+
// TODO(ahmetb): when a comment includes stuff like "http://<service>"
265+
// we treat this as a HTML tag with markdown renderer below. solve this.
266+
return string(blackfriday.Run([]byte(doc)))
267+
}
268+
return nl2br(doc)
258269
}
259270

271+
func safe(s string) template.HTML { return template.HTML(s) }
272+
260273
func nl2br(s string) string {
261274
return strings.Replace(s, "\n\n", string(template.HTML("<br/><br/>")), -1)
262275
}
263-
func safe(s string) template.HTML { return template.HTML(s) }
264276

265277
func hiddenMember(m types.Member, c generatorConfig) bool {
266278
for _, v := range c.HiddenMemberFields {
@@ -484,8 +496,7 @@ func render(w io.Writer, pkgs []*types.Package, config generatorConfig) error {
484496
"typeIdentifier": func(t *types.Type) string { return typeIdentifier(t, config) },
485497
"typeDisplayName": func(t *types.Type) string { return typeDisplayName(t, config) },
486498
"visibleTypes": func(t []*types.Type) []*types.Type { return visibleTypes(t, config) },
487-
"showComments": showComments,
488-
"nl2br": nl2br,
499+
"renderComments": func(s []string) string { return renderComments(s, !config.MarkdownDisabled) },
489500
"packageDisplayName": packageDisplayName,
490501
"apiGroup": func(t *types.Type) string { return apiVersions[t.Name.Package] },
491502
"linkForType": func(t *types.Type) string {

template/members.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<em>(Optional)</em>
2727
{{ end }}
2828

29-
{{ safe (nl2br (showComments .CommentLines)) }}
29+
{{ safe (renderComments .CommentLines) }}
3030

3131
{{ if and (eq (.Type.Name.Name) "ObjectMeta") }}
3232
Refer to the Kubernetes API documentation for the fields of the

template/pkg.tpl

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ table, td, th {
1414

1515
{{ with .DocComments }}
1616
<p>
17-
{{ showComments . }}
17+
{{ safe (renderComments .) }}
1818
</p>
1919
{{ end }}
2020

@@ -30,23 +30,6 @@ table, td, th {
3030
</ul>
3131

3232
{{ range (visibleTypes (sortedTypes .Types))}}
33-
<h3 id="{{ .Name.Name }}">
34-
{{- .Name.Name }}
35-
{{ if eq .Kind "Alias" }}(<code>{{.Underlying}}</code> alias)</p>{{ end -}}
36-
</h3>
37-
{{ with (typeReferences .) }}
38-
<p>
39-
(<em>Appears on:</em>
40-
{{- $prev := "" -}}
41-
{{- range . -}}
42-
{{- if $prev -}}, {{ end -}}
43-
{{ $prev = . }}
44-
<a href="#{{ typeIdentifier . }}">{{ typeDisplayName . }}</a>
45-
{{- end -}}
46-
)
47-
</p>
48-
{{ end }}
49-
5033
{{ template "type" . }}
5134
{{ end }}
5235
<hr/>

template/type.tpl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
{{ define "type" }}
22

3+
<h3 id="{{ .Name.Name }}">
4+
{{- .Name.Name }}
5+
{{ if eq .Kind "Alias" }}(<code>{{.Underlying}}</code> alias)</p>{{ end -}}
6+
</h3>
7+
{{ with (typeReferences .) }}
8+
<p>
9+
(<em>Appears on:</em>
10+
{{- $prev := "" -}}
11+
{{- range . -}}
12+
{{- if $prev -}}, {{ end -}}
13+
{{ $prev = . }}
14+
<a href="#{{ typeIdentifier . }}">{{ typeDisplayName . }}</a>
15+
{{- end -}}
16+
)
17+
</p>
18+
{{ end }}
19+
20+
321
<p>
4-
{{ safe (nl2br (showComments .CommentLines)) }}
22+
{{ safe (renderComments .CommentLines) }}
523
</p>
624

725
{{ if .Members }}

0 commit comments

Comments
 (0)