From 20ca509a2b296f7675f5f4161fa73bbe1ee01403 Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Wed, 8 May 2024 16:49:34 +0300 Subject: [PATCH] Add new note prompt --- CHANGELOG.md | 8 ++++++++ config.go | 4 +++- i18n/en.json | 4 +++- i18n/ru.json | 4 +++- info.go | 2 +- pages/base.html | 2 +- public/{date.js => main.js} | 14 ++++++++++++++ routes.go | 15 ++++++++++++--- 8 files changed, 45 insertions(+), 8 deletions(-) rename public/{date.js => main.js} (73%) diff --git a/CHANGELOG.md b/CHANGELOG.md index f04fa32..1ea4e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # Changelog This file keeps track of changes in more human-readable fashion +## v0.5.0 +* Added a JS prompt to create new note + * "Sanitization" for this method is basic and assumes a well-meaning user + * Old instructions appear if JS is disabled +* Bug fixes + * Non-latin note names are now rendered correctly + * Config reload now sets removed values to defaults + ## v0.4.0 * Customisation changes * Added `title` option to config diff --git a/config.go b/config.go index 0de2fc8..2e7e687 100644 --- a/config.go +++ b/config.go @@ -76,6 +76,8 @@ func (c *Config) Save() error { } func (c *Config) Reload() error { + *c = DefaultConfig // Reset config + if _, err := os.Stat(ConfigFile); errors.Is(err, os.ErrNotExist) { err := c.Save() if err != nil { @@ -156,7 +158,7 @@ func (c *Config) Reload() error { // ConfigInit loads config on startup func ConfigInit() Config { - cfg := DefaultConfig + cfg := Config{} err := cfg.Reload() if err != nil { log.Fatal(err) diff --git a/i18n/en.json b/i18n/en.json index 018d761..79092b6 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -12,10 +12,12 @@ "link.notes": "notes", "link.info": "app information", - "description.notes": "/notes/ for a new note", "time.date": "Today is", "time.grace": "grace period active", "button.save": "Save", + "button.notes": "New note", + "prompt.notes": "Note name", + "noscript.notes": "/notes/", "info.version": "Version", "info.version.link": "source and changelog", diff --git a/i18n/ru.json b/i18n/ru.json index 274e279..4240be5 100644 --- a/i18n/ru.json +++ b/i18n/ru.json @@ -12,10 +12,12 @@ "link.notes": "заметки", "link.info": "системная информация", - "description.notes": "/notes/<название> для новой заметки", "time.date": "Сегодня", "time.grace": "льготный период", "button.save": "Сохранить", + "button.notes": "Новая заметка", + "prompt.notes": "Название заметки", + "noscript.notes": "/notes/<название>", "info.version": "Версия", "info.version.link": "исходный код", diff --git a/info.go b/info.go index 0185d27..27649b2 100644 --- a/info.go +++ b/info.go @@ -15,7 +15,7 @@ type AppInfo struct { // Info contains app information var Info = AppInfo{ - Version: "0.4.0", + Version: "0.5.0", SourceLink: "https://git.a71.su/Andrew71/hibiscus", } diff --git a/pages/base.html b/pages/base.html index c53fcad..39fc9a6 100644 --- a/pages/base.html +++ b/pages/base.html @@ -15,7 +15,7 @@ - + Hibiscus.txt diff --git a/public/date.js b/public/main.js similarity index 73% rename from public/date.js rename to public/main.js index de59770..ea339a2 100644 --- a/public/date.js +++ b/public/main.js @@ -31,4 +31,18 @@ function beginTimeUpdater() { setTimeout(callEveryMinute, difference); setTimeout(updateTime, difference); updateTime(); +} + +// This does NOT properly sanitize, and assumes a well-meaning user +function sanitize(title) { + return title + .trim() + .replace(/ +/g, '-') + .replace(/[!*'();:@&=+$,\/?#\[\]]/g, '') +} + +// Open a new note +function newNote(text_prompt) { + name = sanitize(prompt(text_prompt + ':')) + window.location.replace('/notes/' + name) } \ No newline at end of file diff --git a/routes.go b/routes.go index 5d72ff8..5d3fa3d 100644 --- a/routes.go +++ b/routes.go @@ -6,6 +6,7 @@ import ( "html/template" "log/slog" "net/http" + "net/url" "os" "strings" "time" @@ -13,7 +14,7 @@ import ( type EntryList struct { Title string - Description string + Description template.HTML Entries []Entry } @@ -77,7 +78,7 @@ func PostToday(w http.ResponseWriter, r *http.Request) { } // GetEntries is a generic HTML renderer for a list -func GetEntries(w http.ResponseWriter, r *http.Request, title string, description string, dir string, format formatEntries) { +func GetEntries(w http.ResponseWriter, r *http.Request, title string, description template.HTML, dir string, format formatEntries) { filesList, err := ListFiles(dir) if err != nil { slog.Error("error reading file list", "directory", dir, "error", err) @@ -124,7 +125,11 @@ func GetDays(w http.ResponseWriter, r *http.Request) { // GetNotes renders HTML list of all notes func GetNotes(w http.ResponseWriter, r *http.Request) { - GetEntries(w, r, TranslatableText("title.notes"), TranslatableText("description.notes"), "notes", func(files []string) []Entry { + // This is suboptimal, but will do... + description := template.HTML( + "" + TranslatableText("button.notes") + "" + + " ") + GetEntries(w, r, TranslatableText("title.notes"), description, "notes", func(files []string) []Entry { var filesFormatted []Entry for _, v := range files { titleString := strings.Replace(v, "-", " ", -1) // FIXME: what if I need a hyphen? @@ -195,6 +200,10 @@ func GetNote(w http.ResponseWriter, r *http.Request) { HandleWrite(w.Write([]byte("note not specified"))) return } + // Handle non-latin note names + if decodedNote, err := url.QueryUnescape(noteString); err == nil { + noteString = decodedNote + } GetEntry(w, r, noteString, "notes/"+noteString, true) }