|
|
@@ -8,6 +8,7 @@ import (
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "regexp"
|
|
|
"strings"
|
|
|
"text/template"
|
|
|
"time"
|
|
|
@@ -16,6 +17,7 @@ import (
|
|
|
"git.linuxit.ro/turos.robert/mynotes/lib/inout"
|
|
|
"github.com/yuin/goldmark"
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
+ "github.com/yuin/goldmark/renderer/html"
|
|
|
)
|
|
|
|
|
|
/*func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
|
|
|
@@ -73,10 +75,12 @@ import (
|
|
|
}*/
|
|
|
|
|
|
func MDToHTML(w http.ResponseWriter, r *http.Request) {
|
|
|
+
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
if err != nil {
|
|
|
fmt.Fprintf(w, "%+v", err)
|
|
|
}
|
|
|
+
|
|
|
mp := make(map[string]any)
|
|
|
err = json.Unmarshal(body, &mp)
|
|
|
if err != nil {
|
|
|
@@ -84,11 +88,19 @@ func MDToHTML(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
var buff bytes.Buffer
|
|
|
md := goldmark.New(
|
|
|
- goldmark.WithExtensions(extension.Table,
|
|
|
+ goldmark.WithExtensions(
|
|
|
+ extension.Table,
|
|
|
extension.DefinitionList,
|
|
|
extension.Footnote,
|
|
|
- extension.Typographer))
|
|
|
- err = md.Convert([]byte(mp["markdown"].(string)), &buff)
|
|
|
+ extension.Typographer,
|
|
|
+ ),
|
|
|
+ goldmark.WithRendererOptions(
|
|
|
+ html.WithUnsafe(),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ tagRegex := regexp.MustCompile(`\[\[([^\[]+)\]\]`)
|
|
|
+ markdown := tagRegex.ReplaceAllString(mp["markdown"].(string), `<a href="/tags/?tag=$1" class="tag">${1}</a>`)
|
|
|
+ err = md.Convert([]byte(markdown), &buff)
|
|
|
if err != nil {
|
|
|
fmt.Fprintf(w, "%+v", err)
|
|
|
}
|
|
|
@@ -175,7 +187,47 @@ func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
} else {
|
|
|
http.Redirect(w, r, "/index.html", http.StatusSeeOther)
|
|
|
}
|
|
|
+ case "delete_dir":
|
|
|
+ r.ParseForm()
|
|
|
+ path := filepath.Join("./notes_folder", r.Form.Get("path"))
|
|
|
+ fmt.Println(path)
|
|
|
+ err := inout.SafeDir(path)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = os.RemoveAll(path)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ referinta := r.Referer()
|
|
|
+ if referinta != "" {
|
|
|
+ http.Redirect(w, r, referinta, http.StatusSeeOther)
|
|
|
+ } else {
|
|
|
+ http.Redirect(w, r, "/index.html", http.StatusSeeOther)
|
|
|
+ }
|
|
|
+ case "delete_file":
|
|
|
+ r.ParseForm()
|
|
|
+ path := filepath.Join("./", r.Form.Get("path"))
|
|
|
+ err := inout.SafeDir(path)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = os.Remove(path)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ referinta := r.Referer()
|
|
|
+ if referinta != "" {
|
|
|
+ http.Redirect(w, r, referinta, http.StatusSeeOther)
|
|
|
+ } else {
|
|
|
+ http.Redirect(w, r, "/index.html", http.StatusSeeOther)
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
|
|
|
w.Header().Set("Content-Type", "text/html")
|