Allow to preset post parameters in the editor template (to allow bookmarklets) (#43)

This commit is contained in:
Jan-Lukas Else 2022-12-13 16:05:49 +01:00
parent 2bb536f2f7
commit 4d8c6bde5d
3 changed files with 39 additions and 13 deletions

View File

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

View File

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

3
ui.go
View File

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