Skip to content

Commit 7168e91

Browse files
committed
feat:support summmary of articles
1 parent f6afbb3 commit 7168e91

File tree

10 files changed

+64
-16
lines changed

10 files changed

+64
-16
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
github.com/88250/lute v1.7.2
7+
github.com/PuerkitoBio/goquery v1.6.1
78
github.com/fsnotify/fsnotify v1.4.9 // indirect
89
github.com/gin-gonic/gin v1.6.3
910
github.com/go-playground/validator/v10 v10.4.0 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/88250/lute v1.7.2/go.mod h1:APx6IhxzrUGJqmIalKYomjVLrO4yw/PzEBUtYxpMr
1616
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
1717
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
1818
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
19+
github.com/PuerkitoBio/goquery v1.6.1 h1:FgjbQZKl5HTmcn4sKBgvx8vv63nhyhIpv7lJpFGCWpk=
20+
github.com/PuerkitoBio/goquery v1.6.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
1921
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
2022
github.com/alecthomas/chroma v0.8.2 h1:x3zkuE2lUk/RIekyAJ3XRqSCP4zwWDfcw/YJCuCAACg=
2123
github.com/alecthomas/chroma v0.8.2/go.mod h1:sko8vR34/90zvl5QdcUdvzL3J8NKjAUx9va9jPuFNoM=
@@ -26,6 +28,8 @@ github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1p
2628
github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
2729
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
2830
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
31+
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
32+
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
2933
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
3034
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
3135
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
@@ -312,6 +316,7 @@ golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU
312316
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
313317
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
314318
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
319+
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
315320
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
316321
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
317322
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -326,6 +331,8 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn
326331
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
327332
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
328333
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
334+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
335+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
329336
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
330337
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
331338
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

