mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Added feature for Issue #12
This commit is contained in:
parent
ec605e1b8f
commit
c95720fe54
1
main.go
1
main.go
@ -78,6 +78,7 @@ Options:`)
|
|||||||
r.GET("/:title", editNote)
|
r.GET("/:title", editNote)
|
||||||
r.GET("/:title/*option", everythingElse)
|
r.GET("/:title/*option", everythingElse)
|
||||||
r.DELETE("/listitem", deleteListItem)
|
r.DELETE("/listitem", deleteListItem)
|
||||||
|
r.DELETE("/deletepage", deletePage)
|
||||||
if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" {
|
if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" {
|
||||||
r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey)
|
r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey)
|
||||||
} else {
|
} else {
|
||||||
|
55
routes.go
55
routes.go
@ -72,9 +72,9 @@ func everythingElse(c *gin.Context) {
|
|||||||
versionNum = -1
|
versionNum = -1
|
||||||
}
|
}
|
||||||
currentText, versions, _ := getCurrentText(title, versionNum)
|
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 {
|
} 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" {
|
} else if option == "/list" {
|
||||||
renderList(c, title)
|
renderList(c, title)
|
||||||
} else if title == "static" {
|
} 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("\\[\\[(.*?)\\]\\]")
|
r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]")
|
||||||
for _, s := range r.FindAllString(currentText, -1) {
|
for _, s := range r.FindAllString(currentText, -1) {
|
||||||
currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 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.AllowElements("img")
|
||||||
pClean.AllowAttrs("alt").OnElements("img")
|
pClean.AllowAttrs("alt").OnElements("img")
|
||||||
pClean.AllowAttrs("src").OnElements("img")
|
pClean.AllowAttrs("src").OnElements("img")
|
||||||
|
pClean.AllowAttrs("class").OnElements("a")
|
||||||
|
pClean.AllowAttrs("href").OnElements("a")
|
||||||
|
pClean.AllowAttrs("id").OnElements("a")
|
||||||
pClean.AllowDataURIImages()
|
pClean.AllowDataURIImages()
|
||||||
html := pClean.SanitizeBytes(unsafe)
|
html := pClean.SanitizeBytes(unsafe)
|
||||||
html2 := string(html)
|
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)
|
html2 = strings.Replace(html2, "[", "[", -1)
|
||||||
html2 = strings.Replace(html2, "]", "]", -1)
|
html2 = strings.Replace(html2, "]", "]", -1)
|
||||||
c.HTML(http.StatusOK, "view.tmpl", gin.H{
|
html2 = strings.Replace(html2, "&35;", "#", -1)
|
||||||
"Title": title,
|
|
||||||
"WikiName": RuntimeArgs.WikiName,
|
if AdminKey == "" {
|
||||||
"Body": template.HTML([]byte(html2)),
|
c.HTML(http.StatusOK, "view.tmpl", gin.H{
|
||||||
"Versions": versions,
|
"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) {
|
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 {
|
func listEverything() string {
|
||||||
everything := `| Title | Current size | Changes | Total Size |
|
everything := `| Title | Current size | Changes | Total Size | |
|
||||||
| --------- |-------------| -----| ------------- |
|
| --------- |-------------| -----| ------------- | ------------- |
|
||||||
`
|
`
|
||||||
db.View(func(tx *bolt.Tx) error {
|
db.View(func(tx *bolt.Tx) error {
|
||||||
// Assume bucket exists and has keys
|
// Assume bucket exists and has keys
|
||||||
@ -238,7 +269,7 @@ func listEverything() string {
|
|||||||
contentSize := strconv.Itoa(len(p.CurrentText))
|
contentSize := strconv.Itoa(len(p.CurrentText))
|
||||||
numChanges := strconv.Itoa(len(p.Diffs))
|
numChanges := strconv.Itoa(len(p.Diffs))
|
||||||
totalSize := strconv.Itoa(len(v))
|
totalSize := strconv.Itoa(len(v))
|
||||||
everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + "|\n"
|
everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + ` | <a class="deleteable" id="` + p.Title + `">Delete</a> | ` + "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -10,7 +10,11 @@
|
|||||||
<link rel="stylesheet" type="text/css" href="/static/css/katex.min.css">
|
<link rel="stylesheet" type="text/css" href="/static/css/katex.min.css">
|
||||||
<script src="/static/js/katex.min.js"></script>
|
<script src="/static/js/katex.min.js"></script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
a.deleteable {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
@ -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 }}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user