feat(journal): add EmbedRegistry, YAML boot config, and pair-aware sanitizer
This commit is contained in:
@@ -90,6 +90,7 @@ var (
|
||||
func BlogEditorTypes(app func() *backpack.App) []pact.FormFieldType {
|
||||
upload := uploadBlogImage(app)
|
||||
decode := decodeBlogHTMLFor(app)
|
||||
load := loadBlogHTMLFor(app)
|
||||
options := blogRequestOptions(app)
|
||||
return []pact.FormFieldType{
|
||||
{
|
||||
@@ -100,7 +101,7 @@ func BlogEditorTypes(app func() *backpack.App) []pact.FormFieldType {
|
||||
Options: []string{"uploadFolder"},
|
||||
CheckOptions: checkBlogOptions,
|
||||
Decode: decode,
|
||||
Load: loadBlogHTML,
|
||||
Load: load,
|
||||
ListValue: blogListValue,
|
||||
Upload: upload,
|
||||
RequestOptions: options,
|
||||
@@ -114,7 +115,7 @@ func BlogEditorTypes(app func() *backpack.App) []pact.FormFieldType {
|
||||
Options: []string{"htmlColumn", "uploadFolder"},
|
||||
CheckOptions: checkBlogOptions,
|
||||
Decode: decode,
|
||||
Load: loadBlogHTML,
|
||||
Load: load,
|
||||
ListValue: blogListValue,
|
||||
Upload: upload,
|
||||
RequestOptions: options,
|
||||
@@ -168,7 +169,8 @@ func checkUploadFolder(folder string) error {
|
||||
// or answer nil; the setting is then read from the database alone.
|
||||
func decodeBlogHTMLFor(app func() *backpack.App) func(context.Context, pact.FormFieldValue) (pact.FormFieldDecoded, error) {
|
||||
return func(ctx context.Context, in pact.FormFieldValue) (pact.FormFieldDecoded, error) {
|
||||
out, err := decodeBlogHTML(ctx, in)
|
||||
a := appInstance(app)
|
||||
out, err := decodeBlogHTMLWith(ctx, in, EmbedRegistryFrom(a))
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
@@ -201,12 +203,14 @@ func h1ConvertedNotice(record any) string {
|
||||
|
||||
// blogRequestOptions is the Blog Editor types' RequestOptions (33-01
|
||||
// P33-02, UI-SPEC C28): headingLevels, [2, 3, 4] while the H1 setting is on
|
||||
// and [1, 2, 3, 4] while it is off (J1), and genericAnchors, the settings'
|
||||
// generic link texts (J2, J3). Both are read per schema request, so a
|
||||
// changed setting reaches the next form that opens. db may be nil; the
|
||||
// and [1, 2, 3, 4] while it is off (J1), genericAnchors, the settings'
|
||||
// generic link texts (J2, J3), and embedProviders, the registered embed
|
||||
// providers from the app's EmbedRegistry. All are read per schema request, so
|
||||
// a changed setting reaches the next form that opens. db may be nil; the
|
||||
// defaults then apply. fields.yaml options with the same names win.
|
||||
func blogRequestOptions(app func() *backpack.App) func(context.Context, *gorm.DB) (map[string]any, error) {
|
||||
return func(ctx context.Context, db *gorm.DB) (map[string]any, error) {
|
||||
a := appInstance(app)
|
||||
levels := []int{2, 3, 4}
|
||||
if !EffectiveH1ToH2(ctx, db, appConfig(app)) {
|
||||
levels = []int{1, 2, 3, 4}
|
||||
@@ -214,29 +218,35 @@ func blogRequestOptions(app func() *backpack.App) func(context.Context, *gorm.DB
|
||||
return map[string]any{
|
||||
"headingLevels": levels,
|
||||
"genericAnchors": GenericAnchors(ctx, db),
|
||||
"embedProviders": EmbedRegistryFrom(a).EditorDescriptors(),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func appInstance(app func() *backpack.App) *backpack.App {
|
||||
if app == nil {
|
||||
return nil
|
||||
}
|
||||
return app()
|
||||
}
|
||||
|
||||
// appConfig is the config of the application app answers, nil when there
|
||||
// is none.
|
||||
func appConfig(app func() *backpack.App) *compass.Config {
|
||||
if app == nil {
|
||||
return nil
|
||||
}
|
||||
a := app()
|
||||
a := appInstance(app)
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
return a.Config
|
||||
}
|
||||
|
||||
// decodeBlogHTML stores the HTML policy's output for a submitted value: the
|
||||
// exact submitted bytes with no notice when the policy keeps everything,
|
||||
// otherwise the stripped HTML with notices naming what was removed. nil
|
||||
// decodes to the empty string; any other value that is not a string is
|
||||
// refused.
|
||||
func decodeBlogHTML(_ context.Context, in pact.FormFieldValue) (pact.FormFieldDecoded, error) {
|
||||
// decodeBlogHTML stores the HTML policy's output for a submitted value using
|
||||
// the default embed registry.
|
||||
func decodeBlogHTML(ctx context.Context, in pact.FormFieldValue) (pact.FormFieldDecoded, error) {
|
||||
return decodeBlogHTMLWith(ctx, in, defaultEmbedRegistry)
|
||||
}
|
||||
|
||||
func decodeBlogHTMLWith(_ context.Context, in pact.FormFieldValue, reg *EmbedRegistry) (pact.FormFieldDecoded, error) {
|
||||
var text string
|
||||
switch v := in.Value.(type) {
|
||||
case nil:
|
||||
@@ -246,7 +256,7 @@ func decodeBlogHTML(_ context.Context, in pact.FormFieldValue) (pact.FormFieldDe
|
||||
default:
|
||||
return pact.FormFieldDecoded{}, &pact.FormFieldError{Messages: []string{editorContentInvalid}}
|
||||
}
|
||||
clean, report := SanitizeHTMLReport(text)
|
||||
clean, report := reg.SanitizeHTMLReport(text)
|
||||
return pact.FormFieldDecoded{Value: clean, Notices: removalNotices(report)}, nil
|
||||
}
|
||||
|
||||
@@ -304,7 +314,17 @@ func removalNames(names []string, cut bool) string {
|
||||
// - Any other value is markdown, served as the htmlColumn attribute, or as
|
||||
// FormatHTML of the value when that is empty or cannot be read (the
|
||||
// value itself when rendering fails).
|
||||
func loadBlogHTML(_ context.Context, in pact.FormFieldValue) (any, error) {
|
||||
func loadBlogHTMLFor(app func() *backpack.App) func(context.Context, pact.FormFieldValue) (any, error) {
|
||||
return func(ctx context.Context, in pact.FormFieldValue) (any, error) {
|
||||
return loadBlogHTMLWith(ctx, in, EmbedRegistryFrom(appInstance(app)))
|
||||
}
|
||||
}
|
||||
|
||||
func loadBlogHTML(ctx context.Context, in pact.FormFieldValue) (any, error) {
|
||||
return loadBlogHTMLWith(ctx, in, defaultEmbedRegistry)
|
||||
}
|
||||
|
||||
func loadBlogHTMLWith(_ context.Context, in pact.FormFieldValue, reg *EmbedRegistry) (any, error) {
|
||||
text := storedText(in.Value)
|
||||
column, _ := in.Options["htmlColumn"].(string)
|
||||
if column == "" {
|
||||
@@ -327,14 +347,14 @@ func loadBlogHTML(_ context.Context, in pact.FormFieldValue) (any, error) {
|
||||
}
|
||||
return text, nil
|
||||
case LooksLikeHTML(text):
|
||||
if html == "" || sameStoredHTML(text, html) {
|
||||
if html == "" || sameStoredHTMLWith(text, html, reg) {
|
||||
return text, nil
|
||||
}
|
||||
return html, nil
|
||||
case html != "":
|
||||
return html, nil
|
||||
}
|
||||
rendered, err := FormatHTML(text)
|
||||
rendered, err := reg.FormatHTML(text)
|
||||
if err != nil {
|
||||
return text, nil
|
||||
}
|
||||
@@ -346,8 +366,12 @@ func loadBlogHTML(_ context.Context, in pact.FormFieldValue) (any, error) {
|
||||
// white space at either end ignored. It is false when rendered came from
|
||||
// rendering text as markdown in a way that changed it.
|
||||
func sameStoredHTML(text, rendered string) bool {
|
||||
return sameStoredHTMLWith(text, rendered, defaultEmbedRegistry)
|
||||
}
|
||||
|
||||
func sameStoredHTMLWith(text, rendered string, reg *EmbedRegistry) bool {
|
||||
rendered = strings.TrimSpace(rendered)
|
||||
return rendered == strings.TrimSpace(text) || rendered == strings.TrimSpace(SanitizeHTML(text))
|
||||
return rendered == strings.TrimSpace(text) || rendered == strings.TrimSpace(reg.SanitizeHTML(text))
|
||||
}
|
||||
|
||||
// storedText is a stored column value as text: a string, a *string (nil is
|
||||
|
||||
Reference in New Issue
Block a user