model/response.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type ArticleListResponse struct {
3636
type ArticleBriefInfo struct {
3737
ArticleID int64 `json:"article_id"`
3838
Title string `json:"title"`
39+
Summary string `json:"summary"`
3940
User *UserBriefInfo `json:"user"`
4041
LikeCount int `json:"like_count"`
4142
CommentCount int `json:"comment_count"`

service/articles.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"errors"
5+
"unicode/utf8"
56

67
"github.com/nk-akun/NeighborBBS/logs"
78
"github.com/nk-akun/NeighborBBS/model"
@@ -36,7 +37,7 @@ func (s *articleService) BuildArticle(userID int64, title string, content string
3637

3738
func (s *articleService) GetArticleList(limit int, cursorTime int64, sortby string, order string) (*model.ArticleListResponse, error) {
3839
resp := &model.ArticleListResponse{}
39-
fields := []string{"id", "title", "create_time", "user_id", "view_count", "comment_count", "like_count"}
40+
fields := []string{"id", "title", "create_time", "user_id", "view_count", "comment_count", "like_count", "content"}
4041
articles := repository.ArticleRepository.GetArticleFields(util.DB(), fields, cursorTime, limit, sortby, order)
4142

4243
briefList, minCursorTime := buildArticleList(articles)
@@ -66,7 +67,7 @@ func (s *articleService) GetArticleByID(id int64) (*model.ArticleResponse, error
6667
ArticleID: articleInfo.ID,
6768
Title: articleInfo.Title,
6869
User: BuildUserBriefInfo(userInfo),
69-
Content: util.ToHTML(articleInfo.Content),
70+
Content: util.MarkdownToHTML(articleInfo.Content),
7071
CommentCount: articleInfo.CommentCount,
7172
LikeCount: articleInfo.LikeCount,
7273
CreateTime: articleInfo.CreateTime,
@@ -79,9 +80,11 @@ func buildArticleList(articles []model.Article) ([]*model.ArticleBriefInfo, int6
7980
briefList := make([]*model.ArticleBriefInfo, len(articles))
8081
for i := range articles {
8182
minCursorTime = util.MinInt64(minCursorTime, articles[i].CreateTime)
83+
mkSummary := util.MarkdownToHTML(util.SubString(articles[i].Content, 0, util.MinInt(128, utf8.RuneCountInString(articles[i].Content))))
8284
briefList[i] = new(model.ArticleBriefInfo)
8385
briefList[i].ArticleID = articles[i].ID
8486
briefList[i].Title = articles[i].Title
87+
briefList[i].Summary = util.GetHTMLText(mkSummary)
8588
briefList[i].CommentCount = articles[i].CommentCount
8689
briefList[i].LikeCount = articles[i].LikeCount
8790
briefList[i].ViewCount = articles[i].ViewCount

service/comments.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func buildCommentInfo(comment *model.Comment) *model.CommentInfo {
7272
AuthorUserName: userInfo.Username,
7373
AuthorID: userInfo.ID,
7474
AvatarURL: userInfo.AvatarURL,
75-
Content: util.ToHTML(comment.Content),
75+
Content: util.MarkdownToHTML(comment.Content),
7676
LikeCount: comment.LikeCount,
7777
CreateTime: comment.CreateTime,
7878
}
@@ -105,7 +105,7 @@ func buildCommentInfoList(comtList []model.Comment) ([]*model.CommentInfo, int64
105105
AuthorUserName: userInfo.Username,
106106
AuthorID: userInfo.ID,
107107
AvatarURL: userInfo.AvatarURL,
108-
Content: util.ToHTML(comtList[i].Content),
108+
Content: util.MarkdownToHTML(comtList[i].Content),
109109
LikeCount: comtList[i].LikeCount,
110110
CreateTime: comtList[i].CreateTime,
111111
}

site/components/TopicList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<h1 class="topic-title">
1818
<a :href="'/topic/' + article.article_id">{{ article.title }}</a>
1919
</h1>
20-
<a :href="'/topic/' + article.article_id" class="topic-summary">{{ article.title }}</a>
20+
<a :href="'/topic/' + article.article_id" class="topic-summary">{{ article.summary }}</a>
2121
</template>
2222
<!-- <template v-if="topic.type === 1">
2323
<a

site/nuxt.config.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ const isProduction = process.env.NODE_ENV === 'production'
22
const isDocker = process.env.NODE_ENV === 'docker'
33

44
export default {
5-
// server: {
6-
// port: 3000,
7-
// host: '0.0.0.0',
8-
// timing: {
9-
// total: true,
10-
// },
11-
// },
12-
// mode: 'universal',
13-
// modern: true,
5+
server: {
6+
port: 3000,
7+
host: '0.0.0.0',
8+
timing: {
9+
total: true,
10+
},
11+
},
12+
mode: 'universal',
13+
modern: true,
1414
/*
1515
** Headers of the page
1616
*/

util/markdown.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package util
22

33
import (
4+
"strings"
45
"sync"
56

67
"github.com/88250/lute"
8+
"github.com/PuerkitoBio/goquery"
9+
"github.com/nk-akun/NeighborBBS/logs"
710
)
811

912
var (
@@ -22,9 +25,18 @@ func getEngine() *lute.Lute {
2225
}
2326

2427
// ToHTML ...
25-
func ToHTML(markdownStr string) string {
28+
func MarkdownToHTML(markdownStr string) string {
2629
if IsBlank(markdownStr) {
2730
return ""
2831
}
2932
return getEngine().MarkdownStr("", markdownStr)
3033
}
34+
35+
// GetHTMLText ...
36+
func GetHTMLText(html string) string {
37+
txt, err := goquery.NewDocumentFromReader(strings.NewReader(html))
38+
if err != nil {
39+
logs.Logger.Errorf("从html读取文本出错", txt)
40+
}
41+
return txt.Text()
42+
}

util/markdown_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
func Test_ToHTML(t *testing.T) {
88
str := "## 你是谁"
99

10-
t.Errorf(ToHTML(str))
10+
t.Errorf(MarkdownToHTML(str))
1111
}

util/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func RandomAvatarURL(str string) string {
2323
return fmt.Sprintf(avatarURLTemplate, hex.EncodeToString(cipherStr), str[0]%4+1)
2424
}
2525

26+
// SubString ...
27+
func SubString(str string, start int, end int) string {
28+
if start >= len(str) || end >= len(str) || start > end || end < 0 {
29+
return ""
30+
}
31+
return string([]rune(str)[start:end])
32+
}
33+
2634
// NowTimestamp return timestamp of now
2735
func NowTimestamp() int64 {
2836
return time.Now().UnixNano() / 1e6
@@ -48,3 +56,19 @@ func MaxInt64(a int64, b int64) int64 {
4856
}
4957
return b
5058
}
59+
60+
// MinInt return min(a,b)
61+
func MinInt(a int, b int) int {
62+
if a < b {
63+
return a
64+
}
65+
return b
66+
}
67+
68+
// MaxInt return max(a,b)
69+
func MaxInt(a int, b int) int {
70+
if a > b {
71+
return a
72+
}
73+
return b
74+
}

0 commit comments

Comments
 (0)