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

Markdown file with CRLF line endings will cause wrong-style output #423

Open
jinliming2 opened this issue Jan 4, 2018 · 7 comments
Open

Comments

@jinliming2
Copy link

Hi, When I started to use blackfriday to parse my Markdown file, I got a wrong-style output.
My Markdown file looks like this, with Windows-style CRLF (\r\n) line endings:

# Hello World

This is my content.

And I wrote the code like this in my project:

func main() {
	file, err := ioutil.ReadFile("./test.md")
	if err != nil {
		println(err)
	}
	println(string(file))
	println("---------------------------------------------")
	out := blackfriday.Run(file)
	println(string(out))
}

And now, when I ran my program, I got output in my console like this:

# Hello World

This is my content.

---------------------------------------------
</h1>ello World

<p>
</p> is my content.

Is this a problem in blackfriday?
Thanks.

@klingtnet
Copy link

I can confirm that text with Windows line endings (CLRF) is not handled correctly. My current workaround is to replace them before passing it to blackfriday:

markdownWithUnixLineEndings := strings.Replace(markdown, "\r\n", "\n", -1)
blackfriday.Run([]byte(markdownWithUnixLineEndings))

@rtfb
Copy link
Collaborator

rtfb commented Jan 18, 2018

Confirmed here as well. I have a WIP fix, but I'm not spending much time on Blackfriday lately, so haven't cleaned it up yet.

@rtfb
Copy link
Collaborator

rtfb commented Jan 18, 2018

Submitted #428, feel free to review and comment.

@guillep2k
Copy link

For those that need a solution and can't wait for the PR to come up, this is what we did at Gitea to make it work in the meantime: go-gitea/gitea#8925

Basically, we convert every \r or \r\n sequence to \n. We did some tests and we arrived to that algorithm as the fastest (save modifying the string in-situ, which is even faster).

@zeripath
Copy link

Here's the actual code:

https://github.com/go-gitea/gitea/blob/dc8036dcc680abab52b342d18181a5ee42f40318/modules/util/util.go#L68-L102

It just rips out all \r\n and \r replacing them with \n - so if for some perverse reason you actually intend there to be a raw \r in your markdown page it will become a newline - however it is fast.

@zeripath
Copy link

If you would prefer it replacing in place - then remove the definition of tmp and replace all references to tmp with input or vice versa. That would do it.

@TACIXAT
Copy link

TACIXAT commented May 17, 2020

I just do a bytes.Replace on the input, but this had me confused as for an hour or so.

gwd pushed a commit to gwd/session-scheduler that referenced this issue Apr 16, 2021
There were two problems with the rendering path.

First, HTML standard requires POSTed text to use CRLF, but blackfriday
processes this incorrectly (see
russross/blackfriday#423). Brute-force this
by replacing '\r\n' with '\n'.

Secondly, the template was displaying Description rather than
DescriptionHTML, and this was succeeding due to DiscussionFull being
the top-level type. Fix this.

Signed-off-by: George Dunlap <[email protected]>
mmb added a commit to mmb/tmpbbs that referenced this issue Oct 25, 2024
To fix fenced code blocks. blackfriday doesn't handle CRLF well.

See russross/blackfriday#423
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

6 participants