mirror of https://gitee.com/answerdev/answer.git
feat(question): change create question title function
This commit is contained in:
parent
3c08f31e07
commit
5d28954e4c
5
go.mod
5
go.mod
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
|
6
go.sum
6
go.sum
|
@ -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=
|
||||
|
@ -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=
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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++ {
|
||||
|
@ -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 {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package htmltext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -63,6 +63,6 @@ func TestUrlTitle(t *testing.T) {
|
|||
}
|
||||
for _, title := range list {
|
||||
formatTitle := UrlTitle(title)
|
||||
spew.Dump(formatTitle)
|
||||
fmt.Println(formatTitle)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
package uid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func Test_ShortID(t *testing.T) {
|
||||
nums := []int64{
|
||||
10010000000000001, 10010000000000002, 10010000000000003, 10010000000000004,
|
||||
10030000000000689, 10010000000000676, 10020000000000658, 10020000000000654,
|
||||
10030000009000689, 10010000009000676, 10020000009000658, 10020000009999999,
|
||||
10030000090000689, 10010000090000676, 10020000090000658, 10020000099999999,
|
||||
10030000900000689, 10010000900000676, 10020000900000658, 10020000999999999,
|
||||
10030009000000689, 10010009000000676, 10020009000000658, 10020009999999999,
|
||||
10030090000000689, 10010090000000676, 10020090000000658, 10020099999999999,
|
||||
10030900000000689, 10010900000000676, 10020900000000658, 10020999999999999,
|
||||
10039000000000689, 10019000000000676, 10029000000000658, 10029999999999999,
|
||||
10610000000000689, 10610000000000676, 10610000000000658, 10610000000000654,
|
||||
19990000000000689, 19990000000000676, 19990000000000658, 19990000000000654,
|
||||
}
|
||||
for _, num := range nums {
|
||||
code := NumToShortID(num)
|
||||
denum := ShortIDToNum(code)
|
||||
fmt.Println(num, code, denum)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_EnDeShortID(t *testing.T) {
|
||||
nums := []string{"0", "1", "10", "100", "1000", "10000", "100000", "1234567", "10000000000000000", "10010000000001316", "19930000000001316"}
|
||||
for _, num := range nums {
|
||||
code := EnShortID(num)
|
||||
denum := DeShortID(code)
|
||||
fmt.Println(num, code, denum)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Demo(t *testing.T) {
|
||||
nums := []int64{0, 1, 10, 100, 1000, 10000, 100000, 1000000001316, 9000000001316, 10000000000000000, 10010000000001316, 10030000000001316, 99999999999999999, 999999999999999999, 1999999999999999999}
|
||||
for _, num := range nums {
|
||||
code := strconv.FormatInt(num, 36) //10 yo 16
|
||||
fmt.Println(num, code)
|
||||
}
|
||||
}
|
||||
|
||||
// https://answer.dev.segmentfault.com/questions/D112
|
||||
func Test_DeCode(t *testing.T) {
|
||||
aaa := DeShortID("D1w2")
|
||||
spew.Dump(aaa)
|
||||
}
|
Loading…
Reference in New Issue