From c43f8c18ca287a83a9937b158e372749a517cba1 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 12 Feb 2020 17:42:03 -0500 Subject: [PATCH] slug: create slug from md every time Fixes #13 --- entry/entry.go | 2 ++ frontend/src/components/MainEditor.vue | 2 +- web/entry.go | 11 +++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/entry/entry.go b/entry/entry.go index 41a06f1..0b39b9b 100644 --- a/entry/entry.go +++ b/entry/entry.go @@ -55,6 +55,8 @@ func PrepareTable(tx *sqlx.Tx) error { func NewFromMd(db *db.Database, body string) *Entry { e := New(db) e.Content = body + e.Title = e.GenerateTitle() + e.Slug = e.UniqueSlug() return e } diff --git a/frontend/src/components/MainEditor.vue b/frontend/src/components/MainEditor.vue index 1b2c4ec..2a808b6 100644 --- a/frontend/src/components/MainEditor.vue +++ b/frontend/src/components/MainEditor.vue @@ -75,7 +75,7 @@ .then(res => { this.$emit('markDirty', false) this.$store.dispatch('updateSearch') - if (res.data.Slug != this.$route.params.slug) + if (res.data.Slug !== this.$route.params.slug) this.$router.replace({params: { slug: res.data.Slug }}) }) .catch(() => { }) diff --git a/web/entry.go b/web/entry.go index 83a0e0f..70ddf84 100644 --- a/web/entry.go +++ b/web/entry.go @@ -17,13 +17,16 @@ func (web *Web) editEntry(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) slug := vars["slug"] dec := json.NewDecoder(r.Body) - newEntry := entry.New(web.db) - err := dec.Decode(&newEntry) + req := struct { + Content string + }{} + err := dec.Decode(&req) if err != nil { w.WriteHeader(500) fmt.Fprint(w, err) return } + newEntry := entry.NewFromMd(web.db, req.Content) oldEntry, err := entry.GetBySlug(web.db, slug) if err != nil { @@ -33,6 +36,8 @@ func (web *Web) editEntry(w http.ResponseWriter, r *http.Request) { } oldEntry.Content = newEntry.Content + oldEntry.Title = newEntry.Title + oldEntry.Slug = newEntry.UniqueSlug() oldEntry.Tags = newEntry.Tags oldEntry.Updated = time.Now() @@ -49,8 +54,6 @@ func (web *Web) editEntry(w http.ResponseWriter, r *http.Request) { return } - log.Debug().Interface("oldEntry", oldEntry).Msg("Got a PUT") - w.Header().Set("content-type", "application/json") fmt.Fprint(w, string(resp)) }