Skip to content

Commit 17e499b

Browse files
committed
Merge branch 'feat/1.1.2/question-title' into test
2 parents 0f26ab5 + 5d28954 commit 17e499b

6 files changed

Lines changed: 36 additions & 70 deletions

File tree

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ go 1.18
44

55
require (
66
github.com/Chain-Zhang/pinyin v0.1.3
7+
github.com/Machiel/slugify v1.0.1
78
github.com/Masterminds/semver/v3 v3.1.1
89
github.com/anargu/gin-brotli v0.0.0-20220116052358-12bf532d5267
910
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
1011
github.com/bwmarrin/snowflake v0.3.0
11-
github.com/davecgh/go-spew v1.1.1
1212
github.com/disintegration/imaging v1.6.2
1313
github.com/gin-gonic/gin v1.9.1
1414
github.com/go-playground/locales v0.14.1
@@ -19,7 +19,6 @@ require (
1919
github.com/golang/mock v1.6.0
2020
github.com/google/uuid v1.3.0
2121
github.com/google/wire v0.5.0
22-
github.com/gosimple/slug v1.13.1
2322
github.com/grokify/html-strip-tags-go v0.0.1
2423
github.com/jinzhu/copier v0.3.5
2524
github.com/jinzhu/now v1.1.5
@@ -63,6 +62,7 @@ require (
6362
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
6463
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
6564
github.com/containerd/continuity v0.3.0 // indirect
65+
github.com/davecgh/go-spew v1.1.1 // indirect
6666
github.com/docker/cli v20.10.14+incompatible // indirect
6767
github.com/docker/docker v20.10.7+incompatible // indirect
6868
github.com/docker/go-connections v0.4.0 // indirect
@@ -86,7 +86,6 @@ require (
8686
github.com/golang/snappy v0.0.4 // indirect
8787
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8888
github.com/gorilla/css v1.0.0 // indirect
89-
github.com/gosimple/unidecode v1.0.1 // indirect
9089
github.com/hashicorp/hcl v1.0.0 // indirect
9190
github.com/imdario/mergo v0.3.12 // indirect
9291
github.com/inconshreveable/mousetrap v1.0.1 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
5252
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
5353
github.com/LinkinStars/go-i18n/v2 v2.2.2 h1:ZfjpzbW13dv6btv3RALKZkpN9A+7K1JA//2QcNeWaxU=
5454
github.com/LinkinStars/go-i18n/v2 v2.2.2/go.mod h1:hLglSJ4/3M0Y7ZVcoEJI+OwqkglHCA32DdjuJJR2LbM=
55+
github.com/Machiel/slugify v1.0.1 h1:EfWSlRWstMadsgzmiV7d0yVd2IFlagWH68Q+DcYCm4E=
56+
github.com/Machiel/slugify v1.0.1/go.mod h1:fTFGn5uWEynW4CUMG7sWkYXOf1UgDxyTM3DbR6Qfg3k=
5557
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
5658
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
5759
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
@@ -340,10 +342,6 @@ github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl
340342
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
341343
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
342344
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
343-
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
344-
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
345-
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
346-
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
347345
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
348346
github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
349347
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=

pkg/htmltext/htmltext.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package htmltext
22

33
import (
4+
"github.com/Chain-Zhang/pinyin"
5+
"github.com/answerdev/answer/pkg/checker"
46
"io"
57
"net/http"
68
"net/url"
79
"regexp"
810
"strings"
911

10-
"github.com/gosimple/slug"
12+
"github.com/Machiel/slugify"
1113
strip "github.com/grokify/html-strip-tags-go"
1214
)
1315

@@ -46,14 +48,15 @@ func ClearText(html string) (text string) {
4648
}
4749

4850
func UrlTitle(title string) (text string) {
49-
title = ClearEmoji(title)
50-
title = slug.Make(title)
51-
// title = strings.ReplaceAll(title, " ", "-")
51+
title = convertChinese(title)
52+
title = clearEmoji(title)
53+
title = slugify.Slugify(title)
5254
title = url.QueryEscape(title)
55+
title = cutLongTitle(title)
5356
return title
5457
}
5558

56-
func ClearEmoji(s string) string {
59+
func clearEmoji(s string) string {
5760
ret := ""
5861
rs := []rune(s)
5962
for i := 0; i < len(rs); i++ {
@@ -64,6 +67,25 @@ func ClearEmoji(s string) string {
6467
return ret
6568
}
6669

70+
func convertChinese(content string) string {
71+
has := checker.IsChinese(content)
72+
if !has {
73+
return content
74+
}
75+
str, err := pinyin.New(content).Split("-").Mode(pinyin.WithoutTone).Convert()
76+
if err != nil {
77+
return content
78+
}
79+
return str
80+
}
81+
82+
func cutLongTitle(title string) string {
83+
if len(title) > 150 {
84+
return title[0:150]
85+
}
86+
return title
87+
}
88+
6789
// FetchExcerpt return the excerpt from the HTML string
6890
func FetchExcerpt(html, trimMarker string, limit int) (text string) {
6991
if len(html) == 0 {

pkg/htmltext/htmltext_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package htmltext
22

33
import (
4+
"fmt"
45
"testing"
56

6-
"github.com/davecgh/go-spew/spew"
77
"github.com/stretchr/testify/assert"
88
)
99

@@ -63,6 +63,6 @@ func TestUrlTitle(t *testing.T) {
6363
}
6464
for _, title := range list {
6565
formatTitle := UrlTitle(title)
66-
spew.Dump(formatTitle)
66+
fmt.Println(formatTitle)
6767
}
6868
}

pkg/uid/sid.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
const salt = int64(100)
1010

11-
// NumToString num to string
11+
// NumToShortID num to string
1212
func NumToShortID(id int64) string {
1313
sid := strconv.FormatInt(id, 10)
1414
if len(sid) < 17 {
@@ -26,10 +26,10 @@ func NumToShortID(id int64) string {
2626
}
2727
code := utils.EnShortID(id, salt)
2828
tcode := utils.EnShortID(typeCode, salt)
29-
return string(tcode) + string(code)
29+
return tcode + code
3030
}
3131

32-
// StringToNum string to num
32+
// ShortIDToNum string to num
3333
func ShortIDToNum(code string) int64 {
3434
if len(code) < 2 {
3535
return 0

pkg/uid/sid_test.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)