github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/blogweb_gin/utils/utils.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "crypto/md5" 6 "fmt" 7 "github.com/PuerkitoBio/goquery" 8 "github.com/russross/blackfriday" 9 "github.com/sourcegraph/syntaxhighlight" 10 "html/template" 11 "time" 12 ) 13 14 //传入的数据不一样,那么MD5后的32位长度的数据肯定会不一样 15 func MD5(str string) string { 16 md5str := fmt.Sprintf("%x", md5.Sum([]byte(str))) 17 return md5str 18 } 19 20 //将传入的时间戳转为时间 21 func SwitchTimeStampToData(timeStamp int64) string { 22 t := time.Unix(timeStamp, 0) 23 return t.Format("2006-01-02 15:04:05") 24 } 25 26 // md 转html 27 func SwitchMarkdownToHtml(content string) template.HTML { 28 markdown := blackfriday.MarkdownCommon([]byte(content)) 29 30 //获取到html文档 31 doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(markdown)) 32 /** 33 对document进程查询,选择器和css的语法一样 34 第一个参数:i是查询到的第几个元素 35 第二个参数:selection就是查询到的元素 36 */ 37 doc.Find("code").Each(func(i int, selection *goquery.Selection) { 38 light, _ := syntaxhighlight.AsHTML([]byte(selection.Text())) 39 selection.SetHtml(string(light)) 40 //logs.Info.Println(selection.Html()) 41 //logs.Info.Println("light:", string(light)) 42 //fmt.Println("\n\n\n") 43 }) 44 45 htmlString, _ := doc.Html() 46 return template.HTML(htmlString) 47 }