Skip to content

Commit

Permalink
feat(question): change create question title function
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Aug 18, 2023
1 parent 3c08f31 commit 0299c3b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 70 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.18

require (
github.com/Chain-Zhang/pinyin v0.1.3
github.com/Machiel/slugify v1.0.1
github.com/Masterminds/semver/v3 v3.1.1
github.com/anargu/gin-brotli v0.0.0-20220116052358-12bf532d5267
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/bwmarrin/snowflake v0.3.0
github.com/davecgh/go-spew v1.1.1
github.com/disintegration/imaging v1.6.2
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/locales v0.14.1
Expand All @@ -19,7 +19,6 @@ require (
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/google/wire v0.5.0
github.com/gosimple/slug v1.13.1
github.com/grokify/html-strip-tags-go v0.0.1
github.com/jinzhu/copier v0.3.5
github.com/jinzhu/now v1.1.5
Expand Down Expand Up @@ -63,6 +62,7 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.14+incompatible // indirect
github.com/docker/docker v20.10.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
Expand All @@ -86,7 +86,6 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/LinkinStars/go-i18n/v2 v2.2.2 h1:ZfjpzbW13dv6btv3RALKZkpN9A+7K1JA//2QcNeWaxU=
github.com/LinkinStars/go-i18n/v2 v2.2.2/go.mod h1:hLglSJ4/3M0Y7ZVcoEJI+OwqkglHCA32DdjuJJR2LbM=
github.com/Machiel/slugify v1.0.1 h1:EfWSlRWstMadsgzmiV7d0yVd2IFlagWH68Q+DcYCm4E=
github.com/Machiel/slugify v1.0.1/go.mod h1:fTFGn5uWEynW4CUMG7sWkYXOf1UgDxyTM3DbR6Qfg3k=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
Expand Down Expand Up @@ -340,10 +342,6 @@ github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
Expand Down
32 changes: 27 additions & 5 deletions pkg/htmltext/htmltext.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package htmltext

import (
"github.com/Chain-Zhang/pinyin"
"github.com/answerdev/answer/pkg/checker"
"io"
"net/http"
"net/url"
"regexp"
"strings"

"github.com/gosimple/slug"
"github.com/Machiel/slugify"
strip "github.com/grokify/html-strip-tags-go"
)

Expand Down Expand Up @@ -46,14 +48,15 @@ func ClearText(html string) (text string) {
}

func UrlTitle(title string) (text string) {
title = ClearEmoji(title)
title = slug.Make(title)
// title = strings.ReplaceAll(title, " ", "-")
title = convertChinese(title)
title = clearEmoji(title)
title = slugify.Slugify(title)
title = url.QueryEscape(title)
title = cutLongTitle(title)
return title
}

func ClearEmoji(s string) string {
func clearEmoji(s string) string {
ret := ""
rs := []rune(s)
for i := 0; i < len(rs); i++ {
Expand All @@ -64,6 +67,25 @@ func ClearEmoji(s string) string {
return ret
}

func convertChinese(content string) string {
has := checker.IsChinese(content)
if !has {
return content
}
str, err := pinyin.New(content).Split("-").Mode(pinyin.WithoutTone).Convert()
if err != nil {
return content
}
return str
}

func cutLongTitle(title string) string {
if len(title) > 150 {
return title[0:150]
}
return title
}

// FetchExcerpt return the excerpt from the HTML string
func FetchExcerpt(html, trimMarker string, limit int) (text string) {
if len(html) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/htmltext/htmltext_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package htmltext

import (
"fmt"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -63,6 +63,6 @@ func TestUrlTitle(t *testing.T) {
}
for _, title := range list {
formatTitle := UrlTitle(title)
spew.Dump(formatTitle)
fmt.Println(formatTitle)
}
}
6 changes: 3 additions & 3 deletions pkg/uid/sid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

const salt = int64(100)

// NumToString num to string
// NumToShortID num to string
func NumToShortID(id int64) string {
sid := strconv.FormatInt(id, 10)
if len(sid) < 17 {
Expand All @@ -26,10 +26,10 @@ func NumToShortID(id int64) string {
}
code := utils.EnShortID(id, salt)
tcode := utils.EnShortID(typeCode, salt)
return string(tcode) + string(code)
return tcode + code
}

// StringToNum string to num
// ShortIDToNum string to num
func ShortIDToNum(code string) int64 {
if len(code) < 2 {
return 0
Expand Down
53 changes: 0 additions & 53 deletions pkg/uid/sid_test.go

This file was deleted.

0 comments on commit 0299c3b

Please sign in to comment.