Skip to content

Commit a049b32

Browse files
authored
Merge pull request #1 from kubeshop/mdx
MDX renderer
2 parents 68b7763 + e168820 commit a049b32

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ crd-ref-docs \
3131
--renderer=markdown
3232
```
3333

34+
Additionally, a very basic MDX renderer is provided:
35+
36+
```
37+
crd-ref-docs \
38+
--source-path=$GOPATH/src/github.com/elastic/cloud-on-k8s/pkg/apis \
39+
--config=config.yaml \
40+
--renderer=mdx
41+
```
42+
3443
Default templates are embedded in the binary. You may provide your own templates by specifying the templates directory:
3544

3645
```

renderer/markdown-x.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package renderer
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/elastic/crd-ref-docs/config"
24+
)
25+
26+
type MarkdownXRenderer struct {
27+
*MarkdownRenderer
28+
}
29+
30+
func NewMarkdownXRenderer(conf *config.Config) (*MarkdownXRenderer, error) {
31+
markdownRenderer, err := NewMarkdownRenderer(conf)
32+
if err != nil {
33+
return nil, err
34+
}
35+
return &MarkdownXRenderer{markdownRenderer}, nil
36+
}
37+
38+
func (m *MarkdownXRenderer) RenderLocalLink(text string) string {
39+
anchor := strings.ToLower(
40+
strings.NewReplacer(
41+
" ", "-",
42+
".", "",
43+
"/", "",
44+
"(", "",
45+
")", "",
46+
).Replace(text),
47+
)
48+
49+
label := strings.NewReplacer("{", "\\{").Replace(text)
50+
51+
return fmt.Sprintf("[%s](#%s)", label, anchor)
52+
}
53+
54+
func (m *MarkdownXRenderer) RenderFieldDoc(text string) string {
55+
out := text
56+
57+
// escape inlined markup
58+
out = strings.ReplaceAll(out, "<", "&lt;")
59+
out = strings.ReplaceAll(out, ">", "&gt;")
60+
61+
// Pass to the inner renderer for further processing
62+
return m.MarkdownRenderer.RenderFieldDoc(out)
63+
}

renderer/renderer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func New(conf *config.Config) (Renderer, error) {
4040
return NewAsciidoctorRenderer(conf)
4141
case "markdown":
4242
return NewMarkdownRenderer(conf)
43+
case "mdx":
44+
return NewMarkdownXRenderer(conf)
4345
default:
4446
return nil, fmt.Errorf("unknown renderer: %s", conf.Renderer)
4547
}

0 commit comments

Comments
 (0)