From 0ece32794a9c2b2bf4acabd34788582c3ad3e39b Mon Sep 17 00:00:00 2001 From: caixw Date: Fri, 11 Feb 2022 21:26:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(internal/data):=20=E7=A9=BA=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E5=B0=86=E4=B8=8D=E5=86=8D=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 10 ++++----- go.sum | 37 +++++++++---------------------- internal/data/data.go | 2 +- internal/data/tag.go | 13 +++++++++-- internal/filesystem/filesystem.go | 2 +- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 40b6fe3..9b9e7e6 100644 --- a/go.mod +++ b/go.mod @@ -3,20 +3,20 @@ module github.com/caixw/blogit/v2 go 1.16 require ( - github.com/alecthomas/chroma v0.9.4 + github.com/alecthomas/chroma v0.10.0 github.com/fsnotify/fsnotify v1.5.1 - github.com/issue9/assert/v2 v2.0.1 + github.com/issue9/assert/v2 v2.2.1 github.com/issue9/cmdopt v0.7.1 github.com/issue9/errwrap v0.2.1 - github.com/issue9/localeutil v0.8.1 + github.com/issue9/localeutil v0.8.2 github.com/issue9/sliceutil v0.7.1 github.com/issue9/term/v2 v2.1.5 github.com/issue9/validation v0.5.1 github.com/issue9/version v1.0.5 github.com/mdigger/goldmark-toc v0.0.0-20191225162753-7bc0e0d778c3 github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef - github.com/yuin/goldmark v1.4.4 - github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 + github.com/yuin/goldmark v1.4.5 + github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 github.com/yuin/goldmark-meta v1.0.0 golang.org/x/text v0.3.7 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 3e1effa..5906881 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,8 @@ -github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= -github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a/go.mod h1:fv5SzZPFJbwp2NXJWpFIX7DZS4HgV1K4ew4Pc2OZD9s= -github.com/alecthomas/chroma v0.9.4 h1:YL7sOAE3p8HS96T9km7RgvmsZIctqbK1qJ0b7hzed44= -github.com/alecthomas/chroma v0.9.4/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= -github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= -github.com/alecthomas/kong v0.2.1-0.20190708041108-0548c6b1afae/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI= -github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= -github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= +github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= +github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= -github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= @@ -19,16 +11,16 @@ github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs= github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg= -github.com/issue9/assert/v2 v2.0.0 h1:vN7fr70g5ND6zM39tPZk/E4WCyjGMqApmFbujSTmEo0= github.com/issue9/assert/v2 v2.0.0/go.mod h1:rKr1eVGzXUhAo2af1thiKAhIA8uiSK9Wyn7mcZ4BzAg= -github.com/issue9/assert/v2 v2.0.1 h1:K70rjhzz9i5Tf/HK8Dh7NESAbxb2a4mN0UfdAjpWFg4= github.com/issue9/assert/v2 v2.0.1/go.mod h1:rKr1eVGzXUhAo2af1thiKAhIA8uiSK9Wyn7mcZ4BzAg= +github.com/issue9/assert/v2 v2.2.1 h1:XQEnAFrGiT1Z55Tav39muM/Jg9ECiSLm7ehHQWoCHmA= +github.com/issue9/assert/v2 v2.2.1/go.mod h1:rKr1eVGzXUhAo2af1thiKAhIA8uiSK9Wyn7mcZ4BzAg= github.com/issue9/cmdopt v0.7.1 h1:Jle2Be9ykHPU/1p3uTGfK/QQVb2zoE+8bG5mCHpH4As= github.com/issue9/cmdopt v0.7.1/go.mod h1:2s73seejsiImh4JGBSJrYoWjK5rCHT+eYntYIUjlzro= github.com/issue9/errwrap v0.2.1 h1:hrzBGbOCRs8acitpWSg54xNl2oaLGA/WZEf71bKaiEU= github.com/issue9/errwrap v0.2.1/go.mod h1:hjjkt9S/owgy48er68ACFfA6YPDO/xax18QE3lsguVQ= -github.com/issue9/localeutil v0.8.1 h1:8ngATedH5SEyZjS37KOjuxlNbPWnIpYhRGE35RDsT6k= -github.com/issue9/localeutil v0.8.1/go.mod h1:611rGRiQKJeEoUSsd5c7PvOuiGkR7a+4C7Ce0NURcu0= +github.com/issue9/localeutil v0.8.2 h1:0IGQav8rsxn0l89il1EzTV5DkRM+uVILxxat/z/XkOM= +github.com/issue9/localeutil v0.8.2/go.mod h1:R1EC7Xf9qNbPGwJ0Ona5Nrkodb26j7darIscTefJmFU= github.com/issue9/sliceutil v0.7.1 h1:lnzYryF50+mxqvnsPZRZ1on+gxx0Khx5t5G7q0Cf7gU= github.com/issue9/sliceutil v0.7.1/go.mod h1:8e+l2bD67pBUtV8cVnGChE/Ltlq8czcDLR1pOGCC2tE= github.com/issue9/term/v2 v2.1.5 h1:qU8C5Y7Zbv7amINkUirs/dtFxislNdW7QGHAuzssOX4= @@ -37,34 +29,25 @@ github.com/issue9/validation v0.5.1 h1:olnraMrm4aKqkT60ILUVIJH3alZNLhEU/8NkCQJ7A github.com/issue9/validation v0.5.1/go.mod h1:xi4eqjEdtwTXi4wepoyi68tvdYpSYzuU4JyVN9piFXA= github.com/issue9/version v1.0.5 h1:y4JRHcAM0nXMBju9Vhe13Wv08KP/uNveP4uwg3Wknh8= github.com/issue9/version v1.0.5/go.mod h1:bgni2RNBbtygajgpVeqBiEXT9JZjkLCuYJ4ceoSXjYo= -github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mdigger/goldmark-toc v0.0.0-20191225162753-7bc0e0d778c3 h1:V8Lm1RZWBBCemfs8u4aCkfrovo6TQWxCdIPbNFzZmZs= github.com/mdigger/goldmark-toc v0.0.0-20191225162753-7bc0e0d778c3/go.mod h1:8N/DY/esIgHhhFgb7Hqu6IImzuGUmc3rc2vJlkg7MVw= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef h1:NKxTG6GVGbfMXc2mIk+KphcH6hagbVXhcFkbTgYleTI= github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef/go.mod h1:tcaRap0jS3eifrEEllL6ZMd9dg8IlDpi2S1oARrQ+NI= github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ= github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q= -github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.18/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.3.6/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/yuin/goldmark v1.4.4 h1:zNWRjYUW32G9KirMXYHQHVNFkXvMI7LpgNW2AgYAoIs= -github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= -github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 h1:0SJnXjE4jDClMW6grE0xpNhwpqbPwkBTn8zpVw5C0SI= -github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01/go.mod h1:TwKQPa5XkCCRC2GRZ5wtfNUTQ2+9/i19mGRijFeJ4BE= +github.com/yuin/goldmark v1.4.5 h1:4OEQwtW2uLXjEdgnGM3Vg652Pq37X7NOIRzFWb3BzIc= +github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= +github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 h1:yHfZyN55+5dp1wG7wDKv8HQ044moxkyGq12KFFMFDxg= +github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594/go.mod h1:U9ihbh+1ZN7fR5Se3daSPoz1CGF9IYtSvWwVQtnzGHU= github.com/yuin/goldmark-meta v1.0.0 h1:ScsatUIT2gFS6azqzLGUjgOnELsBOxMXerM3ogdJhAM= github.com/yuin/goldmark-meta v1.0.0/go.mod h1:zsNNOrZ4nLuyHAJeLQEZcQat8dm70SmB2kHbls092Gc= -golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab h1:rfJ1bsoJQQIAoAxTxB7bme+vHrNkRw8CqfsYh9w54cw= golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/data/data.go b/internal/data/data.go index fea8592..c32dd59 100644 --- a/internal/data/data.go +++ b/internal/data/data.go @@ -97,7 +97,7 @@ func build(conf *loader.Config, tags *loader.Tags, posts []*loader.Post, theme * return nil, err } - created, modified, err := relationTagsPosts(ts.Tags, ps) + created, modified, err := ts.relationTagsPosts(ps) if err != nil { return nil, err } diff --git a/internal/data/tag.go b/internal/data/tag.go index edd71f5..a8fe5e9 100644 --- a/internal/data/tag.go +++ b/internal/data/tag.go @@ -112,7 +112,7 @@ func sortTags(tags []*Tag, typ, order string) { } // 关联 tags 和 posts 的信息 -func relationTagsPosts(tags []*Tag, posts []*Post) (created, modified time.Time, err error) { +func (ts *Tags) relationTagsPosts(posts []*Post) (created, modified time.Time, err error) { for _, p := range posts { if created.Before(p.Created) { created = p.Created @@ -122,7 +122,7 @@ func relationTagsPosts(tags []*Tag, posts []*Post) (created, modified time.Time, } for _, tag := range p.tags { - t := findTagByName(tags, tag) + t := findTagByName(ts.Tags, tag) if t == nil { return time.Time{}, time.Time{}, &loader.FieldError{File: p.Slug, Message: localeutil.Phrase("not found"), Field: "tags." + tag} } @@ -149,6 +149,8 @@ func relationTagsPosts(tags []*Tag, posts []*Post) (created, modified time.Time, } } + ts.clearTags() // 清除无文章关联的标签 + if modified.IsZero() { modified = created } @@ -163,3 +165,10 @@ func findTagByName(tags []*Tag, slug string) *Tag { } return nil } + +func (ts *Tags) clearTags() { + size := sliceutil.Delete(ts.Tags, func(i int) bool { + return len(ts.Tags[i].Posts) == 0 + }) + ts.Tags = ts.Tags[:size] +} diff --git a/internal/filesystem/filesystem.go b/internal/filesystem/filesystem.go index c62d74c..e27497e 100644 --- a/internal/filesystem/filesystem.go +++ b/internal/filesystem/filesystem.go @@ -10,6 +10,6 @@ import ( // Exists 判断文件或是目录是否存在 func Exists(f fs.FS, path string) bool { - _, err := f.Open(path) + _, err := fs.Stat(f, path) return err == nil || errors.Is(err, fs.ErrExist) }