fix: custom page support embed

This commit is contained in:
henry.chen
2025-07-21 20:07:26 +08:00
parent b69248f6a4
commit aee4194c71
9 changed files with 77 additions and 10 deletions

View File

@@ -20,7 +20,7 @@ import (
var (
XMLTemplate *template.Template // template/xml模板
HTMLTemplate *template.Template // website/html模板
HTMLTemplate *template.Template // page/html | website/html模板
Store store.Store // 数据库存储
Ei *Cache // 博客数据缓存
@@ -49,6 +49,11 @@ func init() {
// should not read dir & .DS_Store
return strings.HasPrefix(fi.Name(), ".")
})
root = filepath.Join(config.EtcDir, "page")
pageFiles := tools.ReadDirFiles(root, func(fi fs.DirEntry) bool {
return !strings.HasSuffix(fi.Name(), ".html")
})
files = append(files, pageFiles...)
HTMLTemplate, err = template.New("eiblog").Funcs(tools.TplFuncMap).ParseFiles(files...)
if err != nil {
logrus.Fatal("init html template: ", err)

View File

@@ -1,4 +1,4 @@
package page
package pages
import (
"bytes"

View File

@@ -1,4 +1,4 @@
package page
package pages
import (
"bytes"
@@ -14,6 +14,7 @@ import (
"github.com/eiblog/eiblog/cmd/eiblog/config"
"github.com/eiblog/eiblog/cmd/eiblog/handler/internal"
pconfig "github.com/eiblog/eiblog/pkg/config"
"github.com/eiblog/eiblog/pkg/third/disqus"
"github.com/eiblog/eiblog/tools"
@@ -382,16 +383,53 @@ func handleBeaconPage(c *gin.Context) {
c.Status(http.StatusNoContent)
}
// handleCustomPage 自定义页面
func handleCustomPage(c *gin.Context) {
path := c.Request.URL.Path
if !strings.HasSuffix(path, ".html") {
handleNotFound(c)
return
}
// find config
var page *pconfig.CustomPage
for _, p := range config.Conf.Pages {
if p.Path == path {
page = &p
break
}
}
if page == nil {
handleNotFound(c)
return
}
var params gin.H
if page.IsEmbed {
params = baseFEParams(c)
params["Path"] = c.Request.URL.Path
params["Title"] = page.Name + " | " + internal.Ei.Blogger.SubTitle
}
// serve custom page
name := c.Param("path")
renderHTMLHomeLayout(c, name, params)
}
// renderHTMLHomeLayout homelayout html
func renderHTMLHomeLayout(c *gin.Context, name string, data gin.H) {
c.Header("Content-Type", "text/html; charset=utf-8")
// special page
if name == "disqus.html" {
switch {
case name == "disqus.html":
err := internal.HTMLTemplate.ExecuteTemplate(c.Writer, name, data)
if err != nil {
panic(err)
}
return
case data == nil:
err := internal.HTMLTemplate.ExecuteTemplate(c.Writer, name, nil)
if err != nil {
panic(err)
}
return
}
buf := bytes.Buffer{}
err := internal.HTMLTemplate.ExecuteTemplate(&buf, name, data)

View File

@@ -1,9 +1,14 @@
package page
package pages
import (
"github.com/gin-gonic/gin"
)
// RegisterRoutesCustomPages 注册自定义页面
func RegisterRoutesCustomPages(e *gin.Engine) {
e.GET("/page/:path", handleCustomPage)
}
// RegisterRoutes register routes
func RegisterRoutes(e *gin.Engine) {
e.NoRoute(handleNotFound)