|
| 1 | +package openapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + _ "embed" |
| 6 | + "errors" |
| 7 | + "strings" |
| 8 | + |
| 9 | + // Packages |
| 10 | + tokenizer "github.com/mutablelogic/go-tokenizer" |
| 11 | + ast "github.com/mutablelogic/go-tokenizer/pkg/ast" |
| 12 | + markdown "github.com/mutablelogic/go-tokenizer/pkg/markdown" |
| 13 | +) |
| 14 | + |
| 15 | +/////////////////////////////////////////////////////////////////////////////// |
| 16 | +// TYPES |
| 17 | + |
| 18 | +type MarkdownDoc struct { |
| 19 | + root ast.Node |
| 20 | + sections []Section |
| 21 | +} |
| 22 | + |
| 23 | +type Section struct { |
| 24 | + Level int |
| 25 | + Title string |
| 26 | + Body string |
| 27 | +} |
| 28 | + |
| 29 | +/////////////////////////////////////////////////////////////////////////////// |
| 30 | +// LIFECYCLE |
| 31 | + |
| 32 | +func ParseMarkdown(data []byte) (doc *MarkdownDoc) { |
| 33 | + return &MarkdownDoc{ |
| 34 | + root: markdown.Parse(bytes.NewReader(data), tokenizer.Pos{}), |
| 35 | + sections: parseSections(string(data)), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/////////////////////////////////////////////////////////////////////////////// |
| 40 | +// PUBLIC METHODS |
| 41 | + |
| 42 | +func (doc *MarkdownDoc) Sections() []Section { |
| 43 | + if doc == nil { |
| 44 | + return nil |
| 45 | + } |
| 46 | + return doc.sections |
| 47 | +} |
| 48 | + |
| 49 | +func (doc *MarkdownDoc) Section(level int, title string) Section { |
| 50 | + if doc == nil { |
| 51 | + return Section{} |
| 52 | + } |
| 53 | + title = normalizeTitle(title) |
| 54 | + for _, section := range doc.sections { |
| 55 | + if section.Level == level && strings.EqualFold(section.Title, title) { |
| 56 | + return section |
| 57 | + } |
| 58 | + } |
| 59 | + return Section{} |
| 60 | +} |
| 61 | + |
| 62 | +// FindSection returns the first heading whose text matches title. |
| 63 | +func (doc *MarkdownDoc) FindSection(title string) *markdown.Heading { |
| 64 | + if doc == nil || doc.root == nil { |
| 65 | + return nil |
| 66 | + } |
| 67 | + title = strings.TrimSpace(title) |
| 68 | + if title == "" { |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + var section *markdown.Heading |
| 73 | + _ = ast.Walk(doc.root, func(node ast.Node, depth int) error { |
| 74 | + heading, ok := node.(*markdown.Heading) |
| 75 | + if !ok { |
| 76 | + return nil |
| 77 | + } |
| 78 | + if strings.EqualFold(markdownNodeText(heading), title) { |
| 79 | + section = heading |
| 80 | + return errors.New("found markdown section") |
| 81 | + } |
| 82 | + return nil |
| 83 | + }) |
| 84 | + return section |
| 85 | +} |
| 86 | + |
| 87 | +/////////////////////////////////////////////////////////////////////////////// |
| 88 | +// PRIVATE METHODS |
| 89 | + |
| 90 | +func markdownNodeText(node ast.Node) string { |
| 91 | + if node == nil { |
| 92 | + return "" |
| 93 | + } |
| 94 | + |
| 95 | + var text strings.Builder |
| 96 | + _ = ast.Walk(node, func(node ast.Node, depth int) error { |
| 97 | + switch node := node.(type) { |
| 98 | + case *markdown.Text: |
| 99 | + text.WriteString(node.Value()) |
| 100 | + case *markdown.Code: |
| 101 | + text.WriteString(node.String()) |
| 102 | + } |
| 103 | + return nil |
| 104 | + }) |
| 105 | + |
| 106 | + return strings.TrimSpace(text.String()) |
| 107 | +} |
| 108 | + |
| 109 | +func parseSections(content string) []Section { |
| 110 | + lines := strings.Split(content, "\n") |
| 111 | + sections := make([]Section, 0, len(lines)) |
| 112 | + |
| 113 | + var current *Section |
| 114 | + for _, line := range lines { |
| 115 | + if level, title, ok := parseHeading(line); ok { |
| 116 | + if current != nil { |
| 117 | + current.Body = strings.Trim(current.Body, "\n") |
| 118 | + sections = append(sections, *current) |
| 119 | + } |
| 120 | + current = &Section{Level: level, Title: title} |
| 121 | + continue |
| 122 | + } |
| 123 | + if current == nil { |
| 124 | + current = &Section{} |
| 125 | + } |
| 126 | + current.Body += line + "\n" |
| 127 | + } |
| 128 | + if current != nil { |
| 129 | + current.Body = strings.Trim(current.Body, "\n") |
| 130 | + sections = append(sections, *current) |
| 131 | + } |
| 132 | + |
| 133 | + return sections |
| 134 | +} |
| 135 | + |
| 136 | +func parseHeading(line string) (level int, title string, ok bool) { |
| 137 | + trimmed := strings.TrimLeft(line, " ") |
| 138 | + if len(trimmed) == 0 || trimmed[0] != '#' { |
| 139 | + return 0, "", false |
| 140 | + } |
| 141 | + i := 0 |
| 142 | + for i < len(trimmed) && trimmed[i] == '#' { |
| 143 | + i++ |
| 144 | + } |
| 145 | + if i > 6 { |
| 146 | + return 0, "", false |
| 147 | + } |
| 148 | + if i < len(trimmed) && trimmed[i] != ' ' && trimmed[i] != '\t' { |
| 149 | + return 0, "", false |
| 150 | + } |
| 151 | + title = strings.TrimSpace(trimmed[i:]) |
| 152 | + title = strings.TrimRight(title, "# ") |
| 153 | + title = normalizeTitle(title) |
| 154 | + return i, title, true |
| 155 | +} |
| 156 | + |
| 157 | +func normalizeTitle(title string) string { |
| 158 | + title = strings.TrimSpace(title) |
| 159 | + for _, marker := range []string{"`", "**", "__", "*", "_"} { |
| 160 | + if strings.HasPrefix(title, marker) && strings.HasSuffix(title, marker) && len(title) >= 2*len(marker) { |
| 161 | + title = strings.TrimSpace(title[len(marker) : len(title)-len(marker)]) |
| 162 | + } |
| 163 | + } |
| 164 | + return title |
| 165 | +} |
0 commit comments