Skip to content

Commit

Permalink
feat: move minifier to ext/
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed May 6, 2022
1 parent 15ae4d6 commit 62bf74f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
21 changes: 0 additions & 21 deletions pkg/core/generator/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"sort"
"sync"

"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
"go.uber.org/atomic"
)

Expand All @@ -26,8 +24,6 @@ type Context struct {

postSlugTemplate *template.Template
tagLinkTemplate *template.Template

minifier *minify.M
}

func NewContext(s *SiteData, opt *Option) *Context {
Expand Down Expand Up @@ -167,20 +163,3 @@ func (ctx *Context) incrOutputCounter(delta int64) int64 {
func (ctx *Context) appendCopyDir(srcDir, dstDir string) {
ctx.copingDirs = append(ctx.copingDirs, &models.CopyDir{SrcDir: srcDir, DestDir: dstDir})
}

func (ctx *Context) MinifyHTML(raw []byte) ([]byte, error) {
if ctx.minifier == nil {
m := minify.New()
m.Add("text/html", &html.Minifier{
KeepComments: false,
KeepConditionalComments: true,
KeepDefaultAttrVals: true,
KeepDocumentTags: true,
KeepEndTags: false,
KeepQuotes: true,
KeepWhitespace: false,
})
ctx.minifier = m
}
return ctx.minifier.Bytes("text/html", raw)
}
3 changes: 2 additions & 1 deletion pkg/core/generator/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"pugo/pkg/core/theme"
"pugo/pkg/ext/markdown"
"pugo/pkg/utils"
"pugo/pkg/utils/zlog"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func outputFiles(s *SiteData, ctx *Context) error {
data := buf.Bytes()
dataLen := len(data)
if s.BuildConfig.EnableMinifyHTML {
data, err = ctx.MinifyHTML(data)
data, err = markdown.MinifyHTML(data)
if err != nil {
zlog.Warnf("output: failed to minify: %s, %s", fpath, err)
data = buf.Bytes()
Expand Down
6 changes: 6 additions & 0 deletions pkg/ext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package ext

import (
"pugo/pkg/core/configs"
"pugo/pkg/ext/markdown"
"pugo/pkg/ext/sitemap"
"pugo/pkg/utils/zlog"
)

func Reload(cfg *configs.Config) {
markdown.InitMinfier(cfg.Build.EnableMinifyHTML)
if cfg.Build.EnableMinifyHTML {
zlog.Debugf("minify html: enabled")
}

ext := cfg.Extension
if ext.Feed != nil {
zlog.Debugf("feed reloaded, enabled:%v", ext.Feed.Enabled)
Expand Down
37 changes: 37 additions & 0 deletions pkg/ext/markdown/minify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package markdown

import (
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
)

var (
globalMinifier *minify.M = nil
)

// InitMinfier initializes minifier
func InitMinfier(flag bool) {
if flag {
m := minify.New()
m.Add("text/html", &html.Minifier{
KeepComments: false,
KeepConditionalComments: true,
KeepDefaultAttrVals: true,
KeepDocumentTags: true,
KeepEndTags: false,
KeepQuotes: true,
KeepWhitespace: false,
})
globalMinifier = m
} else {
globalMinifier = nil
}
}

// MinifyHTML minifies HTML
func MinifyHTML(raw []byte) ([]byte, error) {
if globalMinifier == nil {
return raw, nil
}
return globalMinifier.Bytes("text/html", raw)
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.14
0.1.16

0 comments on commit 62bf74f

Please sign in to comment.