Skip to content

Commit bcedfe7

Browse files
committed
1 parent 02f43f0 commit bcedfe7

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎main.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func init() {
5050
logger = log.NewLogger(os.Stdout)
5151

5252
util.LoadConf()
53+
util.LoadMakrdown()
5354
i18n.Load()
5455
theme.Load()
5556
replaceServerConf()

‎util/markdowns.go‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"crypto/md5"
2121
"regexp"
2222
"strings"
23+
"time"
2324
"unicode"
2425
"unicode/utf8"
2526

2627
"github.com/PuerkitoBio/goquery"
2728
"github.com/bluele/gcache"
2829
"github.com/hackebrot/turtle"
2930
"github.com/microcosm-cc/bluemonday"
31+
"github.com/parnurzeal/gorequest"
3032
"github.com/vinta/pangu"
3133
"gopkg.in/russross/blackfriday.v2"
3234
)
@@ -39,6 +41,37 @@ type MarkdownResult struct {
3941
ThumbURL string
4042
}
4143

44+
var markedAvailable = false
45+
46+
func LoadMakrdown() {
47+
_, content, errs := gorequest.New().Timeout(2 * time.Second).Post("http://localhost:8250").
48+
SendString("Pipe 大法好").End()
49+
if nil != errs {
50+
logger.Info("[marked] is not available, uses built-in [blackfriday] for markdown processing")
51+
52+
return
53+
}
54+
55+
markedAvailable = "<p>Pipe 大法好</p>" == content
56+
if markedAvailable {
57+
logger.Debug("[marked] is available, uses it for markdown processing")
58+
} else {
59+
logger.Debug("[marked] is not available, uses built-in [blackfriday] for markdown processing")
60+
}
61+
}
62+
63+
func marked(mdText string) []byte {
64+
_, ret, errs := gorequest.New().Timeout(500 * time.Microsecond).Post("http://localhost:8250").
65+
SendString(mdText).End()
66+
if nil != errs {
67+
logger.Errorf("markdown failed with [marked]: " + errs[0].Error())
68+
69+
return []byte("")
70+
}
71+
72+
return []byte(ret)
73+
}
74+
4275
func Markdown(mdText string) *MarkdownResult {
4376
digest := md5.New()
4477
digest.Write([]byte(mdText))
@@ -50,8 +83,13 @@ func Markdown(mdText string) *MarkdownResult {
5083
}
5184

5285
mdText = emojify(mdText)
53-
mdTextBytes := []byte(mdText)
54-
unsafe := blackfriday.Run(mdTextBytes)
86+
var unsafe []byte
87+
if markedAvailable {
88+
unsafe = marked(mdText)
89+
} else {
90+
mdTextBytes := []byte(mdText)
91+
unsafe = blackfriday.Run(mdTextBytes)
92+
}
5593
contentHTML := string(unsafe)
5694
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(contentHTML))
5795
doc.Find("img").Each(func(i int, ele *goquery.Selection) {

0 commit comments

Comments
 (0)