fix: image alt texts on markdown posts are written into the markdown
POST /_journal/api/v1/images/alt writes the alt of a markdown post into its source: the inline image  outside code (classes.SetMarkdownImageAlt) or a raw img outside code (classes.SetImageAltOutsideCode), so the next content save keeps it. An image the markdown does not hold that way is refused with 422 changes.N.src and nothing is written.
This commit is contained in:
@@ -86,14 +86,23 @@ type imageAltText struct {
|
||||
oldContent, oldHTML string
|
||||
}
|
||||
|
||||
// errMarkdownImage refuses an alt for an image a markdown post's source
|
||||
// does not hold as an inline image or a raw img.
|
||||
const errMarkdownImage = "The image is not an inline image of the post's markdown; edit the post."
|
||||
|
||||
// errForbiddenPost stops a batch that names a post the caller may not edit.
|
||||
var errForbiddenPost = errors.New("forbidden post")
|
||||
|
||||
// SetImageAlt is POST /_journal/api/v1/images/alt {dry_run, changes:
|
||||
// [{post_id, locale, src, alt}]}: it sets the alt attribute of the img whose
|
||||
// src equals src in the post's content_html (and in content when that holds
|
||||
// the same image), in the default locale when locale is empty and in that
|
||||
// translation otherwise; alt "" marks a decorative image. It needs
|
||||
// src equals src in the post's content_html, in the default locale when
|
||||
// locale is empty and in that translation otherwise; alt "" marks a
|
||||
// decorative image. The post's content gets it too: an HTML body when it
|
||||
// holds the same img; a markdown body in its inline image  or a
|
||||
// raw img outside code (classes.SetMarkdownImageAlt,
|
||||
// classes.SetImageAltOutsideCode), so the next content save keeps it. An
|
||||
// image the markdown does not hold that way (a reference-style image, for
|
||||
// example) refuses the change with 422 changes.N.src. It needs
|
||||
// golem15.journal.access_posts and may edit only the posts the caller may
|
||||
// edit (403). The batch is all or nothing: a missing post or an src that is
|
||||
// not in the post refuses it with 422 (errors keyed changes.N.field). It
|
||||
@@ -160,10 +169,24 @@ func SetImageAlt(app *backpack.App) http.HandlerFunc {
|
||||
verrs = addSettingsError(verrs, fmt.Sprintf("changes.%d.src", i), "The image is not in the post.")
|
||||
continue
|
||||
}
|
||||
text.contentHTML = html
|
||||
if content, m, _ := classes.SetImageAlt(text.content, c.src, c.alt); m > 0 {
|
||||
if classes.LooksLikeHTML(text.content) {
|
||||
// An HTML body (the rich editor's) holds the same img.
|
||||
if content, m, _ := classes.SetImageAlt(text.content, c.src, c.alt); m > 0 {
|
||||
text.content = content
|
||||
}
|
||||
} else {
|
||||
// A markdown body: the next save derives content_html
|
||||
// from it again, so the alt must land in the markdown
|
||||
// (L1 WR-05): an inline image, or a raw img outside code.
|
||||
content, m, _ := classes.SetMarkdownImageAlt(text.content, c.src, c.alt)
|
||||
content, r, _ := classes.SetImageAltOutsideCode(content, c.src, c.alt)
|
||||
if m+r == 0 {
|
||||
verrs = addSettingsError(verrs, fmt.Sprintf("changes.%d.src", i), errMarkdownImage)
|
||||
continue
|
||||
}
|
||||
text.content = content
|
||||
}
|
||||
text.contentHTML = html
|
||||
var oldAlt any
|
||||
if old != nil {
|
||||
oldAlt = *old
|
||||
|
||||
Reference in New Issue
Block a user