|
| 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 | + "io/fs" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/Masterminds/sprig" |
| 26 | + "github.com/elastic/crd-ref-docs/config" |
| 27 | + "github.com/elastic/crd-ref-docs/templates" |
| 28 | + "github.com/elastic/crd-ref-docs/types" |
| 29 | +) |
| 30 | + |
| 31 | +type MarkdownXRenderer struct { |
| 32 | + *MarkdownRenderer |
| 33 | +} |
| 34 | + |
| 35 | +func NewMarkdownXRenderer(conf *config.Config) (*MarkdownXRenderer, error) { |
| 36 | + markdownRenderer, err := NewMarkdownRenderer(conf) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + return &MarkdownXRenderer{markdownRenderer}, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (m *MarkdownXRenderer) Render(gvd []types.GroupVersionDetails) error { |
| 44 | + funcMap := combinedFuncMap(funcMap{prefix: "markdown", funcs: m.ToFuncMap()}, funcMap{funcs: sprig.TxtFuncMap()}) |
| 45 | + |
| 46 | + var tpls fs.FS |
| 47 | + if m.conf.TemplatesDir != "" { |
| 48 | + tpls = os.DirFS(m.conf.TemplatesDir) |
| 49 | + } else { |
| 50 | + sub, err := fs.Sub(templates.Root, "markdown-x") |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + tpls = sub |
| 55 | + } |
| 56 | + |
| 57 | + tmpl, err := loadTemplate(tpls, funcMap) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + return renderTemplate(tmpl, m.conf, "mdx", gvd) |
| 63 | +} |
| 64 | + |
| 65 | +func (m *MarkdownXRenderer) RenderLocalLink(text string) string { |
| 66 | + anchor := strings.ToLower( |
| 67 | + strings.NewReplacer( |
| 68 | + " ", "-", |
| 69 | + ".", "", |
| 70 | + "/", "", |
| 71 | + "(", "", |
| 72 | + ")", "", |
| 73 | + ).Replace(text), |
| 74 | + ) |
| 75 | + |
| 76 | + label := strings.NewReplacer("{", "\\{").Replace(text) |
| 77 | + |
| 78 | + return fmt.Sprintf("[%s](#%s)", label, anchor) |
| 79 | +} |
| 80 | + |
| 81 | +func (m *MarkdownXRenderer) RenderFieldDoc(text string) string { |
| 82 | + out := text |
| 83 | + |
| 84 | + // escape inlined markup |
| 85 | + out = strings.ReplaceAll(out, "<", "<") |
| 86 | + out = strings.ReplaceAll(out, ">", ">") |
| 87 | + |
| 88 | + // Pass to the inner renderer for further processing |
| 89 | + return m.MarkdownRenderer.RenderFieldDoc(out) |
| 90 | +} |
0 commit comments