-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_markdown.go
More file actions
147 lines (125 loc) · 3.45 KB
/
server_markdown.go
File metadata and controls
147 lines (125 loc) · 3.45 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
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func serverAddMarkdown(markdown *gin.RouterGroup) {
// Create a new markdown cell.
markdown.GET("/new/:unique", func(c *gin.Context) {
unique := c.Param("unique")
if !IsValidNonce(unique, PageNonceSize) {
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid page identifier",
})
return
}
eunique := Nonce(ElementNonceSize)
if err := catalog.AddCell(unique, eunique, "markdown", ""); err != nil {
c.HTML(http.StatusNotFound, "error.tmpl", gin.H{
"root": webroot,
"error": "failed to create markdown cell",
})
return
}
c.Redirect(http.StatusFound, webroot+"markdown/edit/"+unique+"/"+eunique)
})
// View a rendered markdown cell.
markdown.GET("/view/*path", func(c *gin.Context) {
unique, eunique, ok := parseElementPath(c.Param("path"))
if !ok {
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid path",
})
return
}
cell, _ := catalog.GetCell(unique, eunique)
var content string
if cell != nil {
content = cell.Content
}
c.HTML(200, "markdown-view.tmpl", gin.H{
"root": webroot,
"path": "/" + unique + "/" + eunique,
"raw": content,
})
})
// Edit a markdown cell.
markdown.GET("/edit/*path", func(c *gin.Context) {
unique, eunique, ok := parseElementPath(c.Param("path"))
if !ok {
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid path",
})
return
}
cell, _ := catalog.GetCell(unique, eunique)
var content []byte
if cell != nil {
content = []byte(cell.Content)
}
c.HTML(200, "markdown-edit.tmpl", gin.H{
"root": webroot,
"path": "/" + unique + "/" + eunique,
"raw": content,
})
})
// Save markdown cell content.
markdown.POST("/save/*path", func(c *gin.Context) {
unique, eunique, ok := parseElementPath(c.Param("path"))
if !ok {
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid path",
})
return
}
content := c.PostForm("markdown")
catalog.UpdateCellContent(unique, eunique, content)
c.Redirect(http.StatusFound, webroot+"markdown/view/"+unique+"/"+eunique)
})
// Delete a markdown cell.
markdown.GET("/delete/*path", func(c *gin.Context) {
unique, eunique, ok := parseElementPath(c.Param("path"))
if !ok {
c.HTML(http.StatusBadRequest, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid path",
})
return
}
catalog.DeleteCell(unique, eunique)
// Redirect to "deleted" pseudo-page so iframe can detect removal.
c.Redirect(http.StatusFound, webroot+"markdown/deleted")
})
// Pseudo-page for iframe deletion detection.
markdown.GET("/deleted", func(c *gin.Context) {
c.String(200, "deleted")
})
}
/* ─── Helpers ─────────────────────────────────────────────── */
func parseElementPath(path string) (string, string, bool) {
parts := splitPath(path)
if len(parts) != 2 {
return "", "", false
}
unique := parts[0]
eunique := parts[1]
if !IsValidNonce(unique, PageNonceSize) || !IsValidNonce(eunique, ElementNonceSize) {
return "", "", false
}
return unique, eunique, true
}
func splitPath(path string) []string {
raw := strings.Split(path, "/")
var parts []string
for _, p := range raw {
if p != "" {
parts = append(parts, p)
}
}
return parts
}