-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyUtils.go
43 lines (39 loc) · 1.13 KB
/
myUtils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package utils
import (
"bytes"
"crypto/md5"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/russross/blackfriday"
"github.com/sourcegraph/syntaxhighlight"
"html/template"
"time"
)
func MD5(str string) string {
md5str := fmt.Sprintf("%x", md5.Sum([]byte(str)))
return md5str
}
//将传入的时间戳转为时间
func SwitchTimeStampToData(timeStamp int64) string {
t := time.Unix(timeStamp, 0)
return t.Format("2006-01-02 15:04:05")
}
func SwitchMarkdownToHtml(content string) template.HTML {
markdown := blackfriday.MarkdownCommon([]byte(content))
//获取到html文档
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(markdown))
/**
对document进程查询,选择器和css的语法一样
第一个参数:i是查询到的第几个元素
第二个参数:selection就是查询到的元素
*/
doc.Find("code").Each(func(i int, selection *goquery.Selection) {
light, _ := syntaxhighlight.AsHTML([]byte(selection.Text()))
selection.SetHtml(string(light))
fmt.Println(selection.Html())
fmt.Println("light:", string(light))
fmt.Println("\n\n\n")
})
htmlString, _ := doc.Html()
return template.HTML(htmlString)
}