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

@@ -69,7 +69,7 @@ failed".`,
path := flagOrDefault(in, "path", defaultExportPath)
force := flagOn(in, "force")
dry := flagOn(in, "dry-run")
stats, err := ImportFromDirConfig(ctx, mustDB(app), appConfig(app), path, force, dry, out, nil)
stats, err := ImportFromDirApp(ctx, mustDB(app), app, path, force, dry, out, nil)
if err != nil {
return err
}
@@ -98,6 +98,16 @@ func ImportFromDir(ctx context.Context, gdb *gorm.DB, dir string, force, dry boo
// setting is on, every H1 the import stores becomes an H2, counted in
// ImportStats.H1Converted.
func ImportFromDirConfig(ctx context.Context, gdb *gorm.DB, cfg *compass.Config, dir string, force, dry bool, out bonfire.Output, guard ImportGuard) (ImportStats, error) {
return importFromDirWith(ctx, gdb, cfg, classes.EmbedRegistryFrom(nil), dir, force, dry, out, guard)
}
// ImportFromDirApp is ImportFromDirConfig using app's config and registered
// EmbedRegistry for HTML sanitization.
func ImportFromDirApp(ctx context.Context, gdb *gorm.DB, app *backpack.App, dir string, force, dry bool, out bonfire.Output, guard ImportGuard) (ImportStats, error) {
return importFromDirWith(ctx, gdb, appConfig(app), classes.EmbedRegistryFrom(app), dir, force, dry, out, guard)
}
func importFromDirWith(ctx context.Context, gdb *gorm.DB, cfg *compass.Config, reg *classes.EmbedRegistry, dir string, force, dry bool, out bonfire.Output, guard ImportGuard) (ImportStats, error) {
var stats ImportStats
if gdb == nil {
return stats, fmt.Errorf("journal: database is not configured")
@@ -142,7 +152,7 @@ func ImportFromDirConfig(ctx context.Context, gdb *gorm.DB, cfg *compass.Config,
stats.Failed++
continue
}
result, converted, err := importPost(ctx, tx, data, force, dry, h1ToH2, out, guard)
result, converted, err := importPost(ctx, tx, reg, data, force, dry, h1ToH2, out, guard)
if err != nil {
return err
}
@@ -174,7 +184,7 @@ func ImportFromDirConfig(ctx context.Context, gdb *gorm.DB, cfg *compass.Config,
// importPost writes one post file. converted is the number of H1 headings
// stored as H2 (h1ToH2 on); a dry run converts nothing.
func importPost(ctx context.Context, db *gorm.DB, data PostFile, force, dry, h1ToH2 bool, out bonfire.Output, guard ImportGuard) (string, int, error) {
func importPost(ctx context.Context, db *gorm.DB, reg *classes.EmbedRegistry, data PostFile, force, dry, h1ToH2 bool, out bonfire.Output, guard ImportGuard) (string, int, error) {
var existing models.Post
err := db.Where("slug = ?", data.Slug).Take(&existing).Error
found := err == nil
@@ -219,7 +229,7 @@ func importPost(ctx context.Context, db *gorm.DB, data PostFile, force, dry, h1T
return "failed", 0, err
}
}
html, err := classes.FormatHTML(post.Content)
html, err := reg.FormatHTML(post.Content)
if err != nil {
return "failed", 0, err
}
@@ -240,7 +250,7 @@ func importPost(ctx context.Context, db *gorm.DB, data PostFile, force, dry, h1T
if err := syncCategories(ctx, db, post, data, out); err != nil {
return "failed", 0, err
}
n, err := syncTranslations(ctx, db, post, data.Translations, h1ToH2)
n, err := syncTranslations(ctx, db, reg, post, data.Translations, h1ToH2)
if err != nil {
return "failed", 0, err
}
@@ -351,7 +361,7 @@ func titleFromSlug(slug string) string {
// syncTranslations writes the file's translations and returns the number
// of H1 headings stored as H2 (h1ToH2 on).
func syncTranslations(ctx context.Context, db *gorm.DB, post *models.Post, translations map[string]map[string]any, h1ToH2 bool) (int, error) {
func syncTranslations(ctx context.Context, db *gorm.DB, reg *classes.EmbedRegistry, post *models.Post, translations map[string]map[string]any, h1ToH2 bool) (int, error) {
if len(translations) == 0 {
return 0, nil
}
@@ -360,7 +370,7 @@ func syncTranslations(ctx context.Context, db *gorm.DB, post *models.Post, trans
for locale, attrs := range translations {
// A translated content_html is derived from content or sanitized,
// as the host column is; Winter's ImportPosts stored it verbatim.
if err := classes.FormatTranslation(attrs); err != nil {
if err := reg.FormatTranslation(attrs); err != nil {
return 0, err
}
if h1ToH2 {