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 ![alt](src) 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:
Jakub Zych
2026-11-04 23:20:02 +01:00
parent 5f683cba31
commit 7662d54ab3
6 changed files with 449 additions and 8 deletions

View File

@@ -662,6 +662,36 @@ func TestSetImageAlt(t *testing.T) {
if !found {
t.Fatalf("no RecordSaved with the default and pl content_html: %#v", records.savedEvents())
}
// L1 WR-05: on a markdown post the alt lands in the markdown, outside
// code, and survives the next save of the same content.
sample := "```\n![](" + a + ")\n```\n"
md := "Intro\n\n![](" + a + ")\n\n" + sample
mdPost := apiPostID(t, env.expect(t, http.StatusCreated, http.MethodPost, journalAPIPrefix+"/posts", editor, map[string]any{"title": "Markdown images", "content": md}))
env.expect(t, http.StatusOK, http.MethodPost, path, editor, map[string]any{
"changes": []any{map[string]any{"post_id": mdPost, "src": a, "alt": "Grzyb [leśny]"}},
})
stored := storedPost(t, env, mdPost)
if stored.Content != "Intro\n\n![Grzyb \\[leśny\\]]("+a+")\n\n"+sample || !strings.Contains(stored.ContentHTML, `alt="Grzyb [leśny]"`) {
t.Fatalf("markdown post after the alt = %q / %q", stored.Content, stored.ContentHTML)
}
env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/posts/%d", journalAPIPrefix, mdPost), editor, map[string]any{"content": stored.Content})
if again := storedPost(t, env, mdPost); !strings.Contains(again.ContentHTML, `alt="Grzyb [leśny]"`) {
t.Fatalf("the next content save lost the alt: %q", again.ContentHTML)
}
// A reference-style image is not rewritten: 422, nothing written.
ref := "![old][r]\n\n[r]: " + a + "\n"
refPost := apiPostID(t, env.expect(t, http.StatusCreated, http.MethodPost, journalAPIPrefix+"/posts", editor, map[string]any{"title": "Reference image", "content": ref}))
before := storedPost(t, env, refPost)
rec = env.expect(t, http.StatusUnprocessableEntity, http.MethodPost, path, editor, map[string]any{
"changes": []any{map[string]any{"post_id": refPost, "src": a, "alt": "new"}},
})
if !strings.Contains(rec.Body.String(), "changes.0.src") || !strings.Contains(rec.Body.String(), "not an inline image") {
t.Fatalf("reference-style answer %s", rec.Body.String())
}
if after := storedPost(t, env, refPost); after.Content != before.Content || after.ContentHTML != before.ContentHTML {
t.Fatalf("a refused markdown change wrote: %q / %q", after.Content, after.ContentHTML)
}
}
// TestBlogEditorRequestOptions (J1, UI-SPEC C28): the post and category