fix: 1. template read panic

2. optimization variable naming
This commit is contained in:
henry.chen
2023-01-05 09:17:31 +08:00
parent 4690d5123b
commit f6d8656c83
5 changed files with 46 additions and 37 deletions

12
pkg/cache/cache.go vendored
View File

@@ -25,9 +25,11 @@ var (
// Ei eiblog cache
Ei *Cache
// regenerate pages chan
PagesCh = make(chan string, 2)
PageSeries = "series-md"
// PagesCh regenerate pages chan
PagesCh = make(chan string, 2)
// PageSeries the page series regenerate flag
PageSeries = "series-md"
// PageArchive the page archive regenerate flag
PageArchive = "archive-md"
// ArticleStartID article start id
@@ -518,7 +520,7 @@ func (c *Cache) regeneratePages() {
}
buf.WriteString("\n")
}
c.PageSeries = string(render.RenderPage(buf.Bytes()))
c.PageSeries = string(render.PageRender(buf.Bytes()))
case PageArchive:
sort.Sort(c.Archives)
buf := bytes.Buffer{}
@@ -551,7 +553,7 @@ func (c *Cache) regeneratePages() {
}
}
}
c.PageArchives = string(render.RenderPage(buf.Bytes()))
c.PageArchives = string(render.PageRender(buf.Bytes()))
}
}
}

View File

@@ -14,7 +14,7 @@ import (
// blackfriday 配置
const (
commonHtmlFlags = 0 |
commonHTMLFlags = 0 |
blackfriday.HTML_TOC |
blackfriday.HTML_USE_XHTML |
blackfriday.HTML_USE_SMARTYPANTS |
@@ -42,9 +42,9 @@ var (
regHeader = regexp.MustCompile("</nav></div>")
)
// RenderPage 渲染markdown
func RenderPage(md []byte) []byte {
renderer := blackfriday.HtmlRenderer(commonHtmlFlags, "", "")
// PageRender 渲染markdown
func PageRender(md []byte) []byte {
renderer := blackfriday.HtmlRenderer(commonHTMLFlags, "", "")
return blackfriday.Markdown(md, renderer, commonExtensions)
}
@@ -56,12 +56,12 @@ func GenerateExcerptMarkdown(article *model.Article) {
index := strings.Index(article.Content, "\r\n")
prefix := article.Content[len(blogapp.General.DescPrefix):index]
article.Desc = tools.IgnoreHtmlTag(prefix)
article.Desc = tools.IgnoreHTMLTag(prefix)
article.Content = article.Content[index:]
}
// 查找目录
content := RenderPage([]byte(article.Content))
content := PageRender([]byte(article.Content))
index := regHeader.FindIndex(content)
if index != nil {
article.Header = string(content[0:index[1]])
@@ -73,7 +73,7 @@ func GenerateExcerptMarkdown(article *model.Article) {
// excerpt
index = regIdentifier.FindStringIndex(article.Content)
if index != nil {
article.Excerpt = tools.IgnoreHtmlTag(article.Content[:index[0]])
article.Excerpt = tools.IgnoreHTMLTag(article.Content[:index[0]])
return
}
uc := []rune(article.Content)
@@ -81,5 +81,5 @@ func GenerateExcerptMarkdown(article *model.Article) {
if len(uc) < length {
length = len(uc)
}
article.Excerpt = tools.IgnoreHtmlTag(string(uc[0:length]))
article.Excerpt = tools.IgnoreHTMLTag(string(uc[0:length]))
}

View File

@@ -2,6 +2,7 @@
package page
import (
"io/fs"
"path/filepath"
"text/template"
@@ -17,10 +18,15 @@ var htmlTmpl *template.Template
func init() {
htmlTmpl = template.New("eiblog").Funcs(tools.TplFuncMap)
root := filepath.Join(config.WorkDir, "website")
files := tools.ReadDirFiles(root, func(name string) bool {
files := tools.ReadDirFiles(root, func(fi fs.FileInfo) bool {
name := fi.Name()
if name == ".DS_Store" {
return true
}
// should not read template dir
if fi.IsDir() && name == "template" {
return true
}
return false
})
_, err := htmlTmpl.ParseFiles(files...)

View File

@@ -49,7 +49,7 @@ func checkESConfig() error {
}
// ElasticSearch 搜索文章
func ElasticSearch(query string, size, from int) (*searchIndexResult, error) {
func ElasticSearch(query string, size, from int) (*SearchIndexResult, error) {
if err := checkESConfig(); err != nil {
return nil, err
}
@@ -114,7 +114,7 @@ func ElasticAddIndex(article *model.Article) error {
img := tools.PickFirstImage(article.Content)
mapping := map[string]interface{}{
"title": article.Title,
"content": tools.IgnoreHtmlTag(article.Content),
"content": tools.IgnoreHTMLTag(article.Content),
"slug": article.Slug,
"tag": article.Tags,
"img": img,
@@ -241,8 +241,8 @@ func deleteIndexDocument(index, typ string, ids []string) error {
return nil
}
// searchIndexResult 查询结果
type searchIndexResult struct {
// SearchIndexResult 查询结果
type SearchIndexResult struct {
Took float32 `json:"took"`
Hits struct {
Total int `json:"total"`
@@ -264,7 +264,7 @@ type searchIndexResult struct {
}
// indexQueryDSL 语句查询文档
func indexQueryDSL(index, typ string, size, from int, dsl []byte) (*searchIndexResult, error) {
func indexQueryDSL(index, typ string, size, from int, dsl []byte) (*SearchIndexResult, error) {
rawurl := fmt.Sprintf("%s/%s/%s/_search?size=%d&from=%d", config.Conf.ESHost,
index, typ, size, from)
resp, err := httpPost(rawurl, dsl)
@@ -276,7 +276,7 @@ func indexQueryDSL(index, typ string, size, from int, dsl []byte) (*searchIndexR
if err != nil {
return nil, err
}
result := &searchIndexResult{}
result := &SearchIndexResult{}
err = json.Unmarshal(data, result)
if err != nil {
return nil, err