-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
285 lines (237 loc) · 6.32 KB
/
Copy pathserver.go
File metadata and controls
285 lines (237 loc) · 6.32 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
package main
import (
"html/template"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"unicode/utf8"
"github.com/whalelogic/marknote/internal/render"
)
type MarkdownFile struct {
Name string
Path string
Category string
}
type SearchResult struct {
File MarkdownFile
Snippet string
}
type PageData struct {
Title string
Content template.HTML
Files []MarkdownFile
CurrentPath string
CategoryCounts map[string]int
SearchQuery string
SearchResults []SearchResult
}
var notesRoot string
var templates *template.Template
func mustGlob(pattern string) []string {
matches, _ := filepath.Glob(pattern)
return matches
}
func init() {
var err error
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
notesRoot = filepath.Join(cwd, "static")
if _, err := os.Stat(notesRoot); os.IsNotExist(err) {
notesRoot = filepath.Join(cwd, "web/static")
}
}
func main() {
templatePath := "templates/*.html"
if _, err := filepath.Glob(templatePath); err != nil || len(mustGlob(templatePath)) == 0 {
templatePath = "web/templates/*.html"
}
templates = template.Must(template.New("").ParseGlob(templatePath))
http.HandleFunc("/", indexHandler)
http.HandleFunc("/view/", viewHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(notesRoot))))
log.Println("Server starting on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
files := getAllMarkdownFiles()
query := strings.TrimSpace(r.URL.Query().Get("q"))
data := PageData{
Title: "Markdown Documentation Platform",
Files: files,
CategoryCounts: getCategoryCounts(files),
SearchQuery: query,
SearchResults: searchMarkdownFiles(files, query),
}
templates.ExecuteTemplate(w, "index.html", data)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/view/")
if path == "" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Prevent directory traversal
cleanedPath := filepath.Clean(path)
if strings.Contains(cleanedPath, "..") {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
fullPath := filepath.Join(notesRoot, cleanedPath)
content, err := os.ReadFile(fullPath)
if err != nil {
http.Error(w, "File not found", http.StatusNotFound)
return
}
htmlContent := render.MarkdownToHTML(content)
files := getAllMarkdownFiles()
data := PageData{
Title: prettifyName(filepath.Base(path)),
Content: template.HTML(htmlContent),
Files: files,
CurrentPath: cleanedPath,
CategoryCounts: getCategoryCounts(files),
SearchQuery: strings.TrimSpace(r.URL.Query().Get("q")),
}
templates.ExecuteTemplate(w, "view.html", data)
}
func getAllMarkdownFiles() []MarkdownFile {
var files []MarkdownFile
filepath.WalkDir(notesRoot, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
// Skip the root notes directory itself
if path == notesRoot {
return nil
}
if d.IsDir() {
// Skip internal folders
name := d.Name()
if name == ".git" || name == "bin" || name == "out" || name == "node_modules" {
return filepath.SkipDir
}
return nil
}
if strings.HasSuffix(d.Name(), ".md") {
relPath, _ := filepath.Rel(notesRoot, path)
category := getCategoryFromPath(relPath)
files = append(files, MarkdownFile{
Name: prettifyName(d.Name()),
Path: relPath,
Category: category,
})
}
return nil
})
sort.Slice(files, func(i, j int) bool {
if files[i].Category != files[j].Category {
return files[i].Category < files[j].Category
}
return files[i].Name < files[j].Name
})
return files
}
func getCategoryFromPath(path string) string {
dir := filepath.Dir(path)
if dir == "." {
return "General"
}
parts := strings.Split(dir, string(filepath.Separator))
return strings.Title(parts[0])
}
func prettifyName(filename string) string {
name := strings.TrimSuffix(filename, ".md")
name = strings.ReplaceAll(name, "-", " ")
name = strings.ReplaceAll(name, "_", " ")
return strings.Title(name)
}
func getCategoryCounts(files []MarkdownFile) map[string]int {
counts := make(map[string]int)
for _, file := range files {
counts[file.Category]++
}
return counts
}
func searchMarkdownFiles(files []MarkdownFile, query string) []SearchResult {
query = strings.TrimSpace(query)
if query == "" {
return nil
}
lowerQuery := strings.ToLower(query)
results := make([]SearchResult, 0)
for _, file := range files {
fullPath := filepath.Join(notesRoot, file.Path)
content, err := os.ReadFile(fullPath)
if err != nil {
continue
}
nameMatch := strings.Contains(strings.ToLower(file.Name), lowerQuery)
pathMatch := strings.Contains(strings.ToLower(file.Path), lowerQuery)
contentText := string(content)
contentMatch := strings.Contains(strings.ToLower(contentText), lowerQuery)
if !nameMatch && !pathMatch && !contentMatch {
continue
}
results = append(results, SearchResult{
File: file,
Snippet: buildSearchSnippet(contentText, lowerQuery),
})
}
return results
}
func buildSearchSnippet(content, lowerQuery string) string {
for _, line := range strings.Split(content, "\n") {
trimmed := strings.TrimSpace(line)
if trimmed == "" {
continue
}
if strings.Contains(strings.ToLower(trimmed), lowerQuery) {
return shortenMatch(trimmed, lowerQuery, 160)
}
}
for _, line := range strings.Split(content, "\n") {
trimmed := strings.TrimSpace(line)
if trimmed != "" {
return shortenMatch(trimmed, "", 160)
}
}
return "No preview available."
}
func shortenMatch(text, lowerQuery string, maxLen int) string {
runes := []rune(text)
if len(runes) <= maxLen {
return text
}
if lowerQuery == "" {
return string(runes[:maxLen-1]) + "…"
}
lowerText := strings.ToLower(text)
matchByteIndex := strings.Index(lowerText, lowerQuery)
if matchByteIndex < 0 {
return string(runes[:maxLen-1]) + "…"
}
matchIndex := utf8.RuneCountInString(lowerText[:matchByteIndex])
start := matchIndex - (maxLen / 3)
if start < 0 {
start = 0
}
end := start + maxLen
if end > len(runes) {
end = len(runes)
start = max(0, end-maxLen)
}
snippet := string(runes[start:end])
if start > 0 {
snippet = "…" + snippet
}
if end < len(runes) {
snippet += "…"
}
return snippet
}