From 2b450b66fee4906e746f6e04a6d07a72ff2faf64 Mon Sep 17 00:00:00 2001 From: grm Date: Sat, 12 Sep 2026 12:26:17 +0300 Subject: Insert images straight from the Markdown editor A shared editor partial adds an "Insert image" file picker plus paste and drag-and-drop on the textarea; a few lines of inline JS upload the file to the existing images endpoint (which now answers JSON when asked) and put the Markdown at the caret. Without JS the same file rides along with the form and is appended to the text on save, so nothing depends on scripting. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Sd8UPWrvyYCLj97JexNw3A --- internal/web/handlers_pages.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'internal/web/handlers_pages.go') diff --git a/internal/web/handlers_pages.go b/internal/web/handlers_pages.go index db46097..f2f82d1 100644 --- a/internal/web/handlers_pages.go +++ b/internal/web/handlers_pages.go @@ -59,14 +59,17 @@ func (s *Server) handlePageSave(w http.ResponseWriter, r *http.Request) { } p.Title = strings.TrimSpace(r.FormValue("title")) p.Slug = strings.TrimSpace(r.FormValue("slug")) - p.IntroMD = r.FormValue("intro") + p.IntroMD = strings.ReplaceAll(r.FormValue("intro"), "\r\n", "\n") p.ShowInNav = r.FormValue("show_in_nav") == "on" autoSlug := p.Slug == "" if autoSlug { p.Slug = slug.Make(p.Title) } + img, err := s.readUpload(r, "inline_image") var msg string switch { + case err != nil: + msg = "Image not added: " + err.Error() case p.Title == "" || len(p.Title) > 120: msg = "Title is required (max 120 characters)." case !slug.Valid(p.Slug) || reservedPageSlugs[p.Slug]: @@ -76,8 +79,8 @@ func (s *Server) handlePageSave(w http.ResponseWriter, r *http.Request) { s.renderStatus(w, r, http.StatusBadRequest, "dashboard/page_form.html", map[string]any{"page": p, "error": msg}) return } + p.IntroMD = appendImageMD(p.IntroMD, img) p.IntroHTML = markdown.Render(p.IntroMD) - var err error base := p.Slug for n := 1; ; n++ { // generated slugs get -2, -3… on collision; typed ones report the conflict p.Slug = slug.WithSuffix(base, n) -- cgit v1.2.3