-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
377 lines (315 loc) · 9.84 KB
/
main.go
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
package main
/**
To use this, make a new markdown file. For convention name the file the same as the slug. The md file should be in /markdown
To see more detailed instructions, see my blog at https://fluxsec.red, or reach out to me
on twitter https://twitter.com/0xfluxsec.
The .md file must then have the following attributes, including the 3 lines ---
those lines separate the tags from the content:
Title: Page title, and title in left sidebar
Slug: slug-of-url
Parent: The name you wish the parent series to be called
Order: number in terms of parent order
Description: Small strap-line description which appears under the title
MetaPropertyTitle: Title for social sharing
MetaDescription: Description ~ 150 - 200 words of the page for SEO.
MetaPropertyDescription: SHORT description for social media sharing.
MetaOgURL: https://www.fluxsec.red/slug-of-url
---
Content goes here
Additional downloads:
* FontAwesome free, into /static/
*/
import (
"errors"
"fmt"
"html/template"
"log"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
type BlogPost struct {
Title string
Slug string
Parent string
Content template.HTML
Description string
Order int
Headers []string // these are the in page h2 tags
MetaDescription string
MetaPropertyTitle string
MetaPropertyDescription string
MetaOgURL string
}
type SidebarData struct {
Categories []Category
}
type Category struct {
Name string
Pages []BlogPost
Order int
}
var BaseURL = "http://localhost:8080"
func main() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// sidebar data
sidebarData, err := loadSidebarData("./markdown")
if err != nil {
log.Fatal(err) // I think this one is ok
}
// register the sidebar template as a partial
r.SetFuncMap(template.FuncMap{
"loadSidebar": func() SidebarData {
return sidebarData
},
"dict": dict,
})
// load in the templates
r.LoadHTMLGlob("templates/*")
// serve static assets
r.Static("/static", "./static")
// load and parse markdown files
posts, err := loadMarkdownPosts("./markdown")
if err != nil {
log.Fatal(err) // i think this fatal is ok
}
// single route for the home page
r.GET("/", func(c *gin.Context) {
indexPath := "./markdown/index.md"
indexContent, err := os.ReadFile(indexPath)
if err != nil {
log.Printf("Error occurred during operation: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}
post, err := parseMarkdownFile(indexContent)
if err != nil {
log.Printf("Error occurred during operation: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}
sidebarLinks := createSidebarLinks(post.Headers)
c.HTML(http.StatusOK, "index.html", gin.H{
"Title": post.Title,
"Content": post.Content,
"SidebarData": sidebarData,
"Headers": post.Headers,
"SidebarLinks": sidebarLinks,
"CurrentSlug": post.Slug,
"MetaDescription": post.MetaDescription,
"MetaPropertyTitle": post.MetaPropertyTitle,
"MetaPropertyDescription": post.MetaPropertyDescription,
"MetaOgURL": post.MetaOgURL,
})
})
// routes for each blog post, based of of Slug following the /
for _, post := range posts {
localPost := post
if localPost.Slug != "" {
sidebarLinks := createSidebarLinks(localPost.Headers)
r.GET("/"+localPost.Slug, func(c *gin.Context) {
c.HTML(http.StatusOK, "layout.html", gin.H{
"Title": localPost.Title,
"Content": localPost.Content,
"SidebarData": sidebarData,
"Headers": localPost.Headers,
"Description": localPost.Description,
"SidebarLinks": sidebarLinks,
"CurrentSlug": localPost.Slug,
"MetaDescription": localPost.MetaDescription,
"MetaPropertyTitle": localPost.MetaPropertyTitle,
"MetaPropertyDescription": localPost.MetaPropertyDescription,
"MetaOgURL": localPost.MetaOgURL,
})
})
} else {
log.Printf("Warning: Post titled '%s' has an empty slug and will not be accessible via a unique URL.\n", localPost.Title)
}
}
r.NoRoute(func(c *gin.Context) {
c.HTML(http.StatusNotFound, "404.html", gin.H{
"Title": "Page Not Found",
})
})
r.Run()
}
func loadMarkdownPosts(dir string) ([]BlogPost, error) {
var posts []BlogPost
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, file := range files {
if strings.HasSuffix(file.Name(), ".md") {
path := dir + "/" + file.Name()
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
post, err := parseMarkdownFile(content)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
}
return posts, nil
}
func parseMarkdownFile(content []byte) (BlogPost, error) {
sections := strings.SplitN(string(content), "---", 2)
if len(sections) < 2 {
return BlogPost{}, errors.New("invalid markdown format")
}
metadata := sections[0]
mdContent := sections[1]
// deal with rogue \r's
metadata = strings.ReplaceAll(metadata, "\r", "")
mdContent = strings.ReplaceAll(mdContent, "\r", "")
title, slug, parent, description, order, metaDescriptionStr,
metaPropertyTitleStr, metaPropertyDescriptionStr,
metaOgURLStr := parseMetadata(metadata)
htmlContent := mdToHTML([]byte(mdContent))
headers := extractHeaders([]byte(mdContent))
return BlogPost{
Title: title,
Slug: slug,
Parent: parent,
Description: description,
Content: template.HTML(htmlContent),
Headers: headers,
Order: order,
MetaDescription: metaDescriptionStr,
MetaPropertyTitle: metaPropertyTitleStr,
MetaPropertyDescription: metaPropertyDescriptionStr,
MetaOgURL: metaOgURLStr,
}, nil
}
func extractHeaders(content []byte) []string {
var headers []string
//match only level 2 markdown headers
re := regexp.MustCompile(`(?m)^##\s+(.*)`)
matches := re.FindAllSubmatch(content, -1)
for _, match := range matches {
// match[1] contains header text without the '##'
headers = append(headers, string(match[1]))
}
return headers
}
func mdToHTML(md []byte) []byte {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
parser := parser.NewWithExtensions(extensions)
opts := html.RendererOptions{
Flags: html.CommonFlags | html.HrefTargetBlank,
}
renderer := html.NewRenderer(opts)
doc := parser.Parse(md)
output := markdown.Render(doc, renderer)
return output
}
func parseMetadata(metadata string) (
title string,
slug string,
parent string,
description string,
order int,
metaDescription string,
metaPropertyTitle string,
metaPropertyDescription string,
metaOgURL string,
) {
re := regexp.MustCompile(`(?m)^(\w+):\s*(.+)`)
matches := re.FindAllStringSubmatch(metadata, -1)
metaDataMap := make(map[string]string)
for _, match := range matches {
if len(match) == 3 {
metaDataMap[match[1]] = match[2]
}
}
title = metaDataMap["Title"]
slug = metaDataMap["Slug"]
parent = metaDataMap["Parent"]
description = metaDataMap["Description"]
orderStr := metaDataMap["Order"]
metaDescriptionStr := metaDataMap["MetaDescription"]
metaPropertyTitleStr := metaDataMap["MetaPropertyTitle"]
metaPropertyDescriptionStr := metaDataMap["MetaPropertyDescription"]
metaOgURLStr := metaDataMap["MetaOgURL"]
orderStr = strings.TrimSpace(orderStr)
order, err := strconv.Atoi(orderStr)
if err != nil {
log.Printf("Error converting order from string: %v", err)
order = 9999
}
return title, slug, parent, description, order, metaDescriptionStr,
metaPropertyTitleStr, metaPropertyDescriptionStr, metaOgURLStr
}
func loadSidebarData(dir string) (SidebarData, error) {
var sidebar SidebarData
categoriesMap := make(map[string]*Category)
posts, err := loadMarkdownPosts(dir)
if err != nil {
return sidebar, err
}
for _, post := range posts {
if post.Parent != "" {
if _, exists := categoriesMap[post.Parent]; !exists {
categoriesMap[post.Parent] = &Category{
Name: post.Parent,
Pages: []BlogPost{post},
Order: post.Order,
}
} else {
categoriesMap[post.Parent].Pages = append(categoriesMap[post.Parent].Pages, post)
}
}
}
// convert map to slice
for _, cat := range categoriesMap {
sidebar.Categories = append(sidebar.Categories, *cat)
}
// sort categories by order
sort.Slice(sidebar.Categories, func(i, j int) bool {
return sidebar.Categories[i].Order < sidebar.Categories[j].Order
})
return sidebar, nil
}
func createSidebarLinks(headers []string) template.HTML {
var linksHTML string
for _, header := range headers {
sanitizedHeader := sanitizeHeaderForID(header)
link := fmt.Sprintf(`<li><a href="#%s">%s</a></li>`, sanitizedHeader, header)
linksHTML += link
}
return template.HTML(linksHTML)
}
func sanitizeHeaderForID(header string) string {
// lowercase
header = strings.ToLower(header)
// replace spaces with hyphens
header = strings.ReplaceAll(header, " ", "-")
// remove any characters that are not alphanumeric or hyphens
header = regexp.MustCompile(`[^a-z0-9\-]`).ReplaceAllString(header, "")
return header
}
func dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}