Skip to content

Commit

Permalink
初步完成所有功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaofan committed Sep 24, 2019
1 parent 8546376 commit 99f9337
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 40 deletions.
5 changes: 1 addition & 4 deletions controllers/blog.go
Expand Up @@ -121,13 +121,10 @@ func PostSearch(c *gin.Context) {
Posts["url"] = post.Url()
Posts["tags"] = post.GetTagsArray()
Posts["title"] = post.Title
Posts["content"] = models.GetContent(int(post.ID))
Posts["content"] = post.Content
ret = append(ret,Posts)

}

c.JSON(http.StatusOK,ret)

}

func PostPage(c *gin.Context) {
Expand Down
11 changes: 2 additions & 9 deletions controllers/comment.go
Expand Up @@ -22,11 +22,11 @@ func CreateComment(c *gin.Context) {
comment := models.Comment{
GitHubID:gid.(int64),
PostID:postID,
Content:content,
RefID:0,
}
_ = models.CommentCreatAndGetID(&comment)
models.SetCommentContent(int64(comment.ID), content)
commentHTML,_ := utils.RenderSingleCommnet(&comment)
commentHTML,_ := utils.RenderSingleComment(&comment)
c.JSON(http.StatusOK,gin.H{
"r":0,
"html":commentHTML,
Expand Down Expand Up @@ -77,13 +77,6 @@ func Comments(c *gin.Context) {
"r":0,
"html":commentsHTML,
})

//c.JSON(http.StatusOK,gin.H{
// "r": 0,
// "comments":comments,
// "githubuser":gitHubUser,
// "post":post,
//})
}

func CommentMarkdown(c *gin.Context) {
Expand Down
8 changes: 3 additions & 5 deletions controllers/post.go
Expand Up @@ -100,13 +100,11 @@ func GetEditPost(c *gin.Context) {
if post == nil {
c.Redirect(http.StatusMovedPermanently,"/admin/users")
}
content := models.GetContent(int(postID))
c.HTML(http.StatusOK, "admin/post.html",gin.H{
"post":post,
"users":users,
"allTags":allTags,
"postTags":postTags,
"content": content,
})
}

Expand Down Expand Up @@ -134,11 +132,11 @@ func AddPost(c *gin.Context){
Slug:slug,
Summary:summary,
AuthorID:int(authorID),
Content:content,
CanComment:canComment,
Published:publish,
}
_ = models.PostCreatAndGetID(post)
models.SetContent(int(post.ID), content)
models.UpdateMultiTags([]string{}, tags, int(post.ID))
posts, _ := models.ListPosts()
for _, post := range posts {
Expand Down Expand Up @@ -169,12 +167,12 @@ func UpdatePost(c *gin.Context) {
Slug:slug,
Summary:summary,
AuthorID:int(authorID),
Content:content,
CanComment:canComment,
Published:publish,
}
post.ID = uint64(pID)
post.Update()
models.SetContent(int(pID), content)
originPostTags,_ := models.ListTagByPostID(post.ID)
originPostTagNames := models.GetTagNames(originPostTags)
models.UpdateMultiTags(originPostTagNames, tags, int(post.ID))
Expand Down Expand Up @@ -217,7 +215,7 @@ func getPost(c *gin.Context, isPublish bool) {
}
tags ,_ := models.ListTagByPostID(post.ID)
post.Tags = tags
content := models.GetContent(int(postID))
content := post.Content
comments, _ := models.ListCommentsByPostID(int(postID))
gitHubUser, _ := c.Get(models.CONTEXT_GIT_USER_KEY)
policy := bluemonday.UGCPolicy()
Expand Down
7 changes: 3 additions & 4 deletions models/comment.go
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"html/template"
Expand All @@ -13,6 +12,7 @@ type Comment struct {
BaseModel
GitHubID int64
PostID int64
Content string `gorm:"type:longtext"`
RefID int64
}

Expand All @@ -24,7 +24,6 @@ func (comment *Comment) Insert()error{
func ListCommentsByPostID(postid int)([]*Comment, error){
var comments []*Comment
err := DB.Model(&Comment{}).Order("id desc").Find(&comments,"post_id=?",postid).Error
fmt.Println(comments)
return comments,err
}

Expand All @@ -33,9 +32,9 @@ func (comment *Comment) GitUser() *GitHubUser{
return gitUser
}

func (comment *Comment) CommentHTML(content string) template.HTML {
func (comment *Comment) CommentHTML() template.HTML {
policy := bluemonday.UGCPolicy()
unsafe := blackfriday.MarkdownCommon([]byte(content))
unsafe := blackfriday.MarkdownCommon([]byte(comment.Content))
contentHtml:=template.HTML(string(policy.SanitizeBytes(unsafe)))
return contentHtml
}
Expand Down
5 changes: 3 additions & 2 deletions models/post.go
Expand Up @@ -19,6 +19,7 @@ type Post struct {
AuthorID int
Slug string
Summary string
Content string `gorm:"type:longtext"`
CanComment bool
Published bool
Tags []*Tag `gorm:"-"`
Expand Down Expand Up @@ -52,9 +53,9 @@ func (post *Post) GetUserName(userID int)string {
}

func (post *Post) Excerpt() template.HTML {
content :=GetContent(int(post.ID))
//content :=GetContent(int(post.ID))
policy := bluemonday.StrictPolicy() //remove all html tags
sanitized := policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(content))))
sanitized := policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Content))))
runes := []rune(sanitized)
if len(runes) > 300 {
sanitized = string(runes[:300])
Expand Down
2 changes: 1 addition & 1 deletion request.token
@@ -1 +1 @@
{"AccessToken":"14734960e993c5a7f70fa5f5a1f2dcd4d9aade9e","RefreshToken":"","Expiry":"0001-01-01T00:00:00Z","Extra":null}
{"AccessToken":"07f3f0fad19f0c1bccec6c3d7864d44846b4e02a","RefreshToken":"","Expiry":"0001-01-01T00:00:00Z","Extra":null}
4 changes: 2 additions & 2 deletions static/css/main.css
Expand Up @@ -1246,7 +1246,7 @@ img {
.post-container .toc-container .toc-article {
display: block;
position: fixed;
margin-left: -15px;
margin-left: 80px;
width: 200px;
top: 25%;
z-index: 1;
Expand Down Expand Up @@ -1335,7 +1335,7 @@ img {
.post-container .toc-container .toc-article {
display: block;
position: fixed;
margin-left: -15px;
margin-left: 80px;
width: 200px;
top: 25%;
z-index: 1;
Expand Down
2 changes: 1 addition & 1 deletion utils/template.go
Expand Up @@ -60,7 +60,7 @@ func RenderAllComment(hh HH) (string,error) {
return AllCommentHTML,nil
}

func RenderSingleCommnet(comment *models.Comment)(string,error){
func RenderSingleComment(comment *models.Comment)(string,error){
var (
commentHTML string
err error
Expand Down
8 changes: 6 additions & 2 deletions utils/utils_test.go
@@ -1,10 +1,14 @@
package utils

import "testing"
import (
"fmt"
"testing"
)

func TestMd5(t *testing.T) {
username := "admin"
password := "123456"
md5_res := Md5(username + password)
t.Log(md5_res)
fmt.Println(md5_res)
//t.Log(md5_res)
}
2 changes: 1 addition & 1 deletion views/admin/post.html
Expand Up @@ -42,7 +42,7 @@
<div class="uk-margin">
<label class="uk-form-label" for="">Content</label>
<div class="uk-form-controls">
<textarea name="content" class="uk-textarea">{{if .post }}{{.Content}}{{end}}</textarea>
<textarea name="content" class="uk-textarea">{{if .post }}{{ .post.Content }}{{end}}</textarea>
</div>
</div>
<div class="uk-margin">
Expand Down
4 changes: 2 additions & 2 deletions views/front/comment.html
Expand Up @@ -29,8 +29,8 @@
</div>
{{end}}
</div>
{{$Conent := $Comment.GetComment $Comment.ID}}
<div class="gitment-comment-body gitment-markdown">{{ $Comment.CommentHTML $Conent }}</div>
{{/* {{$Conent := $Comment.GetComment $Comment.ID}}*/}}
<div class="gitment-comment-body gitment-markdown">{{ $Comment.CommentHTML }}</div>
</div>
</li>
{{end}}
Expand Down
7 changes: 2 additions & 5 deletions views/front/post.html
Expand Up @@ -56,7 +56,6 @@ <h1 class="title">{{.Post.Title}}</h1>
</span>
</span>
</header>

{{ if ne .Post.Slug "aboutme"}}
<div class="post-content" id="body">
{{ .contentHtml }}
Expand Down Expand Up @@ -101,14 +100,12 @@ <h1 class="title">{{.Post.Title}}</h1>
{{ range $k, $q := $PAGES }}
<li class="gitment-comments-page-item {{ if not $q}}gitment-selected{{else}}''{{end}}">{{ add $q 1}}</li>
{{end}}

<li class="gitment-comments-page-item {{ if le .CommentNum 10 }}gitment-hidden{{else}}''{{end}} } next">Next</li>
</ul>
{{ if not .Comments}}
<div class="gitment-comments-empty">还没有评论</div>
{{end}}
</div>

{{end}}
<div class="gitment-container gitment-editor-container">
<a class="gitment-editor-avatar" href="{{ if .Githubuser }}gituser.NickName{{else}}'/oauth2/auth/post/{{.Post.ID}}'{{end}}">
Expand All @@ -135,11 +132,11 @@ <h1 class="title">{{.Post.Title}}</h1>
</div>
<div class="gitment-editor-body">
<div class="gitment-editor-write-field">
<textarea placeholder="评价一下吧" title=""
<textarea placeholder="评价一下吧" title=""
{{if not .Githubuser }}
disabled
{{end}}
></textarea>
></textarea>
</div>
<div class="gitment-editor-preview-field gitment-hidden">
<div class="gitment-editor-preview gitment-markdown">空空如也</div>
Expand Down
3 changes: 1 addition & 2 deletions views/front/singleComment.html
Expand Up @@ -19,7 +19,6 @@
</div>
{{end}}
</div>
{{$Conent := .GetComment .ID}}
<div class="gitment-comment-body gitment-markdown">{{ .CommentHTML $Conent }}</div>
<div class="gitment-comment-body gitment-markdown">{{ .CommentHTML }}</div>
</div>
</li>

0 comments on commit 99f9337

Please sign in to comment.