diff --git a/.gitignore b/.gitignore index 4b1abc7e..1120a01b 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ themes/installed/* !themes/installed/.gitkeep .idea + +# subagent-driven-development scratch (ledger, briefs, review packages) +.superpowers/ diff --git a/admin/preview.go b/admin/preview.go new file mode 100644 index 00000000..d2af41cb --- /dev/null +++ b/admin/preview.go @@ -0,0 +1,27 @@ +package admin + +import ( + "net/http" + + "goblog/blog" + + "github.com/gin-gonic/gin" +) + +// Preview renders unsaved markdown for the editor's Preview tab, through +// exactly the path the published page uses (blog.Post.HTML), so what an +// author sees is what readers get. Admin only: the renderer keeps raw HTML. +func (a *Admin) Preview(c *gin.Context) { + if !a.auth.IsAdmin(c) { + c.JSON(http.StatusUnauthorized, "Not Authorized") + return + } + var body struct { + Content string `json:"content"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.JSON(http.StatusBadRequest, "Malformed request") + return + } + c.JSON(http.StatusOK, gin.H{"html": string(blog.Post{Content: body.Content}.HTML())}) +} diff --git a/admin/preview_test.go b/admin/preview_test.go new file mode 100644 index 00000000..f4ee06db --- /dev/null +++ b/admin/preview_test.go @@ -0,0 +1,96 @@ +package admin_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "goblog/admin" + "goblog/auth" + "goblog/blog" + + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/cookie" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/mock" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func previewHarness(t *testing.T, isAdmin bool) *gin.Engine { + t.Helper() + db, _ := gorm.Open(sqlite.Open(":memory:")) + db.AutoMigrate(&auth.BlogUser{}, &blog.PostType{}, &blog.Post{}, &blog.Setting{}, &blog.Page{}) + a := &Auth{} + a.On("IsAdmin", mock.Anything).Return(isAdmin) + a.On("IsLoggedIn", mock.Anything).Return(isAdmin) + b := blog.New(db, a, "test") + ad := admin.New(db, a, &b, "test") + gin.SetMode(gin.TestMode) + router := gin.New() + router.Use(sessions.Sessions("s", cookie.NewStore([]byte("test")))) + router.POST("/api/v1/preview", ad.Preview) + return router +} + +func postPreview(t *testing.T, router *gin.Engine, body string) *httptest.ResponseRecorder { + t.Helper() + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodPost, "/api/v1/preview", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + router.ServeHTTP(w, req) + return w +} + +// TestPreview_RendersLikeThePublishedPost: the editor's preview must be the +// same rendering the page will show, so it goes through blog.Post.HTML. +func TestPreview_RendersLikeThePublishedPost(t *testing.T) { + router := previewHarness(t, true) + md := "# Title\n\nSome **bold** text.\n\n\n" + w := postPreview(t, router, `{"content":`+jsonString(md)+`}`) + if w.Code != http.StatusOK { + t.Fatalf("code = %d, body = %s", w.Code, w.Body.String()) + } + var got struct { + HTML string `json:"html"` + } + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("response is not JSON: %v (%s)", err, w.Body.String()) + } + if want := string((blog.Post{Content: md}).HTML()); got.HTML != want { + t.Errorf("preview = %q, want %q", got.HTML, want) + } + if !strings.Contains(got.HTML, ", so a test can assert +// against the content of a specific element rather than the whole page. +// The post body under test never nests a
inside itself, so the first +//
after the opening tag is the matching close. +func divContents(t *testing.T, body, openTag string) string { + t.Helper() + start := strings.Index(body, openTag) + if start < 0 { + t.Fatalf("did not find %q in page", openTag) + } + contentStart := strings.Index(body[start:], ">") + if contentStart < 0 { + t.Fatalf("unterminated tag %q", openTag) + } + contentStart += start + 1 + end := strings.Index(body[contentStart:], "") + if end < 0 { + t.Fatalf("no closing for %q", openTag) + } + return body[contentStart : contentStart+end] +} + +// TestPostPage_ServerRendered: the post body and its comments are HTML in +// the response, so a crawler or link-preview bot that runs no JavaScript +// sees the content; showdown and DOMPurify are gone from the page (#617). +func TestPostPage_ServerRendered(t *testing.T) { + db, _ := gorm.Open(sqlite.Open(":memory:")) + db.AutoMigrate(&auth.BlogUser{}, &blog.PostType{}, &blog.Post{}, &blog.Tag{}, &blog.Comment{}, &blog.Page{}, &blog.Setting{}, &plugin.PluginSetting{}) + pt := blog.PostType{Name: "Post", Slug: "posts"} + db.Create(&pt) + when := time.Date(2026, 8, 15, 12, 0, 0, 0, time.UTC) + post := blog.Post{Title: "Rendered", Slug: "rendered", PostTypeID: pt.ID, CreatedAt: when, UpdatedAt: when, + Content: "Some **bold** text.\n\n\n"} + db.Create(&post) + db.Create(&blog.Comment{PostID: post.ID, Name: "Visitor", Content: "Nice **post**!\n\n"}) + a := &Auth{} + a.On("IsAdmin", mock.Anything).Return(false) + a.On("IsLoggedIn", mock.Anything).Return(false) + b := blog.New(db, a, "test") + + router := gin.New() + tmpl := template.Must(template.New("").Funcs(template.FuncMap{ + "rawHTML": func(s string) template.HTML { return template.HTML(s) }, + }).ParseGlob("../templates/shared/*.html")) + template.Must(tmpl.ParseGlob("../themes/default/templates/*.html")) + router.SetHTMLTemplate(tmpl) + router.GET("/posts/:yyyy/:mm/:dd/:slug", b.Post) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/posts/2026/08/15/rendered", nil) + router.ServeHTTP(w, req) + body := w.Body.String() + if w.Code != http.StatusOK { + t.Fatalf("code = %d", w.Code) + } + for _, want := range []string{"bold", "post"} { + if !strings.Contains(body, want) { + t.Errorf("page missing %q", want) + } + } + // The iframe must be inside the post body itself, not merely somewhere + // on the page: footer.html also renders an HTMLPreview of this same + // post, which would contain the same raw iframe even if the body were + // never rendered on the server. + postBody := divContents(t, body, `
`) { + t.Errorf("post body missing iframe: %q", postBody) + } + for _, gone := range []string{"showdown", "purify", "DOMPurify", "