yuin / goldmark

:trophy: A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.
MIT License
3.68k stars 255 forks source link

Omitting the final newline in code blocks #440

Closed silverwind closed 9 months ago

silverwind commented 9 months ago

Goldmark renders a \n at the end of <pre><code> blocks but this newline is undesirable in some cases. For example:

package main

import (
  "bytes"
  "github.com/yuin/goldmark"
)

func main() {
  md := goldmark.New()
  var buf bytes.Buffer
  if err := md.Convert([]byte("```\ntest\n```"), &buf); err != nil {
    panic(err)
  }
  println(buf.String())
}

will render

<pre><code>test
</code></pre>

On GitHub, the same source renders without this newline (ignore the different wrapping tags):

<pre>test</pre>

While the presence or absence of this newline makes no difference in the visual HTML, it does make a difference when JavaScript reads the content of the element, for example for the purpose of copying to clipboard.

How about adding a parser option to ignore/remove the final newline? Or is stripping the newline possible already somehow?

yuin commented 9 months ago

Don't ignore issue template

silverwind commented 9 months ago

Yeah it's not a valid issue because GoldMark rendering matches the spec, but GitHub diverges. I should have made a discussion. Still, it might a behaviour to consider for the GFM rendering, assuming the goal is to render like GitHub.

silverwind commented 9 months ago

Related CommonMark issue: https://github.com/commonmark/commonmark-spec/issues/501