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:
@@ -13,7 +13,8 @@ import (
|
||||
// or self-closing tag changes, and every other byte (attributes, text,
|
||||
// comments, the content of script or style) is copied as it is. Text that
|
||||
// merely looks like a tag inside raw-text elements or comments is not a
|
||||
// tag and is not touched. html without an h1 comes back unchanged with 0.
|
||||
// tag and is not touched. html without an h1 comes back unchanged with 0,
|
||||
// and the bytes of an unfinished tag at the end of html are kept.
|
||||
func ConvertH1(html string) (string, int) {
|
||||
if !containsFold(html, "<h1") && !containsFold(html, "</h1") {
|
||||
return html, 0
|
||||
@@ -22,12 +23,17 @@ func ConvertH1(html string) (string, int) {
|
||||
var b bytes.Buffer
|
||||
b.Grow(len(html))
|
||||
count := 0
|
||||
consumed := 0
|
||||
for {
|
||||
tt := z.Next()
|
||||
if tt == xhtml.ErrorToken {
|
||||
// The input may end inside something the tokenizer reads as a
|
||||
// tag ("a <b"): those bytes are kept as they are.
|
||||
b.WriteString(html[min(consumed, len(html)):])
|
||||
break
|
||||
}
|
||||
raw := z.Raw()
|
||||
consumed += len(raw)
|
||||
switch tt {
|
||||
case xhtml.StartTagToken, xhtml.SelfClosingTagToken:
|
||||
if name, _ := z.TagName(); string(name) == "h1" && len(raw) >= 3 {
|
||||
@@ -63,11 +69,18 @@ func containsFold(s, sub string) bool {
|
||||
|
||||
// ConvertContentH1 applies ConvertH1 to a post body and its rendered HTML
|
||||
// (content and content_html) and returns both with the number of headings
|
||||
// converted: content_html's count, or content's when content_html had none
|
||||
// (a markdown body has no h1 tag, its rendered HTML does).
|
||||
// converted: content_html's count, or content's when content_html had none.
|
||||
// content is rewritten only when it is HTML (LooksLikeHTML, the rich
|
||||
// editor's body): a markdown body is never rewritten, so a code sample that
|
||||
// shows an <h1> keeps it, and only its rendered content_html is converted
|
||||
// (every render path derives content_html from the markdown again and
|
||||
// converts it again).
|
||||
func ConvertContentH1(content, contentHTML string) (string, string, int) {
|
||||
content, nContent := ConvertH1(content)
|
||||
contentHTML, nHTML := ConvertH1(contentHTML)
|
||||
if !LooksLikeHTML(content) {
|
||||
return content, contentHTML, nHTML
|
||||
}
|
||||
content, nContent := ConvertH1(content)
|
||||
if nHTML > 0 {
|
||||
return content, contentHTML, nHTML
|
||||
}
|
||||
@@ -76,8 +89,9 @@ func ConvertContentH1(content, contentHTML string) (string, string, int) {
|
||||
|
||||
// ConvertTranslationH1 applies ConvertContentH1 to the string content and
|
||||
// content_html of one locale's translated attributes, in place, and returns
|
||||
// the number of headings converted. Other attributes and values that are
|
||||
// not strings are left alone.
|
||||
// the number of headings converted. A markdown content is never rewritten
|
||||
// (ConvertContentH1). Other attributes and values that are not strings are
|
||||
// left alone.
|
||||
func ConvertTranslationH1(attrs map[string]any) int {
|
||||
if attrs == nil {
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user