fix(markdown): keep a space at hard line breaks in snippets and count characters when stopping early (#6534)

This commit is contained in:
SwastikTripathi
2026-11-09 06:07:41 +05:30
committed by GitHub
parent 9a08a7334f
commit e4f3fb2d2d
2 changed files with 49 additions and 15 deletions
+31 -15
View File
@@ -7,6 +7,7 @@ import (
"regexp"
"slices"
"strings"
"unicode/utf8"
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
@@ -294,10 +295,20 @@ func (s *service) GenerateSnippet(content []byte, maxLength int) (string, error)
}
var buf strings.Builder
var runeCount int
var lastNodeWasBlock bool
// The first line of a GitHub alert, such as "[!NOTE]", which renders as the callout's title.
var alertMarker text.Segment
writeBytes := func(b []byte) {
buf.Write(b)
runeCount += utf8.RuneCount(b)
}
writeByte := func(c byte) {
buf.WriteByte(c)
runeCount++
}
err = gast.Walk(root, func(n gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
// Skip code blocks entirely (but keep inline code spans for snippet text)
@@ -316,7 +327,7 @@ func (s *service) GenerateSnippet(content []byte, maxLength int) (string, error)
switch n.Kind() {
case gast.KindParagraph, gast.KindHeading, gast.KindListItem, east.KindTableCell, east.KindTableRow, east.KindTableHeader:
if buf.Len() > 0 && lastNodeWasBlock {
buf.WriteByte(' ')
writeByte(' ')
}
lastNodeWasBlock = false
default:
@@ -345,42 +356,47 @@ func (s *service) GenerateSnippet(content []byte, maxLength int) (string, error)
}()
// Extract text from various node types
skipChildren := false
switch node := n.(type) {
case *gast.Text:
segment := node.Segment
if alertMarker.Len() > 0 && segment.Start >= alertMarker.Start && segment.Stop <= alertMarker.Stop {
return gast.WalkContinue, nil
}
buf.Write(segment.Value(content))
if node.SoftLineBreak() {
buf.WriteByte(' ')
writeBytes(segment.Value(content))
if node.SoftLineBreak() || node.HardLineBreak() {
writeByte(' ')
}
case *gast.AutoLink:
buf.Write(node.URL(content))
return gast.WalkSkipChildren, nil
writeBytes(node.URL(content))
skipChildren = true
case *mast.TagNode:
if len(node.Source) > 0 {
buf.Write(node.Source)
writeBytes(node.Source)
} else {
buf.WriteByte('#')
buf.Write(node.Tag)
writeByte('#')
writeBytes(node.Tag)
}
case *mast.GFMEmailNode:
buf.Write(node.Address)
writeBytes(node.Address)
case *mast.InlineMathNode:
buf.Write(node.Source)
writeBytes(node.Source)
case *mast.BlockMathNode:
buf.Write(node.Source)
return gast.WalkSkipChildren, nil
writeBytes(node.Source)
skipChildren = true
default:
// Ignore other node types.
}
// Stop walking if we've exceeded double the max length
// (we'll truncate precisely later)
if buf.Len() > maxLength*2 {
// (we'll truncate precisely later). Count runes, not bytes,
// like truncateAtWord does, so multi-byte text isn't cut short.
if runeCount > maxLength*2 {
return gast.WalkStop, nil
}
if skipChildren {
return gast.WalkSkipChildren, nil
}
return gast.WalkContinue, nil
})
+18
View File
@@ -97,6 +97,24 @@ func TestGenerateSnippet(t *testing.T) {
maxLength: 100,
expected: "Item 1 Item 2 Item 3",
},
{
name: "hard line break with trailing spaces",
content: "first line \nsecond line",
maxLength: 100,
expected: "first line second line",
},
{
name: "hard line break with backslash",
content: "first line\\\nsecond line",
maxLength: 100,
expected: "first line second line",
},
{
name: "truncate multi-byte text across paragraphs",
content: strings.Repeat("日", 50) + "\n\n" + strings.Repeat("本", 20),
maxLength: 64,
expected: strings.Repeat("日", 50) + " ...",
},
{
name: "inline code preserved",
content: "`console.log('hello')`",