|
| 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 | + "text/template" |
| 25 | + |
| 26 | + "github.com/Masterminds/sprig" |
| 27 | + "github.com/elastic/crd-ref-docs/config" |
| 28 | + "github.com/elastic/crd-ref-docs/templates" |
| 29 | + "github.com/elastic/crd-ref-docs/types" |
| 30 | +) |
| 31 | + |
| 32 | +type MarkdownXRenderer struct { |
| 33 | + *MarkdownRenderer |
| 34 | +} |
| 35 | + |
| 36 | +func NewMarkdownXRenderer(conf *config.Config) (*MarkdownXRenderer, error) { |
| 37 | + markdownRenderer, err := NewMarkdownRenderer(conf) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + return &MarkdownXRenderer{markdownRenderer}, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (m *MarkdownXRenderer) ToFuncMap() template.FuncMap { |
| 45 | + fm := m.MarkdownRenderer.ToFuncMap() |
| 46 | + fm["RenderLocalLink"] = m.RenderLocalLink |
| 47 | + fm["RenderGVLink"] = m.RenderGVLink |
| 48 | + fm["RenderTypeLink"] = m.RenderTypeLink |
| 49 | + fm["RenderFieldDoc"] = m.RenderFieldDoc |
| 50 | + return fm |
| 51 | +} |
| 52 | + |
| 53 | +func (m *MarkdownXRenderer) Render(gvd []types.GroupVersionDetails) error { |
| 54 | + funcMap := combinedFuncMap(funcMap{prefix: "markdown", funcs: m.ToFuncMap()}, funcMap{funcs: sprig.TxtFuncMap()}) |
| 55 | + |
| 56 | + var tpls fs.FS |
| 57 | + if m.conf.TemplatesDir != "" { |
| 58 | + tpls = os.DirFS(m.conf.TemplatesDir) |
| 59 | + } else { |
| 60 | + sub, err := fs.Sub(templates.Root, "markdown-x") |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + tpls = sub |
| 65 | + } |
| 66 | + |
| 67 | + tmpl, err := loadTemplate(tpls, funcMap) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + return renderTemplate(tmpl, m.conf, "mdx", gvd) |
| 73 | +} |
| 74 | + |
| 75 | +func (m *MarkdownXRenderer) RenderTypeLink(t *types.Type) string { |
| 76 | + text := m.SimplifiedTypeName(t) |
| 77 | + |
| 78 | + link, local := m.LinkForType(t) |
| 79 | + if link == "" { |
| 80 | + return text |
| 81 | + } |
| 82 | + |
| 83 | + if local { |
| 84 | + return m.RenderLocalLink(text) |
| 85 | + } else { |
| 86 | + return m.RenderExternalLink(link, text) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (m *MarkdownXRenderer) RenderLocalLink(text string) string { |
| 91 | + anchor := strings.ToLower( |
| 92 | + strings.NewReplacer( |
| 93 | + " ", "-", |
| 94 | + ".", "", |
| 95 | + "/", "", |
| 96 | + "(", "", |
| 97 | + ")", "", |
| 98 | + ).Replace(text), |
| 99 | + ) |
| 100 | + |
| 101 | + label := strings.NewReplacer("{", "\\{").Replace(text) |
| 102 | + |
| 103 | + return fmt.Sprintf("[%s](#%s)", label, anchor) |
| 104 | +} |
| 105 | + |
| 106 | +func (m *MarkdownXRenderer) RenderGVLink(gv types.GroupVersionDetails) string { |
| 107 | + return m.RenderLocalLink(gv.GroupVersionString()) |
| 108 | +} |
| 109 | + |
| 110 | +func (m *MarkdownXRenderer) RenderFieldDoc(text string) string { |
| 111 | + out := text |
| 112 | + |
| 113 | + // escape inlined markup |
| 114 | + out = strings.ReplaceAll(out, "<", "<") |
| 115 | + out = strings.ReplaceAll(out, ">", ">") |
| 116 | + |
| 117 | + // Pass to the inner renderer for further processing |
| 118 | + return m.MarkdownRenderer.RenderFieldDoc(out) |
| 119 | +} |
0 commit comments