Skip to content

Commit 8ff4373

Browse files
committed
workaround
1 parent 2796300 commit 8ff4373

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

modules/markup/markdown/markdown.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package markdown
66

77
import (
8+
"bytes"
89
"errors"
910
"html/template"
1011
"io"
@@ -182,6 +183,7 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
182183
bufWithMetadataLength := len(buf)
183184

184185
rc := &RenderConfig{Meta: markup.RenderMetaAsDetails}
186+
// Extract metadata if present. If not present, ExtractMetadataBytes returns original contents and an error.
185187
buf, _ = ExtractMetadataBytes(buf, rc)
186188
/*
187189
// Only attempt to extract YAML front matter when the first line is a YAML separator
@@ -225,6 +227,42 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
225227
metaLength := max(bufWithMetadataLength-len(buf), 0)
226228
rc.metaLength = metaLength
227229

230+
// Workaround: goldmark may parse following lists as plain text if the original
231+
// input started with a hyphen-only line (e.g. "-" or "-------") but there
232+
// was no valid YAML front-matter. In that case, prefix the buffer with a
233+
// zero-width space so the parser doesn't treat the hyphen line specially.
234+
if rc.metaLength == 0 && len(buf) > 0 {
235+
// inspect the first line of the ORIGINAL contents (we have buf already set to body
236+
// so use the original length to find the original first line from input - but we no
237+
// longer have the original bytes. Instead, re-check the beginning of the current
238+
// buf: if it starts with a hyphen-only line, apply fix. This is safe because
239+
// ExtractMetadataBytes returned original contents if there was no metadata.
240+
idx := bytes.IndexByte(buf, '\n')
241+
firstLine := buf
242+
if idx >= 0 {
243+
firstLine = buf[:idx]
244+
}
245+
// check if firstLine contains only spaces and hyphens
246+
onlyHyphen := true
247+
foundHyphen := false
248+
for i := 0; i < len(firstLine); i++ {
249+
b := firstLine[i]
250+
if b == '-' {
251+
foundHyphen = true
252+
continue
253+
}
254+
if b == ' ' || b == '\t' || b == '\r' {
255+
continue
256+
}
257+
onlyHyphen = false
258+
break
259+
}
260+
if onlyHyphen && foundHyphen {
261+
// prefix zero-width space
262+
buf = append([]byte("\n"), buf...)
263+
}
264+
}
265+
228266
pc.Set(renderConfigKey, rc)
229267

230268
if err := converter.Convert(buf, lw, parser.WithContext(pc)); err != nil {

0 commit comments

Comments
 (0)