diff --git a/main.go b/main.go index 5d6be28..c9556ee 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ Options:`) r.GET("/:title", editNote) r.GET("/:title/*option", everythingElse) r.DELETE("/listitem", deleteListItem) + r.DELETE("/deletepage", deletePage) if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" { r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey) } else { diff --git a/routes.go b/routes.go index cad45d2..ffc4b2f 100644 --- a/routes.go +++ b/routes.go @@ -72,9 +72,9 @@ func everythingElse(c *gin.Context) { versionNum = -1 } currentText, versions, _ := getCurrentText(title, versionNum) - renderMarkdown(c, currentText, title, versions) + renderMarkdown(c, currentText, title, versions, "") } else if title == "ls" && option == "/"+RuntimeArgs.AdminKey && len(RuntimeArgs.AdminKey) > 1 { - renderMarkdown(c, listEverything(), "ls", nil) + renderMarkdown(c, listEverything(), "ls", nil, RuntimeArgs.AdminKey) } else if option == "/list" { renderList(c, title) } else if title == "static" { @@ -93,7 +93,7 @@ func serveStaticFile(c *gin.Context, option string) { } } -func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo) { +func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string) { r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]") for _, s := range r.FindAllString(currentText, -1) { currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1) @@ -103,6 +103,9 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [ pClean.AllowElements("img") pClean.AllowAttrs("alt").OnElements("img") pClean.AllowAttrs("src").OnElements("img") + pClean.AllowAttrs("class").OnElements("a") + pClean.AllowAttrs("href").OnElements("a") + pClean.AllowAttrs("id").OnElements("a") pClean.AllowDataURIImages() html := pClean.SanitizeBytes(unsafe) html2 := string(html) @@ -118,12 +121,24 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [ html2 = strings.Replace(html2, "$", "$", -1) html2 = strings.Replace(html2, "[", "[", -1) html2 = strings.Replace(html2, "]", "]", -1) - c.HTML(http.StatusOK, "view.tmpl", gin.H{ - "Title": title, - "WikiName": RuntimeArgs.WikiName, - "Body": template.HTML([]byte(html2)), - "Versions": versions, - }) + html2 = strings.Replace(html2, "&35;", "#", -1) + + if AdminKey == "" { + c.HTML(http.StatusOK, "view.tmpl", gin.H{ + "Title": title, + "WikiName": RuntimeArgs.WikiName, + "Body": template.HTML([]byte(html2)), + "Versions": versions, + }) + } else { + c.HTML(http.StatusOK, "view.tmpl", gin.H{ + "Title": title, + "WikiName": RuntimeArgs.WikiName, + "Body": template.HTML([]byte(html2)), + "Versions": versions, + "AdminKey": AdminKey, + }) + } } func reorderList(text string) ([]template.HTML, []string) { @@ -223,9 +238,25 @@ func deleteListItem(c *gin.Context) { } } +func deletePage(c *gin.Context) { + deleteName := c.DefaultQuery("DeleteName", "None") + adminKey := c.DefaultQuery("AdminKey", "None") + if adminKey == RuntimeArgs.AdminKey { + p := WikiData{deleteName, "", []string{}, []string{}} + p.save("") + c.JSON(200, gin.H{ + "message": "Done.", + }) + } else { + c.JSON(404, gin.H{ + "message": "?", + }) + } +} + func listEverything() string { - everything := `| Title | Current size | Changes | Total Size | -| --------- |-------------| -----| ------------- | + everything := `| Title | Current size | Changes | Total Size | | +| --------- |-------------| -----| ------------- | ------------- | ` db.View(func(tx *bolt.Tx) error { // Assume bucket exists and has keys @@ -238,7 +269,7 @@ func listEverything() string { contentSize := strconv.Itoa(len(p.CurrentText)) numChanges := strconv.Itoa(len(p.Diffs)) totalSize := strconv.Itoa(len(v)) - everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + "|\n" + everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + ` | Delete | ` + "\n" } } return nil diff --git a/templates/view.tmpl b/templates/view.tmpl index fff7bb1..7154ddf 100644 --- a/templates/view.tmpl +++ b/templates/view.tmpl @@ -10,7 +10,11 @@ - + @@ -82,6 +86,25 @@ $(document).keydown(function(e){ } }); +{{ if .AdminKey }} + $('.deleteable').click(function(event) { + event.preventDefault(); + var deleteName = $(this).attr('id') + var href = $(this).attr('href') + console.log(deleteName) + $.ajax({ + url: "/deletepage" + '?' + $.param({ + "DeleteName": deleteName, + "AdminKey": "{{ .AdminKey }}" + }), + type: 'DELETE', + success: function() { + window.location.reload(true); + } + }); + + }); +{{ end }}