diff --git a/routers/search.go b/routers/search.go index 6565f763b..bac69cb57 100644 --- a/routers/search.go +++ b/routers/search.go @@ -240,7 +240,7 @@ func makePrivateRepo(repos models.RepositoryList, res *SearchRes, keyword string record["name"] = makeHighLight(keyword, repo.Name) record["real_name"] = repo.Name record["owner_name"] = repo.OwnerName - record["description"] = truncLongText(makeHighLight(keyword, repo.Name), true, keyword) + record["description"] = truncLongText(makeHighLight(keyword, repo.Name), true) hightTopics := make([]string, 0) if len(repo.Topics) > 0 { @@ -352,20 +352,33 @@ func dealLongText(text string, Key string, MatchedQueries []string) string { isNeedToDealText = true } } - return truncLongText(text, isNeedToDealText, "color=") + return truncLongText(text, isNeedToDealText) } -func truncLongText(text string, isNeedToDealText bool, keyword string) string { +func truncLongText(text string, isNeedToDealText bool) string { + startStr := "color=" + endStr := "/font" textRune := []rune(text) stringlen := len(textRune) if isNeedToDealText && stringlen > 200 { - index := chineseIndex(textRune, []rune(keyword)) + index := findFont(textRune, 0, []rune(startStr)) if index > 0 { start := index - 50 if start < 0 { start = 0 } end := index + 150 + endIndex := findFont(textRune, index+5, []rune(endStr)) + if endIndex > 0 { + endIndex2 := findFont(textRune, endIndex+5, []rune(endStr)) + if endIndex2 > 0 { + if endIndex2-start < 300 { + end = endIndex2 + 10 + } + } else { + end = endIndex + 50 + } + } if end >= stringlen { end = stringlen } @@ -382,9 +395,9 @@ func truncLongText(text string, isNeedToDealText bool, keyword string) string { } } -func chineseIndex(text []rune, childText []rune) int { - for i, v := range text { - if v == childText[0] { +func findFont(text []rune, startindex int, childText []rune) int { + for i := 0; i <= len(text); i++ { + if text[i] == childText[0] { re := true for j, k := range childText { if k != text[i+j] {