|
1 | 1 | package frontend |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "html/template" |
| 4 | + "bufio" |
5 | 5 | "net/http" |
6 | 6 | "os" |
| 7 | + "strings" |
7 | 8 |
|
8 | 9 | "github.com/gin-gonic/gin" |
9 | | - "github.com/microcosm-cc/bluemonday" |
10 | | - "github.com/russross/blackfriday" |
11 | 10 | ) |
12 | 11 |
|
| 12 | +type EndpointDoc struct { |
| 13 | + Name string |
| 14 | + Method string // GET, POST, DELETE |
| 15 | + MethodClass string // get, post, delete — for CSS |
| 16 | + URL string |
| 17 | + Auth string |
| 18 | + Description string |
| 19 | +} |
| 20 | + |
| 21 | +type EndpointGroup struct { |
| 22 | + Title string |
| 23 | + IsData bool // true for "Data Endpoints" — controls blue accent |
| 24 | + Endpoints []EndpointDoc |
| 25 | +} |
| 26 | + |
| 27 | +// parseEndpointGroups reads README.md and extracts endpoint sections into structured data. |
| 28 | +func parseEndpointGroups(path string) ([]EndpointGroup, error) { |
| 29 | + f, err := os.Open(path) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + defer f.Close() |
| 34 | + |
| 35 | + var groups []EndpointGroup |
| 36 | + var currentGroup *EndpointGroup |
| 37 | + var currentEndpoint *EndpointDoc |
| 38 | + |
| 39 | + flushEndpoint := func() { |
| 40 | + if currentEndpoint != nil && currentGroup != nil { |
| 41 | + currentGroup.Endpoints = append(currentGroup.Endpoints, *currentEndpoint) |
| 42 | + currentEndpoint = nil |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + scanner := bufio.NewScanner(f) |
| 47 | + for scanner.Scan() { |
| 48 | + line := scanner.Text() |
| 49 | + |
| 50 | + switch { |
| 51 | + case strings.HasPrefix(line, "## "): |
| 52 | + flushEndpoint() |
| 53 | + if currentGroup != nil { |
| 54 | + groups = append(groups, *currentGroup) |
| 55 | + } |
| 56 | + if strings.Contains(line, "Endpoints") { |
| 57 | + title := strings.TrimPrefix(line, "## ") |
| 58 | + currentGroup = &EndpointGroup{ |
| 59 | + Title: title, |
| 60 | + IsData: strings.Contains(title, "Data"), |
| 61 | + } |
| 62 | + } else { |
| 63 | + currentGroup = nil |
| 64 | + } |
| 65 | + |
| 66 | + case strings.HasPrefix(line, "### ") && currentGroup != nil: |
| 67 | + flushEndpoint() |
| 68 | + name := strings.TrimPrefix(line, "### ") |
| 69 | + currentEndpoint = &EndpointDoc{Name: name} |
| 70 | + |
| 71 | + case strings.HasPrefix(line, "- **URL**:") && currentEndpoint != nil: |
| 72 | + currentEndpoint.URL = extractValue(line) |
| 73 | + |
| 74 | + case strings.HasPrefix(line, "- **Method**:") && currentEndpoint != nil: |
| 75 | + currentEndpoint.Method = extractValue(line) |
| 76 | + currentEndpoint.MethodClass = strings.ToLower(currentEndpoint.Method) |
| 77 | + |
| 78 | + case strings.HasPrefix(line, "- **Auth**:") && currentEndpoint != nil: |
| 79 | + currentEndpoint.Auth = extractValue(line) |
| 80 | + |
| 81 | + case strings.HasPrefix(line, "- **Description**:") && currentEndpoint != nil: |
| 82 | + currentEndpoint.Description = extractValue(line) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + flushEndpoint() |
| 87 | + if currentGroup != nil { |
| 88 | + groups = append(groups, *currentGroup) |
| 89 | + } |
| 90 | + |
| 91 | + return groups, scanner.Err() |
| 92 | +} |
| 93 | + |
| 94 | +// extractValue pulls the value after the first colon on a markdown list line, |
| 95 | +// stripping surrounding backticks and whitespace. |
| 96 | +func extractValue(line string) string { |
| 97 | + idx := strings.Index(line, ":") |
| 98 | + if idx < 0 { |
| 99 | + return "" |
| 100 | + } |
| 101 | + val := strings.TrimSpace(line[idx+1:]) |
| 102 | + val = strings.Trim(val, "`") |
| 103 | + return strings.TrimSpace(val) |
| 104 | +} |
| 105 | + |
13 | 106 | func DocumentationPageHandler(c *gin.Context) { |
14 | | - mdContent, err := os.ReadFile("README.md") |
| 107 | + groups, err := parseEndpointGroups("README.md") |
15 | 108 | if err != nil { |
16 | 109 | c.String(http.StatusInternalServerError, "Failed to read documentation") |
17 | 110 | return |
18 | 111 | } |
19 | 112 |
|
20 | | - // Convert markdown to HTML |
21 | | - unsafeHTML := blackfriday.MarkdownCommon(mdContent) |
22 | | - |
23 | | - // Sanitize HTML to prevent XSS |
24 | | - policy := bluemonday.UGCPolicy() |
25 | | - safeHTML := policy.SanitizeBytes(unsafeHTML) |
26 | | - |
27 | 113 | c.HTML(http.StatusOK, "documentation.html", gin.H{ |
28 | | - "Content": template.HTML(safeHTML), |
| 114 | + "Groups": groups, |
29 | 115 | }) |
30 | 116 | } |
0 commit comments