feat(journal): add EmbedRegistry, YAML boot config, and pair-aware sanitizer

This commit is contained in:
Jakub Zych
2026-11-28 12:40:33 +01:00
parent 6378d5e815
commit 55ac26e0d3
10 changed files with 603 additions and 118 deletions

View File

@@ -285,7 +285,7 @@ func Store(app *backpack.App) http.HandlerFunc {
writeAPIError(w, http.StatusBadRequest, errValidationFailed)
return
}
post, verrs := buildNewPost(r, db.WithContext(r.Context()), pr, body)
post, verrs := buildNewPost(r, db.WithContext(r.Context()), app, pr, body)
if verrs != nil {
writeValidation(w, verrs)
return
@@ -305,7 +305,7 @@ func Store(app *backpack.App) http.HandlerFunc {
if err := tx.Create(post).Error; err != nil {
return err
}
tags, err := applyPostAssocs(ctx, tx, post, body, h1)
tags, err := applyPostAssocs(ctx, tx, app, post, body, h1)
if err != nil {
return err
}
@@ -362,7 +362,7 @@ func Update(app *backpack.App) http.HandlerFunc {
// D-92/D-102: the values before the write, for the RecordSaved
// subscribers hear (a slug changed over MCP gets its redirect).
before := snapshotPost(r.Context(), db.WithContext(r.Context()), post, body)
if verrs := applyPostUpdate(r, db.WithContext(r.Context()), post, body); verrs != nil {
if verrs := applyPostUpdate(r, db.WithContext(r.Context()), app, post, body); verrs != nil {
writeValidation(w, verrs)
return
}
@@ -383,7 +383,7 @@ func Update(app *backpack.App) http.HandlerFunc {
if err := tx.Save(post).Error; err != nil {
return err
}
tags, err := applyPostAssocs(ctx, tx, post, body, h1)
tags, err := applyPostAssocs(ctx, tx, app, post, body, h1)
if err != nil {
return err
}

View File

@@ -500,7 +500,7 @@ func generateUniqueSlug(db *gorm.DB, title string, exceptID uint) string {
}
}
func buildNewPost(r *http.Request, db *gorm.DB, pr *bouncer.Principal, body map[string]any) (*models.Post, map[string][]string) {
func buildNewPost(r *http.Request, db *gorm.DB, app *backpack.App, pr *bouncer.Principal, body map[string]any) (*models.Post, map[string][]string) {
errs := map[string][]string{}
title := strings.TrimSpace(asString(body["title"]))
if title == "" {
@@ -534,7 +534,7 @@ func buildNewPost(r *http.Request, db *gorm.DB, pr *bouncer.Principal, body map[
if len(errs) > 0 {
return nil, errs
}
html, err := classes.FormatHTML(content)
html, err := classes.EmbedRegistryFrom(app).FormatHTML(content)
if err != nil {
return nil, map[string][]string{"content": {err.Error()}}
}
@@ -563,7 +563,7 @@ func buildNewPost(r *http.Request, db *gorm.DB, pr *bouncer.Principal, body map[
return post, nil
}
func applyPostUpdate(r *http.Request, db *gorm.DB, post *models.Post, body map[string]any) map[string][]string {
func applyPostUpdate(r *http.Request, db *gorm.DB, app *backpack.App, post *models.Post, body map[string]any) map[string][]string {
errs := map[string][]string{}
if _, has := body["title"]; has {
title := strings.TrimSpace(asString(body["title"]))
@@ -590,7 +590,7 @@ func applyPostUpdate(r *http.Request, db *gorm.DB, post *models.Post, body map[s
if strings.TrimSpace(content) == "" {
errs["content"] = []string{"The content field is required."}
} else {
html, err := classes.FormatHTML(content)
html, err := classes.EmbedRegistryFrom(app).FormatHTML(content)
if err != nil {
errs["content"] = []string{err.Error()}
} else {
@@ -708,7 +708,7 @@ func asUintSlice(raw any) ([]uint, bool) {
//
// h1 converts H1 in the translated content while the H1 setting is on; it
// may be nil.
func applyPostAssocs(ctx context.Context, db *gorm.DB, post *models.Post, body map[string]any, h1 *h1Conversion) (created []models.Tag, err error) {
func applyPostAssocs(ctx context.Context, db *gorm.DB, app *backpack.App, post *models.Post, body map[string]any, h1 *h1Conversion) (created []models.Tag, err error) {
if _, has := body["categories"]; has {
ids, _ := asUintSlice(body["categories"])
if err := db.Where("post_id = ?", post.ID).Delete(&models.PostCategory{}).Error; err != nil {
@@ -736,7 +736,7 @@ func applyPostAssocs(ctx context.Context, db *gorm.DB, post *models.Post, body m
}
}
if raw, has := body["translations"]; has {
if err := syncTranslations(ctx, db, post, raw, h1); err != nil {
if err := syncTranslations(ctx, db, app, post, raw, h1); err != nil {
return nil, err
}
}
@@ -788,11 +788,12 @@ func resolveTagIDs(db *gorm.DB, raw any) (ids []uint, created []models.Tag, err
return ids, created, nil
}
func syncTranslations(ctx context.Context, db *gorm.DB, post *models.Post, raw any, h1 *h1Conversion) error {
func syncTranslations(ctx context.Context, db *gorm.DB, app *backpack.App, post *models.Post, raw any, h1 *h1Conversion) error {
m, ok := raw.(map[string]any)
if !ok {
return nil
}
reg := classes.EmbedRegistryFrom(app)
fields := []string{"title", "slug", "content", "content_html", "excerpt", "metadata"}
for locale, v := range m {
attrs, ok := v.(map[string]any)
@@ -802,7 +803,7 @@ func syncTranslations(ctx context.Context, db *gorm.DB, post *models.Post, raw a
// Every translated content_html goes through the host column's
// policy: derived from content, or sanitized when only content_html
// is supplied. Winter stored a supplied content_html verbatim.
if err := classes.FormatTranslation(attrs); err != nil {
if err := reg.FormatTranslation(attrs); err != nil {
return err
}
h1.convertTranslation(locale, attrs)