Open qiuker521 opened 5 years ago
Here is the minimal runnable code:
package main
import (
"gopkg.in/russross/blackfriday.v2"
"io/ioutil"
"log"
"net/http"
)
func Markdown2Html(content string) string {
output := blackfriday.Run([]byte(content), blackfriday.WithNoExtensions())
return string(output)
}
func main() {
a := Get("https://raw.githubusercontent.com/Zuozuohao/Zuozuohao.github.io/master/_posts/2016-06-16-Object-Oriented-Inheritance-in-Go.md")
//comment this and this issue happens
//a = strings.Replace(a, "\n", "\r\n", -1)
log.Println(Markdown2Html(string(a)))
}
func Get(urls string) string {
resp, err := http.Get(urls)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}
Interestingly, I have almost the exact opposite situation.
I'm dealing with some data stored with Windows/DOS-style EOL (\r\n
) and paragraphs were not being created until I strings.Replace(content, "\r\n", "\n", -1)
.
I0315 15:29:23.876900 14327 serve.go:388] INPUT: A
B
Please ignore
I0315 15:29:23.877727 14327 serve.go:389] INPUT HEX: 410d0a0d0a420d0a0d0a506c656173652069676e6f7265
I0315 15:29:23.877960 14327 serve.go:390] OUTPUT: <p>A
B
Please ignore</p>
After the swap:
I0315 15:31:18.472258 15069 serve.go:388] INPUT: A
B
Please ignore
I0315 15:31:18.475149 15069 serve.go:389] INPUT HEX: 410a0a420a0a506c656173652069676e6f7265
I0315 15:31:18.475919 15069 serve.go:390] OUTPUT: <p>A</p>
<p>B</p>
<p>Please ignore</p>
Observe how the paragraphs only exist with the 0x0a 0x0a
sequence, but not with the 0x0d 0x0a 0x0d 0x0a
.
Relevant chunk:
// post.ContentFiltered contains raw markdown.
input := []byte(strings.Replace(post.ContentFiltered, "\r\n", "\n", -1))
output := blackfriday.Run(input)
glog.Infof("INPUT: %s", string(input))
glog.Infof("INPUT HEX: %s", hex.EncodeToString(output))
glog.Infof("OUTPUT: %s", string(output))
here are two types of code:
returns
however, the code:
returns the good format: