Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference-style links above document add line break #304

Open
cactysman opened this issue May 30, 2024 · 1 comment
Open

Reference-style links above document add line break #304

cactysman opened this issue May 30, 2024 · 1 comment

Comments

@cactysman
Copy link

Reference-style links placed on top of the document add a leading line break to the document.

Setup

  • OS: Debian
  • Shell: bash
  • Go version: 1.21.4
  • glamour version: 0.7.0

To Reproduce

  1. Run attached source code
  2. Look at output

Source Code

package main

import "github.com/charmbracelet/glamour"
import "fmt"

var md = "# Hello World\n\nThis is a [link][url]."

var linkAbove = "[url]: https://example.com\n" + md
var linkBelow = md + "\n\n[url]: https://example.com\n"

func main() {
    print("no link", md)
    print("link below", linkBelow)
    print("link above", linkAbove)
}

func print(title string, in string) {
    out, _ := glamour.Render(in, "dark")
    fmt.Print(title + ": <start>" + out + "<end>\n\n")
}

Expected behavior
The actual output of the markdown document should probably be trimmed, or at least not include that extra line break on top.

Screenshots
image

@cactysman
Copy link
Author

I also confirmed that it's not goldmark's fault with the following code (using a simple goldmark setup that should probably suffice) producing identical results no matter where the refs are placed.

package main

import (
    "bytes"
    "fmt"
    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark/extension"
    "github.com/yuin/goldmark/parser"
)

func main() {
    md := goldmark.New(
        goldmark.WithExtensions(
            extension.GFM,
            extension.DefinitionList,
        ),
        goldmark.WithParserOptions(
            parser.WithAutoHeadingID(),
        ),
    )

    var bufA bytes.Buffer
    var bufB bytes.Buffer
    sourceA := []byte("[a]: https://example.com\n\n# hi\n\n[link][a]")
    sourceB := []byte("# hi\n\n[link][a]\n\n[a]: https://example.com")

    if err := md.Convert(sourceA, &bufA); err != nil {
        panic(err)
    }
    fmt.Println("START1" + bufA.String() + "END1\n")

    if err := md.Convert(sourceB, &bufB); err != nil {
        panic(err)
    }
    fmt.Println("START2" + bufB.String() + "END2")
}
START1<h1 id="hi">hi</h1>
<p><a href="https://example.com">link</a></p>
END1

START2<h1 id="hi">hi</h1>
<p><a href="https://example.com">link</a></p>
END2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant