Much progress

This commit is contained in:
Maximilian Friedersdorff 2025-01-27 22:28:18 +00:00
parent ca35e6760d
commit c6975d3814
6 changed files with 133 additions and 19 deletions

View file

@ -5,19 +5,21 @@ package notes
import (
"bytes"
"fmt"
"html/template"
"log"
"os"
"path/filepath"
"gitea.gwairfelin.com/max/gonotes/internal/conf"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
// Note is the central data structure. It can be Saved, Rendered and Loaded
// using the Save, Render and LoadNote functions.
type Note struct {
Title string
BodyRendered string
BodyRendered template.HTML
Body []byte
}
@ -34,11 +36,17 @@ func (n *Note) Save() error {
// Render the markdown content of the note to HTML
func (n *Note) Render() {
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 {
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

View file

@ -4,7 +4,9 @@ import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"gitea.gwairfelin.com/max/gonotes/internal/conf"
"gitea.gwairfelin.com/max/gonotes/internal/notes"
@ -17,15 +19,16 @@ func GetRoutes(prefix string) *http.ServeMux {
myurls = urls.URLs{
Prefix: prefix,
URLs: map[string]urls.URL{
"view": {Path: "/{note}/", Protocol: "GET", Handler: viewHandler},
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: editHandler},
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: saveHandler},
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
"list": {Path: "/", Protocol: "GET", Handler: list},
},
}
return myurls.GetRouter()
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
func view(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
note, err := notes.LoadNote(title)
if err != nil {
@ -36,13 +39,13 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
note.Render()
err = renderTemplate(w, "view.html", note)
if err != nil {
log.Println(err)
log.Print(err.Error())
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
return
}
}
func editHandler(w http.ResponseWriter, r *http.Request) {
func edit(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
note, err := notes.LoadNote(title)
if err != nil {
@ -51,13 +54,13 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
err = renderTemplate(w, "edit.html", note)
if err != nil {
log.Println(err)
log.Print(err.Error())
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
return
}
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
func save(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
body := r.FormValue("body")
note := &notes.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)
}
func renderTemplate(w http.ResponseWriter, tmpl string, note *notes.Note) error {
t, err := template.ParseFiles(filepath.Join(conf.Conf.TemplatesDir, tmpl))
t.Execute(w, note)
func list(w http.ResponseWriter, r *http.Request) {
files, err := os.ReadDir(conf.Conf.NotesDir)
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
}

36
templates/base.tmpl.html Normal file
View 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}}

View file

@ -1,6 +1,27 @@
<h1>Editing {{.Title}}</h1>
{{define "title"}}Edit {{.Title}}{{end}}
{{define "main"}}
<form action="save/" method="POST">
<textarea name="body">{{printf "%s" .Body}}</textarea>
<input type="submit" value="Save">
<div class="mb-3">
<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>
<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
View 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}}

View file

@ -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>