Much progress
This commit is contained in:
parent
ca35e6760d
commit
c6975d3814
6 changed files with 133 additions and 19 deletions
|
|
@ -5,19 +5,21 @@ package notes
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
|
"github.com/yuin/goldmark/extension"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Note is the central data structure. It can be Saved, Rendered and Loaded
|
// Note is the central data structure. It can be Saved, Rendered and Loaded
|
||||||
// using the Save, Render and LoadNote functions.
|
// using the Save, Render and LoadNote functions.
|
||||||
type Note struct {
|
type Note struct {
|
||||||
Title string
|
Title string
|
||||||
BodyRendered string
|
BodyRendered template.HTML
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,11 +36,17 @@ func (n *Note) Save() error {
|
||||||
// Render the markdown content of the note to HTML
|
// Render the markdown content of the note to HTML
|
||||||
func (n *Note) Render() {
|
func (n *Note) Render() {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := goldmark.Convert(n.Body, &buf)
|
md := goldmark.New(
|
||||||
|
goldmark.WithExtensions(
|
||||||
|
extension.TaskList,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
err := md.Convert(n.Body, &buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
n.BodyRendered = buf.String()
|
|
||||||
|
n.BodyRendered = template.HTML(buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load a note from the disk. The path is derived from the title
|
// Load a note from the disk. The path is derived from the title
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
"gitea.gwairfelin.com/max/gonotes/internal/notes"
|
"gitea.gwairfelin.com/max/gonotes/internal/notes"
|
||||||
|
|
@ -17,15 +19,16 @@ func GetRoutes(prefix string) *http.ServeMux {
|
||||||
myurls = urls.URLs{
|
myurls = urls.URLs{
|
||||||
Prefix: prefix,
|
Prefix: prefix,
|
||||||
URLs: map[string]urls.URL{
|
URLs: map[string]urls.URL{
|
||||||
"view": {Path: "/{note}/", Protocol: "GET", Handler: viewHandler},
|
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
|
||||||
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: editHandler},
|
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
|
||||||
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: saveHandler},
|
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
|
||||||
|
"list": {Path: "/", Protocol: "GET", Handler: list},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return myurls.GetRouter()
|
return myurls.GetRouter()
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
func view(w http.ResponseWriter, r *http.Request) {
|
||||||
title := r.PathValue("note")
|
title := r.PathValue("note")
|
||||||
note, err := notes.LoadNote(title)
|
note, err := notes.LoadNote(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -36,13 +39,13 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
note.Render()
|
note.Render()
|
||||||
err = renderTemplate(w, "view.html", note)
|
err = renderTemplate(w, "view.html", note)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Print(err.Error())
|
||||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func editHandler(w http.ResponseWriter, r *http.Request) {
|
func edit(w http.ResponseWriter, r *http.Request) {
|
||||||
title := r.PathValue("note")
|
title := r.PathValue("note")
|
||||||
note, err := notes.LoadNote(title)
|
note, err := notes.LoadNote(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -51,13 +54,13 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err = renderTemplate(w, "edit.html", note)
|
err = renderTemplate(w, "edit.html", note)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Print(err.Error())
|
||||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
func save(w http.ResponseWriter, r *http.Request) {
|
||||||
title := r.PathValue("note")
|
title := r.PathValue("note")
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
note := ¬es.Note{Title: title, Body: []byte(body)}
|
note := ¬es.Note{Title: title, Body: []byte(body)}
|
||||||
|
|
@ -65,8 +68,37 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, myurls.Reverse("view", map[string]string{"note": title}), http.StatusFound)
|
http.Redirect(w, r, myurls.Reverse("view", map[string]string{"note": title}), http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTemplate(w http.ResponseWriter, tmpl string, note *notes.Note) error {
|
func list(w http.ResponseWriter, r *http.Request) {
|
||||||
t, err := template.ParseFiles(filepath.Join(conf.Conf.TemplatesDir, tmpl))
|
files, err := os.ReadDir(conf.Conf.NotesDir)
|
||||||
t.Execute(w, note)
|
if err != nil {
|
||||||
|
log.Print(err.Error())
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
titles := make([]string, 0)
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
if !f.IsDir() {
|
||||||
|
title := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
||||||
|
titles = append(titles, title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = renderTemplate(w, "list.tmpl.html", map[string]any{"titles": titles})
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err.Error())
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||||
|
files := []string{
|
||||||
|
filepath.Join(conf.Conf.TemplatesDir, "base.tmpl.html"),
|
||||||
|
filepath.Join(conf.Conf.TemplatesDir, tmpl),
|
||||||
|
}
|
||||||
|
t, err := template.ParseFiles(files...)
|
||||||
|
t.ExecuteTemplate(w, "base", context)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
templates/base.tmpl.html
Normal file
36
templates/base.tmpl.html
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
{{define "base"}}
|
||||||
|
<!doctype html>
|
||||||
|
<html lang='en'>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>{{template "title" .}}</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
|
||||||
|
rel="stylesheet"
|
||||||
|
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
|
||||||
|
crossorigin="anonymous">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<nav class="navbar navbar-expand-sm bg-body-tertiary mb-3">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a class="navbar-brand" href="#">GoNotes</a>
|
||||||
|
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/notes/">All Notes</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<div class="container">
|
||||||
|
<div class="row justify-content-md-center">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<h1>{{template "title" .}}</h1>
|
||||||
|
<main>
|
||||||
|
{{template "main" .}}
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
{{end}}
|
||||||
|
|
@ -1,6 +1,27 @@
|
||||||
<h1>Editing {{.Title}}</h1>
|
{{define "title"}}Edit {{.Title}}{{end}}
|
||||||
|
|
||||||
|
{{define "main"}}
|
||||||
<form action="save/" method="POST">
|
<form action="save/" method="POST">
|
||||||
<textarea name="body">{{printf "%s" .Body}}</textarea>
|
<div class="mb-3">
|
||||||
<input type="submit" value="Save">
|
<label for="noteBodyInput" class="form-label">Note</label>
|
||||||
|
<div id="toolbar"></div>
|
||||||
|
<textarea class="form-control" id="noteBodyInput" name="body" aria-described-by="bodyHelp">{{printf "%s" .Body}}</textarea>
|
||||||
|
<div id="bodyHelp" class="form-text">Enter your note content in markdown</div>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" type="submit">Save</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<script src="https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.js"></script>
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
type="text/css"
|
||||||
|
href="https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.css"
|
||||||
|
/>
|
||||||
|
<script type="text/javascript">
|
||||||
|
var tinyMDE = new TinyMDE.Editor({ textarea: "noteBodyInput" });
|
||||||
|
var commandBar = new TinyMDE.CommandBar({
|
||||||
|
element: "toolbar",
|
||||||
|
editor: tinyMDE,
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{{end}}
|
||||||
|
|
|
||||||
10
templates/list.tmpl.html
Normal file
10
templates/list.tmpl.html
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
{{define "title"}}All Notes{{end}}
|
||||||
|
{{define "main"}}
|
||||||
|
<ul>
|
||||||
|
{{range $title := .titles}}
|
||||||
|
<li>
|
||||||
|
<a href="{{$title}}/">{{$title}}</a>
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
|
{{end}}
|
||||||
|
|
@ -1,3 +1,10 @@
|
||||||
<h1>{{.Title}}</h1>
|
{{define "title"}}{{.Title}}{{end}}
|
||||||
|
{{define "main"}}
|
||||||
|
<div>
|
||||||
|
{{.BodyRendered}}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<a href="edit/">Edit</a>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
<div>{{printf "%s" .BodyRendered}}</div>
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue