Skip to content

Commit

Permalink
b3log#151
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 30, 2018
1 parent ccb707d commit 3479b65
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
65 changes: 65 additions & 0 deletions controller/pjaxmidware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-2018, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package controller

import (
"github.com/gin-gonic/gin"
"regexp"
"strings"
)

func pjax(c *gin.Context) {
isPJAX := isPJAX(c)
model := getDataModel(c)
model["pjax"] = isPJAX

if !isPJAX {
c.Next()

return
}

c.Writer = &pjaxHTMLWriter{c.Writer, &strings.Builder{}, c}
c.Next()
}

type pjaxHTMLWriter struct {
gin.ResponseWriter
bodyBuilder *strings.Builder
c *gin.Context
}

func (p *pjaxHTMLWriter) Write(data []byte) (int, error) {
p.bodyBuilder.Write(data)
if !strings.HasSuffix(string(data), "</html>\r\n") {
return 0, nil
}

pjaxContainer := p.c.Request.Header.Get("X-PJAX-Container")
body := p.bodyBuilder.String()
r := regexp.MustCompile("<!---- pjax {" + pjaxContainer + "} start ---->(.+)?<!---- pjax {" + pjaxContainer + "} end ---->")
containers := r.FindStringSubmatch(body)
if 0 == len(containers) {
return p.ResponseWriter.WriteString(body)
}

return p.ResponseWriter.WriteString(strings.Join(containers, ""))
}

func isPJAX(c *gin.Context) bool {
return "true" == c.Request.Header.Get("X-PJAX") && "" != c.Request.Header.Get("X-PJAX-Container")
}
2 changes: 1 addition & 1 deletion controller/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func MapRoutes() *gin.Engine {
templates = append(templates, headTemplates...)
ret.LoadHTMLFiles(templates...)
themeGroup := ret.Group(util.PathBlogs + "/:username")
themeGroup.Use(fillUser, resolveBlog)
themeGroup.Use(fillUser, pjax, resolveBlog)
themeGroup.GET("", showArticlesAction)
themeGroup.Any("/*path", routePath)

Expand Down

0 comments on commit 3479b65

Please sign in to comment.