diff --git a/go.mod b/go.mod index 7a3ee52..05fe6cc 100644 --- a/go.mod +++ b/go.mod @@ -121,7 +121,7 @@ require ( github.com/valyala/fastjson v1.6.4 // indirect go.mau.fi/util v0.2.1 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect + golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect golang.org/x/image v0.14.0 // indirect golang.org/x/oauth2 v0.15.0 // indirect golang.org/x/sys v0.15.0 // indirect diff --git a/go.sum b/go.sum index 65c42d6..bb3591c 100644 --- a/go.sum +++ b/go.sum @@ -308,8 +308,8 @@ golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE= -golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= +golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4= +golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= diff --git a/postsFuncs.go b/postsFuncs.go index 4dd7859..a9bf94e 100644 --- a/postsFuncs.go +++ b/postsFuncs.go @@ -313,40 +313,32 @@ func (p *post) getChannel() string { return p.Blog + "/" + p.Section } -func (a *goBlog) addReplyTitleAndContext(p *post) { - if replyLink := p.firstParameter(a.cfg.Micropub.ReplyParam); replyLink != "" { - addTitle := p.firstParameter(a.cfg.Micropub.ReplyTitleParam) == "" && a.getBlogFromPost(p).addReplyTitle - addContext := p.firstParameter(a.cfg.Micropub.ReplyContextParam) == "" && a.getBlogFromPost(p).addReplyContext +func (a *goBlog) addTitleAndContext(p *post, linkParam, titleParam, contextParam string, addTitleFlag, addContextFlag bool) { + if link := p.firstParameter(linkParam); link != "" { + addTitle := p.firstParameter(titleParam) == "" && addTitleFlag + addContext := p.firstParameter(contextParam) == "" && addContextFlag if !addTitle && !addContext { return } - if mf, err := a.parseMicroformats(replyLink, true); err == nil { + if mf, err := a.parseMicroformats(link, true); err == nil { if addTitle && mf.Title != "" { - p.addParameter(a.cfg.Micropub.ReplyTitleParam, mf.Title) + p.addParameter(titleParam, mf.Title) } if addContext && mf.Content != "" { - p.addParameter(a.cfg.Micropub.ReplyContextParam, mf.Content) + p.addParameter(contextParam, mf.Content) } } } } +func (a *goBlog) addReplyTitleAndContext(p *post) { + bc := a.getBlogFromPost(p) + a.addTitleAndContext(p, a.cfg.Micropub.ReplyParam, a.cfg.Micropub.ReplyTitleParam, a.cfg.Micropub.ReplyContextParam, bc.addReplyTitle, bc.addReplyContext) +} + func (a *goBlog) addLikeTitleAndContext(p *post) { - if likeLink := p.firstParameter(a.cfg.Micropub.LikeParam); likeLink != "" { - addTitle := p.firstParameter(a.cfg.Micropub.LikeTitleParam) == "" && a.getBlogFromPost(p).addLikeTitle - addContext := p.firstParameter(a.cfg.Micropub.LikeContextParam) == "" && a.getBlogFromPost(p).addLikeContext - if !addTitle && !addContext { - return - } - if mf, err := a.parseMicroformats(likeLink, true); err == nil { - if addTitle && mf.Title != "" { - p.addParameter(a.cfg.Micropub.LikeTitleParam, mf.Title) - } - if addContext && mf.Content != "" { - p.addParameter(a.cfg.Micropub.LikeContextParam, mf.Content) - } - } - } + bc := a.getBlogFromPost(p) + a.addTitleAndContext(p, a.cfg.Micropub.LikeParam, a.cfg.Micropub.LikeTitleParam, a.cfg.Micropub.LikeContextParam, bc.addLikeTitle, bc.addLikeContext) } // Public because of rendering diff --git a/postsFuncs_test.go b/postsFuncs_test.go new file mode 100644 index 0000000..1b0f124 --- /dev/null +++ b/postsFuncs_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_AddReplyTitleAndContext(t *testing.T) { + app := createMicropubTestEnv(t) + + bc := app.cfg.Blogs[app.cfg.DefaultBlog] + bc.addReplyContext = true + bc.addReplyTitle = true + + fhc := newFakeHttpClient() + app.httpClient = fhc.Client + fhc.setFakeResponse(http.StatusOK, ` + + Microformats Entry Example +
+

My First Microformats Post

+
+

This is the main content of my post. It can contain links, bold text, and more.

+
+ `) + + err := app.createPost(&post{ + Path: "/testpost", + Parameters: map[string][]string{ + "replylink": {"https://example.com"}, + }, + Content: "Test", + }) + require.NoError(t, err) + + p, err := app.getPost("/testpost") + require.NoError(t, err) + + assert.Equal(t, "My First Microformats Post", p.firstParameter("replytitle")) + assert.Equal(t, "This is the main content of my post. It can contain links, bold text, and more.", p.firstParameter("replycontext")) +}