Skip to content

Commit 3831b5e

Browse files
committed
Edit codeBlockRegexp
1 parent c9104e7 commit 3831b5e

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

util/common.go

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88
"strconv"
99
"strings"
10+
"unicode"
1011
)
1112

1213
// Md5 生成32位MD5
@@ -118,3 +119,17 @@ func StringSplit(str string, sep string) []string {
118119
}
119120
return words
120121
}
122+
123+
func GetLeadingSpaces(s string) (string, int) {
124+
count := 0
125+
var bs []int32
126+
for _, c := range s {
127+
if unicode.IsSpace(c) {
128+
bs = append(bs, c)
129+
count++
130+
} else {
131+
break
132+
}
133+
}
134+
return string(bs), count
135+
}

util/content_fmt.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ var (
2929
aTagRegexp = regexp.MustCompile(`(?m)(<a[^<]+?>.*?</a>)`)
3030
hrefRegexp = regexp.MustCompile(`href="[^"]+?"`) // 图片地址被 auto link
3131
LocalImgRegexp = regexp.MustCompile(`(?:\s|^)(/static/upload/([a-z0-9]+)\.(jpg|jpe|jpeg|gif|png))\s?`) // 本地上传的图片
32-
codeBlockRegexp = regexp.MustCompile("(?s:(`{3} *([^\n]+)?\n(.+?)\n`{3}))")
32+
codeBlockRegexp = regexp.MustCompile("(?s)```([\\s\\S]*?)```") //"(?s)```(.*?)```"
3333
langCaptionRegexp = regexp.MustCompile("([^\\s`]+)\\s*(.+)?")
34-
t4Re = regexp.MustCompile(`\A( {4}|\t)`)
35-
t4Re2 = regexp.MustCompile(`^( {4}|\t)`)
3634
htmlRe = regexp.MustCompile("<.*?>|&.*?;")
3735
MdImgRe = regexp.MustCompile(`(!\[.*]\(.{10,}\))|([\w./:]*/static/upload/([a-z\d-.]+)\.(jpg|jpe|jpeg|gif|png))`)
3836
)
@@ -103,7 +101,7 @@ func ContentFmt(input string) string {
103101
codeRawMap := map[string]string{} // 代码块
104102
if HasCodeBlock(input) {
105103
input = codeBlockRegexp.ReplaceAllStringFunc(input, func(s string) string {
106-
s = strings.TrimSpace(s) // important
104+
s = s[3 : len(s)-3] // 去掉两头 ```
107105
// 获取并代码头部信息及处理代码高亮 html 代码
108106
lines := StringSplit(s, "\n")
109107
// 至少 3 行
@@ -116,20 +114,22 @@ func ContentFmt(input string) string {
116114
caption = v[2] // fmt.Sprintf(`<figcaption><span>%s</span></figcaption>`, v[2])
117115
}
118116
// 最后一行 ``` 舍弃
117+
// 若每行都有相同的空格则去掉
118+
prefixSpace, n := GetLeadingSpaces(lines[1])
119+
if n > 0 {
120+
for i := 1; i < len(lines)-1; i++ {
121+
if len(lines[i]) >= n && lines[i][:n] == prefixSpace {
122+
lines[i] = lines[i][n:]
123+
}
124+
}
125+
}
119126
// 纯代码
120127
codeRaw := strings.Join(lines[1:len(lines)-1], "\n")
121-
// 替换掉每行多余的空格
122-
if t4Re.MatchString(codeRaw) {
123-
codeRaw = t4Re2.ReplaceAllString(codeRaw, "")
124-
}
125128

126129
source := []string{`<figure class="code">`}
127130

128131
langName, hlText := ColorCode(codeRaw, lang)
129132

130-
//if len(caption) > 0 {
131-
// source = append(source, caption)
132-
//}
133133
if len(langName) > 0 || len(caption) > 0 {
134134
source = append(source, `<figcaption><span>`+langName+": "+caption+`</span></figcaption>`)
135135
}
@@ -140,7 +140,7 @@ func ContentFmt(input string) string {
140140
codeTag := codeBlockTag + strconv.Itoa(len(codeRawMap)) + "]"
141141
codeRawMap[codeTag] = strings.Join(source, "\n")
142142

143-
return codeTag
143+
return "\n\n" + codeTag + "\n\n"
144144
}
145145

146146
return s

0 commit comments

Comments
 (0)