feat(journal): add EmbedRegistry, YAML boot config, and pair-aware sanitizer
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user