From e4f3fb2d2d114f6eaea6ec75a10b0a274f57a8a1 Mon Sep 17 00:00:00 2001 From: SwastikTripathi <88632085+SwastikTripathi@users.noreply.github.com> Date: Mon, 9 Nov 2026 06:07:41 +0530 Subject: [PATCH] fix(markdown): keep a space at hard line breaks in snippets and count characters when stopping early (#6534) --- markdown/markdown.go | 46 ++++++++++++++++++++++++++------------- markdown/markdown_test.go | 18 +++++++++++++++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/markdown/markdown.go b/markdown/markdown.go index 12d7aca44..b6c7039c1 100644 --- a/markdown/markdown.go +++ b/markdown/markdown.go @@ -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 }) diff --git a/markdown/markdown_test.go b/markdown/markdown_test.go index 43f4f0b6b..d8694ee81 100644 --- a/markdown/markdown_test.go +++ b/markdown/markdown_test.go @@ -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')`",