fix: H1 conversion leaves markdown and unfinished tags alone
ConvertContentH1 rewrites content only when it is HTML; a markdown source, its code samples included, is kept byte for byte and only its rendered content_html is converted, on API saves, imports, journal:fix-headings and translations. ConvertH1 and SetImageAlt keep the bytes of an unfinished tag at the end of the input.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package classes
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestConvertH1 pins ConvertH1's byte rule: only h1 tag names change.
|
||||
func TestConvertH1(t *testing.T) {
|
||||
@@ -27,6 +30,16 @@ func TestConvertH1(t *testing.T) {
|
||||
t.Errorf("ConvertH1(%q) = %q, %d; want %q, %d", c.in, got, n, c.want, c.n)
|
||||
}
|
||||
}
|
||||
// L1 WR-03: input without a match comes back identical, and an h1
|
||||
// followed by an unfinished tag keeps the tail.
|
||||
for _, in := range []string{"p a <b", "x<y", "<p>a <b class=\"c", "a < b"} {
|
||||
if out, n := ConvertH1(in); out != in || n != 0 {
|
||||
t.Errorf("ConvertH1(%q) = %q %d", in, out, n)
|
||||
}
|
||||
}
|
||||
if out, n := ConvertH1("<h1>x</h1><p>a <b"); out != "<h2>x</h2><p>a <b" || n != 1 {
|
||||
t.Errorf("unfinished tail = %q %d", out, n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConvertContentH1 pins the body pair: content_html's count when its
|
||||
@@ -47,6 +60,25 @@ func TestConvertContentH1(t *testing.T) {
|
||||
if CountH1(`<h1>a</h1><div><h1 class="x">b</h1></div><h2>c</h2>`) != 2 || CountH1("") != 0 {
|
||||
t.Fatal("CountH1 is wrong")
|
||||
}
|
||||
// L1 CR-02: markdown with a fenced html sample and an inline code h1 is
|
||||
// kept byte for byte; its rendered HTML has the real heading converted
|
||||
// and the code still escaped.
|
||||
md := "# Title\n\nExample:\n\n```html\n<h1>Hello</h1>\n```\n\nInline `<h1>` tag."
|
||||
rendered, err := FormatHTML(md)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, h, n = ConvertContentH1(md, rendered)
|
||||
if c != md {
|
||||
t.Fatalf("the markdown source was rewritten:\n%s", c)
|
||||
}
|
||||
if n != 1 || !strings.Contains(h, "<h2") || strings.Contains(h, "<h1") || !strings.Contains(h, "<h1>Hello</h1>") || !strings.Contains(h, "<code><h1></code>") {
|
||||
t.Fatalf("rendered HTML = %q n=%d", h, n)
|
||||
}
|
||||
// Markdown without rendered HTML: nothing changes, nothing counted.
|
||||
if c, h, n := ConvertContentH1("plain <h1>x</h1> text", ""); c != "plain <h1>x</h1> text" || h != "" || n != 0 {
|
||||
t.Fatalf("markdown without HTML = %q %q %d", c, h, n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConvertTranslationH1 pins a locale's translated attributes,
|
||||
@@ -65,4 +97,9 @@ func TestConvertTranslationH1(t *testing.T) {
|
||||
if ConvertTranslationH1(nil) != 0 || ConvertTranslationH1(map[string]any{"title": "x"}) != 0 {
|
||||
t.Fatal("nothing to convert")
|
||||
}
|
||||
md := "Sample:\n\n```html\n<h1>Hi</h1>\n```\n"
|
||||
tr := map[string]any{"content": md, "content_html": "<pre><code><h1>Hi</h1></code></pre>"}
|
||||
if n := ConvertTranslationH1(tr); n != 0 || tr["content"] != md {
|
||||
t.Fatalf("a markdown translation was rewritten: %v n=%d", tr, n)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user