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:
@@ -1,6 +1,9 @@
|
||||
package classes
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestSetImageAlt pins the alt rewrite: only the matching img tag is
|
||||
// rewritten from its token, the alt is escaped, a missing alt is added and
|
||||
@@ -54,3 +57,53 @@ func TestSetImageAltEdges(t *testing.T) {
|
||||
t.Errorf("match with an unfinished tail = %q %d", out, n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetMarkdownImageAlt pins P33-65 (L1 WR-05): inline images with or
|
||||
// without a title get the alt; a destination written unescaped matches the
|
||||
// src goldmark renders for it; an alt with ] and a line break is escaped
|
||||
// and folded; the same image in a fence or a code span is left alone; a
|
||||
// reference-style image is not matched.
|
||||
func TestSetMarkdownImageAlt(t *testing.T) {
|
||||
out, n, old := SetMarkdownImageAlt("A  and  and .", "/a.jpg", "Lion's mane")
|
||||
if n != 2 || old == nil || *old != "" || out != "A  and  and ." {
|
||||
t.Fatalf("plain = %q %d %v", out, n, old)
|
||||
}
|
||||
html, err := FormatHTML("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(html, `src="/a%20b.jpg"`) {
|
||||
t.Fatalf("goldmark src = %s", html)
|
||||
}
|
||||
if out, n, _ := SetMarkdownImageAlt("", "/a%20b.jpg", "y"); n != 1 || out != "" {
|
||||
t.Fatalf("escaped src = %q %d", out, n)
|
||||
}
|
||||
if out, n, _ := SetMarkdownImageAlt("", "/%C5%BC.jpg", "y"); n != 1 || out != "" {
|
||||
t.Fatalf("non-ASCII src = %q %d", out, n)
|
||||
}
|
||||
if out, _, _ := SetMarkdownImageAlt("", "/a.jpg", "a ] b\nc [d] \\e"); out != `![a \] b c \[d\] \\e](/a.jpg)` {
|
||||
t.Fatalf("escaped alt = %q", out)
|
||||
}
|
||||
code := "```\n\n```\n\nInline `` and\n\n \n"
|
||||
if out, n, _ := SetMarkdownImageAlt(code, "/a.jpg", "y"); n != 0 || out != code {
|
||||
t.Fatalf("code = %q %d", out, n)
|
||||
}
|
||||
ref := "![x][a]\n\n[a]: /a.jpg\n"
|
||||
if out, n, _ := SetMarkdownImageAlt(ref, "/a.jpg", "y"); n != 0 || out != ref {
|
||||
t.Fatalf("reference = %q %d", out, n)
|
||||
}
|
||||
// The rendered alt follows the markdown.
|
||||
out, _, _ = SetMarkdownImageAlt("Text\n\n\n", "/a.jpg", "Grzyb [leśny]")
|
||||
if html, _ := FormatHTML(out); !strings.Contains(html, `alt="Grzyb [leśny]"`) {
|
||||
t.Fatalf("rendered = %s", html)
|
||||
}
|
||||
|
||||
// SetImageAltOutsideCode: a raw img gets the alt, one in a code sample
|
||||
// keeps its bytes.
|
||||
raw := "<img src=\"/a.jpg\">\n\n```html\n<img src=\"/a.jpg\">\n```\n\nand `<img src=\"/a.jpg\">`\n"
|
||||
got, n, _ := SetImageAltOutsideCode(raw, "/a.jpg", "z")
|
||||
want := "<img src=\"/a.jpg\" alt=\"z\">\n\n```html\n<img src=\"/a.jpg\">\n```\n\nand `<img src=\"/a.jpg\">`\n"
|
||||
if n != 1 || got != want {
|
||||
t.Fatalf("outside code = %q %d", got, n)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user