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:
Jakub Zych
2026-11-04 23:14:42 +01:00
parent 45d66f6071
commit 0e739c882e
6 changed files with 123 additions and 11 deletions

View File

@@ -13,8 +13,8 @@ import (
// (nil when it had none). It works on the x/net/html tokenizer: a matching
// img tag is written again from its token, so every attribute value,
// alt included, is escaped by the tokenizer and never concatenated; every
// other byte of html is copied as it is. An empty alt marks a decorative
// image (alt="").
// other byte of html is copied as it is, the bytes of an unfinished tag at
// the end included. An empty alt marks a decorative image (alt="").
func SetImageAlt(html, src, alt string) (string, int, *string) {
if src == "" || !containsFold(html, "<img") {
return html, 0, nil
@@ -24,12 +24,16 @@ func SetImageAlt(html, src, alt string) (string, int, *string) {
b.Grow(len(html) + len(alt))
matches := 0
var old *string
consumed := 0
for {
tt := z.Next()
if tt == xhtml.ErrorToken {
// The bytes of an unfinished tag at the end are kept.
b.WriteString(html[min(consumed, len(html)):])
break
}
raw := z.Raw()
consumed += len(raw)
if tt == xhtml.StartTagToken || tt == xhtml.SelfClosingTagToken {
// Raw is copied before Token, which reads the tag; Token is
// the only reader of the tag (TagName would consume it).