Skip to content

Commit 2796300

Browse files
committed
11/17
1 parent 0fb3be7 commit 2796300

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

modules/markup/markdown/markdown.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
169169
return
170170
}
171171

172+
// Log via gitea log package and also print to stderr for debugging when
173+
// running a local reproduction program (some test helpers may suppress log writers).
172174
log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
173175
escapedHTML := template.HTMLEscapeString(giteautil.UnsafeBytesToString(buf))
174176
_, _ = output.Write(giteautil.UnsafeStringToBytes(escapedHTML))
@@ -181,6 +183,44 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
181183

182184
rc := &RenderConfig{Meta: markup.RenderMetaAsDetails}
183185
buf, _ = ExtractMetadataBytes(buf, rc)
186+
/*
187+
// Only attempt to extract YAML front matter when the first line is a YAML separator
188+
// AND there is a matching closing separator later in the content.
189+
// If there is only a leading '---' (no closing '---'), we must not try to
190+
// extract metadata because that will be treated as normal markdown (a
191+
// thematic break) and should not interfere with goldmark parsing.
192+
firstLineEnd := bytes.IndexByte(buf, '\n')
193+
if firstLineEnd == -1 {
194+
firstLineEnd = len(buf)
195+
}
196+
firstLine := buf[:firstLineEnd]
197+
if isYAMLSeparator(firstLine) {
198+
// scan for a later line that is also a YAML separator
199+
foundClosing := false
200+
start := firstLineEnd + 1
201+
for start < len(buf) {
202+
end := len(buf)
203+
if idx := bytes.IndexByte(buf[start:], '\n'); idx >= 0 {
204+
end = start + idx
205+
}
206+
line := buf[start:end]
207+
if isYAMLSeparator(line) {
208+
foundClosing = true
209+
break
210+
}
211+
start = end + 1
212+
}
213+
if foundClosing {
214+
buf, _ = ExtractMetadataBytes(buf, rc)
215+
} else {
216+
// If there's an opening YAML separator but no closing one, some versions
217+
// of the goldmark parser can panic when parsing inline/code spans.
218+
// Prepend a blank line so the leading '---' is treated as a thematic
219+
// break (horizontal rule) and not as frontmatter, matching the
220+
// behaviour when users insert an initial blank line.
221+
buf = append([]byte("\n"), buf...)
222+
}
223+
}*/
184224

185225
metaLength := max(bufWithMetadataLength-len(buf), 0)
186226
rc.metaLength = metaLength

modules/markup/markdown/markdown_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,22 @@ func TestMarkdownLink(t *testing.T) {
547547
<a href="#user-content-foo" rel="nofollow">link3</a></p>
548548
`, string(result))
549549
}
550+
551+
// Test for issue #35800: markdown rendering broken when markup starts with horizontal line
552+
func TestRender_StartsWithHorizontalLine(t *testing.T) {
553+
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
554+
555+
// Test: content starting with --- (without closing separator) should be treated as markdown, not YAML frontmatter
556+
input := `---
557+
Related:
558+
* #1
559+
560+
This should work`
561+
result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
562+
assert.NoError(t, err)
563+
// Should render as a horizontal line followed by content, not fail or lose content
564+
assert.Contains(t, string(result), "Related")
565+
assert.Contains(t, string(result), "#1")
566+
assert.Contains(t, string(result), "This should work")
567+
}
568+

modules/markup/markdown/meta_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ func TestExtractMetadataBytes(t *testing.T) {
8787
assert.Equal(t, metaTest, meta)
8888
assert.True(t, meta.Valid())
8989
})
90+
91+
// Test case for issue #35800: content starting with --- but no closing separator
92+
t.Run("StartsWithSeparatorButNoClosing", func(t *testing.T) {
93+
var meta IssueTemplate
94+
// This should return the original content as-is since no closing separator is found
95+
content := "---\nRelated:\n* #1\n\nThis should work"
96+
_, err := ExtractMetadataBytes([]byte(content), &meta)
97+
// ExtractMetadataBytes should return an error when there's no closing separator
98+
assert.Error(t, err)
99+
})
90100
}
91101

92102
var (

0 commit comments

Comments
 (0)