-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
404 lines (362 loc) · 12.5 KB
/
main.go
File metadata and controls
404 lines (362 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/russross/blackfriday/v2"
)
type TreeNode struct {
Name string
Path string
Children []*TreeNode
CompletePath string // Stores the full path from the root to this node
}
type TOCItem struct {
Level int
Text string
ID string
}
type PageData struct {
Title string
Navbar *TreeNode
Content template.HTML
TOC []TOCItem
CurrentFile string
}
func main() {
http.HandleFunc("/", serveMarkdown)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.Handle("/content/", http.StripPrefix("/content/", http.FileServer(http.Dir("content"))))
port := ":8080"
log.Printf("Server started on http://localhost%s", port)
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatal(err)
}
}
func serveMarkdown(w http.ResponseWriter, r *http.Request) {
dir := "content"
navbarTree := &TreeNode{Name: "root", Path: "", Children: []*TreeNode{}}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
ext := filepath.Ext(path)
if ext == ".md" || ext == ".yaml" || ext == ".yml" {
relPath, _ := filepath.Rel(dir, path)
log.Printf("Processing file: %s", relPath)
// Ensure each file node has a full path
parts := strings.Split(filepath.ToSlash(relPath), "/")
current := navbarTree
var fullPath string
for i, part := range parts {
var parentPath string
parentPath = current.CompletePath
fullPath = parentPath
found := false
for _, child := range current.Children {
if child.Name == part {
current = child
current.CompletePath = filepath.Join(current.CompletePath, part)
found = true
break
}
}
if !found {
var nodePath string
if i == len(parts)-1 {
// This is a file, use the relative path from content directory
nodePath = "/" + relPath
} else {
// This is a directory, store the partial path for search purposes
folderPath := strings.Join(parts[:i+1], "/")
nodePath = "/" + folderPath
fullPath = filepath.Join(parentPath, part)
log.Printf("Setting fullPath: %s", fullPath)
}
newNode := &TreeNode{Name: part, Path: nodePath, Children: []*TreeNode{}, CompletePath: fullPath}
current.Children = append(current.Children, newNode)
current = newNode
current.CompletePath = filepath.Join(current.CompletePath, part)
log.Printf("Added node: %s with path: %s", part, nodePath)
}
}
}
return nil
})
if err != nil {
log.Printf("Error walking the directory: %v", err)
http.Error(w, "Error reading content", http.StatusInternalServerError)
return
}
// Get the search query from the request
searchQuery := r.URL.Query().Get("search")
// Filter the document tree if a search query is present
if searchQuery != "" {
filterTree(navbarTree, searchQuery)
}
// Get the requested file path from the URL
requestedPath := r.URL.Path
var content string
var relPath string
var isSwagger bool
if requestedPath == "/" {
// Home page - render README.md from root folder
readmePath := "README.md"
if contentBytes, err := os.ReadFile(readmePath); err == nil {
content = string(contentBytes)
relPath = "README.md"
} else {
content = "# Welcome to DuckDoc\n\nThe README file could not be found."
relPath = "README.md"
}
} else {
// Remove leading slash and construct full path
relPath = strings.TrimPrefix(requestedPath, "/")
contentPath := filepath.Join(dir, relPath)
if contentBytes, err := os.ReadFile(contentPath); err == nil {
content = string(contentBytes)
// Check if this is a Swagger/OpenAPI file
ext := filepath.Ext(relPath)
if ext == ".yaml" || ext == ".yml" {
// Check if it's a Swagger file by looking for 'openapi' or 'swagger' at the start
if strings.HasPrefix(strings.TrimSpace(content), "openapi:") || strings.HasPrefix(strings.TrimSpace(content), "swagger:") {
isSwagger = true
}
}
} else {
// If file not found, show default content
content = "# File not found\n\nThe requested file could not be found."
}
}
// Handle Swagger files differently
if isSwagger {
renderSwagger(w, relPath, navbarTree)
return
}
log.Printf("Content being passed to extractTOC: %s", content)
toc := extractTOC(content)
// Process content to fix image paths
processedContent := processImagePaths(content, relPath)
htmlContent := template.HTML(blackfriday.Run([]byte(processedContent), blackfriday.WithExtensions(blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs)))
tmpl, err := template.New("layout").Funcs(template.FuncMap{
"defineTree": func(node *TreeNode) template.HTML {
log.Printf("defineTree called with node: %+v", node)
var renderTree func(*TreeNode) string
renderTree = func(node *TreeNode) string {
output := ""
if node != nil {
if node.Name == "root" {
log.Printf("Root node has %d children", len(node.Children))
// For root node, render all children directly
for _, child := range node.Children {
output += renderTree(child)
}
} else {
if len(node.Children) > 0 {
output += "<li class=\"folder\" data-full-path=\"" + node.CompletePath + "\"><span class=\"folder-name\">" + node.Name + "</span><ul>"
for _, child := range node.Children {
output += renderTree(child)
}
output += "</ul></li>"
} else {
output += "<li class=\"file\" data-full-path=\"" + node.CompletePath + "\"><a href=\"" + node.Path + "\">" + node.Name + "</a></li>"
}
}
}
return output
}
result := renderTree(node)
log.Printf("Generated tree HTML: %s", result)
return template.HTML(result)
},
}).ParseFiles("templates/layout.html", "templates/sidebar.html")
if err != nil {
log.Printf("Template parsing error: %v", err)
http.Error(w, "Error loading templates", http.StatusInternalServerError)
return
}
page := PageData{
Title: "Markdown Viewer",
Navbar: navbarTree,
Content: htmlContent,
TOC: toc,
CurrentFile: relPath,
}
log.Printf("About to execute template with navbar tree: %+v", navbarTree)
if err := tmpl.Execute(w, page); err != nil {
log.Printf("Template execution error: %v", err)
http.Error(w, "Error rendering page", http.StatusInternalServerError)
}
}
// Function to extract table of contents (headers) from markdown
func extractTOC(content string) []TOCItem {
var toc []TOCItem
lines := strings.Split(content, "\n")
headerRegex := regexp.MustCompile(`^(#{1,6})\s+(.*)`)
for _, line := range lines {
if matches := headerRegex.FindStringSubmatch(line); matches != nil {
headingLevel := len(matches[1])
headingText := matches[2]
headingID := strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(headingText, ".", "-"), " ", "-"), "#", ""))
toc = append(toc, TOCItem{Level: headingLevel, Text: headingText, ID: headingID})
}
}
return toc
}
// Helper function to filter the tree based on the search query
func filterTree(node *TreeNode, query string) bool {
matches := false
log.Println("DEBUG: filterTree called for node:", node.Name, ", with query:", query)
// Check if current node (folder or file) matches the query
nodeMatches := matchesQuery(node.Name, node.CompletePath, query)
log.Println("Checking node:", node.Name, "Matches:", nodeMatches, "Query:", query)
if nodeMatches {
matches = true
}
// Always filter children recursively, even if current node matches
// This allows us to find nested matches and show full context
filteredChildren := []*TreeNode{}
for _, child := range node.Children {
if filterTree(child, query) {
filteredChildren = append(filteredChildren, child)
matches = true
}
}
// If this node matches, we want to show it and its filtered children
// If this node doesn't match but has matching children, we still show it as context
node.Children = filteredChildren
return matches
}
// Helper function to check if a string matches the query
func matchesQuery(name, completePath, query string) bool {
queryLower := strings.ToLower(query)
// Check if the name matches
if strings.Contains(strings.ToLower(name), queryLower) {
log.Println("DEBUG: matchesQuery found match in name:", name)
return true
}
// Check if the complete path matches (for folders and files)
if strings.Contains(strings.ToLower(completePath), queryLower) {
log.Println("DEBUG: matchesQuery found match in completePath:", completePath)
return true
}
return false
}
// processImagePaths processes markdown content to fix relative image paths
func processImagePaths(content, relPath string) string {
// Regular expression to match markdown image syntax: 
imageRegex := regexp.MustCompile(`!\[([^\]]*)\]\(([^)]+)\)`)
// Get the directory of the current file
var baseDir string
if relPath == "README.md" {
// For README.md in root, images should be looked up relative to content directory
baseDir = "content"
} else {
// For other files, get the directory of the current file
baseDir = filepath.Dir(relPath)
if baseDir == "." {
baseDir = ""
}
}
// Process the content and replace image paths
processedContent := imageRegex.ReplaceAllStringFunc(content, func(match string) string {
// Extract the alt text and image path
matches := imageRegex.FindStringSubmatch(match)
if len(matches) != 3 {
return match // Return original if parsing fails
}
altText := matches[1]
imagePath := matches[2]
// Skip if it's already an absolute URL or starts with /
if strings.HasPrefix(imagePath, "http://") || strings.HasPrefix(imagePath, "https://") || strings.HasPrefix(imagePath, "/") {
return match
}
// Handle relative paths
var newPath string
if after, ok := strings.CutPrefix(imagePath, "./"); ok {
// Remove ./ prefix
imagePath = after
if relPath == "README.md" {
// For README.md, images are in the content directory
newPath = "/content/" + imagePath
} else {
// For other files, resolve relative to the file's directory
if baseDir == "" {
newPath = "/content/" + imagePath
} else {
newPath = "/content/" + baseDir + "/" + imagePath
}
}
} else {
// Handle paths without ./ prefix
if relPath == "README.md" {
newPath = "/content/" + imagePath
} else {
if baseDir == "" {
newPath = "/content/" + imagePath
} else {
newPath = "/content/" + baseDir + "/" + imagePath
}
}
}
log.Printf("Converted image path: %s -> %s (relPath: %s, baseDir: %s)", imagePath, newPath, relPath, baseDir)
log.Printf("Debug: Updated image tag: ", altText, newPath)
return ""
})
log.Printf("Debug: Finished processing content for relPath: %s\nProcessed Content:\n%s", relPath, processedContent)
return processedContent
}
// renderSwagger renders a Swagger/OpenAPI specification file
func renderSwagger(w http.ResponseWriter, relPath string, navbarTree *TreeNode) {
tmpl, err := template.New("layout").Funcs(template.FuncMap{
"defineTree": func(node *TreeNode) template.HTML {
var renderTree func(*TreeNode) string
renderTree = func(node *TreeNode) string {
output := ""
if node != nil {
if node.Name == "root" {
// For root node, render all children directly
for _, child := range node.Children {
output += renderTree(child)
}
} else {
if len(node.Children) > 0 {
output += "<li class=\"folder\" data-full-path=\"" + node.CompletePath + "\"><span class=\"folder-name\">" + node.Name + "</span><ul>"
for _, child := range node.Children {
output += renderTree(child)
}
output += "</ul></li>"
} else {
output += "<li class=\"file\" data-full-path=\"" + node.CompletePath + "\"><a href=\"" + node.Path + "\">" + node.Name + "</a></li>"
}
}
}
return output
}
return template.HTML(renderTree(node))
},
}).ParseFiles("templates/layout.html", "templates/sidebar.html", "templates/swagger.html")
if err != nil {
log.Printf("Template parsing error: %v", err)
http.Error(w, "Error loading templates", http.StatusInternalServerError)
return
}
page := PageData{
Title: "API Documentation",
Navbar: navbarTree,
Content: template.HTML(""), // Content will be rendered by Swagger UI
TOC: []TOCItem{},
CurrentFile: relPath,
}
if err := tmpl.Execute(w, page); err != nil {
log.Printf("Template execution error: %v", err)
http.Error(w, "Error rendering page", http.StatusInternalServerError)
}
}