Repository navigation
Expand file tree
/
Copy patharticle_render.go
More file actions
84 lines (69 loc) · 2.12 KB
/
Copy patharticle_render.go
File metadata and controls
84 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package render
import (
"bbs-go/cache"
"bbs-go/model"
"bbs-go/model/constants"
"bbs-go/pkg/markdown"
"github.com/mlogclub/simple"
)
func BuildArticle(article *model.Article) *model.ArticleResponse {
if article == nil {
return nil
}
rsp := &model.ArticleResponse{}
rsp.ArticleId = article.Id
rsp.Title = article.Title
rsp.Summary = article.Summary
rsp.SourceUrl = article.SourceUrl
rsp.ViewCount = article.ViewCount
rsp.CreateTime = article.CreateTime
rsp.Status = article.Status
rsp.User = BuildUserInfoDefaultIfNull(article.UserId)
tagIds := cache.ArticleTagCache.Get(article.Id)
tags := cache.TagCache.GetList(tagIds)
rsp.Tags = BuildTags(tags)
if article.ContentType == constants.ContentTypeMarkdown {
content := markdown.ToHTML(article.Content)
rsp.Content = handleHtmlContent(content)
} else if article.ContentType == constants.ContentTypeHtml {
rsp.Content = handleHtmlContent(article.Content)
}
return rsp
}
func BuildSimpleArticle(article *model.Article) *model.ArticleSimpleResponse {
if article == nil {
return nil
}
rsp := &model.ArticleSimpleResponse{}
rsp.ArticleId = article.Id
rsp.Title = article.Title
rsp.Summary = article.Summary
rsp.SourceUrl = article.SourceUrl
rsp.ViewCount = article.ViewCount
rsp.CreateTime = article.CreateTime
rsp.Status = article.Status
rsp.User = BuildUserInfoDefaultIfNull(article.UserId)
tagIds := cache.ArticleTagCache.Get(article.Id)
tags := cache.TagCache.GetList(tagIds)
rsp.Tags = BuildTags(tags)
if article.ContentType == constants.ContentTypeMarkdown {
if len(rsp.Summary) == 0 {
rsp.Summary = markdown.GetSummary(article.Content, constants.SummaryLen)
}
} else if article.ContentType == constants.ContentTypeHtml {
if len(rsp.Summary) == 0 {
rsp.Summary = simple.GetSummary(simple.GetHtmlText(article.Content), constants.SummaryLen)
}
}
return rsp
}
func BuildSimpleArticles(articles []model.Article) []model.ArticleSimpleResponse {
if len(articles) == 0 {
return nil
}
var responses []model.ArticleSimpleResponse
for _, article := range articles {
responses = append(responses, *BuildSimpleArticle(&article))
}
return responses
}