From 4d8c6bde5d2e774a51841858de03253890a1472c Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 13 Dec 2022 16:05:49 +0100 Subject: [PATCH] Allow to preset post parameters in the editor template (to allow bookmarklets) (#43) --- docs/usage.md | 6 +++++- editor.go | 43 ++++++++++++++++++++++++++++++++----------- ui.go | 3 ++- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 5554835..f06dc91 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -6,7 +6,11 @@ This section of the documentation is a **work in progress**! ### Scheduling posts -To schedule a post, create a post with `status: scheduled` and set the `published` field to the desired date. A scheduler runs in the background and checks every 10 seconds if a scheduled post should be published. If there's a post to publish, the post status is changed to `published`. That will also trigger configured hooks. Scheduled posts are only visible when logged in. +To schedule a post, create a post with `status: scheduled` and set the `published` field to the desired date. A scheduler runs in the background and checks every 30 seconds if a scheduled post should be published. If there's a post to publish, the post status is changed to `published`. That will also trigger configured hooks. Scheduled posts are only visible when logged in. + +### Bookmarklets + +You can preset post parameters in the editor template by adding query parameters with the prefix `p:`. So `/editor?p:title=Title` will set the title post parameter in the editor template to `Title`. This way you can create yourself bookmarklets to, for example, like posts or reply to them more easily. ## Media storage diff --git a/editor.go b/editor.go index 8050eb3..24fc77d 100644 --- a/editor.go +++ b/editor.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strings" "time" "go.goblog.app/app/pkgs/bufferpool" @@ -21,7 +22,9 @@ const editorPath = "/editor" func (a *goBlog) serveEditor(w http.ResponseWriter, r *http.Request) { a.render(w, r, a.renderEditor, &renderData{ - Data: &editorRenderData{}, + Data: &editorRenderData{ + presetParams: parsePresetPostParamsFromQuery(r), + }, }) } @@ -93,6 +96,7 @@ func (a *goBlog) serveEditorPost(w http.ResponseWriter, r *http.Request) { } a.render(w, r, a.renderEditor, &renderData{ Data: &editorRenderData{ + presetParams: parsePresetPostParamsFromQuery(r), updatePostUrl: a.fullPostURL(post), updatePostContent: a.postToMfItem(post).Properties.Content[0], }, @@ -182,24 +186,30 @@ func (a *goBlog) editorMicropubPost(w http.ResponseWriter, r *http.Request, medi _ = result.Body.Close() } -func (*goBlog) editorPostTemplate(blog string, bc *configBlog) string { +func (*goBlog) editorPostTemplate(blog string, bc *configBlog, presetParams map[string][]string) string { builder := bufferpool.Get() defer bufferpool.Put(builder) - marsh := func(param string, i any) { + marsh := func(param string, preset bool, i any) { + if _, presetPresent := presetParams[param]; !preset && presetPresent { + return + } _ = yaml.NewEncoder(builder).Encode(map[string]any{ param: i, }) } builder.WriteString("---\n") - marsh("blog", blog) - marsh("section", bc.DefaultSection) - marsh("status", statusDraft) - marsh("visibility", visibilityPublic) - marsh("priority", 0) - marsh("slug", "") - marsh("title", "") + marsh("blog", false, blog) + marsh("section", false, bc.DefaultSection) + marsh("status", false, statusDraft) + marsh("visibility", false, visibilityPublic) + marsh("priority", false, 0) + marsh("slug", false, "") + marsh("title", false, "") for _, t := range bc.Taxonomies { - marsh(t.Name, []string{""}) + marsh(t.Name, false, []string{""}) + } + for key, param := range presetParams { + marsh(key, true, param) } builder.WriteString("---\n") return builder.String() @@ -258,3 +268,14 @@ func (a *goBlog) editorPostDesc(bc *configBlog) string { } return fmt.Sprintf(t, paramBuilder.String(), "status", "visibility", statusBuilder.String(), visibilityBuilder.String()) } + +func parsePresetPostParamsFromQuery(r *http.Request) map[string][]string { + m := map[string][]string{} + for key, param := range r.URL.Query() { + if strings.HasPrefix(key, "p:") { + paramKey := strings.TrimPrefix(key, "p:") + m[paramKey] = append(m[paramKey], param...) + } + } + return m +} diff --git a/ui.go b/ui.go index 26d5faa..80e79ed 100644 --- a/ui.go +++ b/ui.go @@ -1380,6 +1380,7 @@ func (a *goBlog) renderWebmentionAdmin(hb *htmlbuilder.HtmlBuilder, rd *renderDa type editorRenderData struct { updatePostUrl string updatePostContent string + presetParams map[string][]string } func (a *goBlog) renderEditor(hb *htmlbuilder.HtmlBuilder, rd *renderData) { @@ -1421,7 +1422,7 @@ func (a *goBlog) renderEditor(hb *htmlbuilder.HtmlBuilder, rd *renderData) { "data-preview", "post-preview", "data-previewws", rd.Blog.getRelativePath("/editor/preview"), "data-syncws", rd.Blog.getRelativePath("/editor/sync"), - "data-template", a.editorPostTemplate(rd.BlogString, rd.Blog), + "data-template", a.editorPostTemplate(rd.BlogString, rd.Blog, edrd.presetParams), ) hb.WriteElementClose("textarea") hb.WriteElementOpen("div", "id", "post-preview", "class", "hide")