Skip to content

Commit db4b7a5

Browse files
committed
Reuse the BlackFriday instance when possible
This is in heavy use in rendering, so this makes a difference: ```bash benchmark old ns/op new ns/op delta BenchmarkSiteBuilding/TOML,num_langs=1,num_pages=500,tags_per_page=5,shortcodes,render-4 124551144 107743429 -13.49% benchmark old allocs new allocs delta BenchmarkSiteBuilding/TOML,num_langs=1,num_pages=500,tags_per_page=5,shortcodes,render-4 528684 435118 -17.70% benchmark old bytes new bytes delta BenchmarkSiteBuilding/TOML,num_langs=1,num_pages=500,tags_per_page=5,shortcodes,render-4 53306848 45147832 -15.31% ```
1 parent 2511498 commit db4b7a5

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

‎helpers/content.go‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var SummaryDivider = []byte("<!--more-->")
4141

4242
// ContentSpec provides functionality to render markdown content.
4343
type ContentSpec struct {
44-
blackfriday map[string]interface{}
44+
BlackFriday *BlackFriday
4545
footnoteAnchorPrefix string
4646
footnoteReturnLinkContents string
4747
// SummaryLength is the length of the summary that Hugo extracts from a content.
@@ -56,8 +56,9 @@ type ContentSpec struct {
5656
// NewContentSpec returns a ContentSpec initialized
5757
// with the appropriate fields from the given config.Provider.
5858
func NewContentSpec(cfg config.Provider) (*ContentSpec, error) {
59+
bf := newBlackfriday(cfg.GetStringMap("blackfriday"))
5960
spec := &ContentSpec{
60-
blackfriday: cfg.GetStringMap("blackfriday"),
61+
BlackFriday: bf,
6162
footnoteAnchorPrefix: cfg.GetString("footnoteAnchorPrefix"),
6263
footnoteReturnLinkContents: cfg.GetString("footnoteReturnLinkContents"),
6364
summaryLength: cfg.GetInt("summaryLength"),
@@ -93,8 +94,8 @@ func NewContentSpec(cfg config.Provider) (*ContentSpec, error) {
9394
return spec, nil
9495
}
9596

96-
// Blackfriday holds configuration values for Blackfriday rendering.
97-
type Blackfriday struct {
97+
// BlackFriday holds configuration values for BlackFriday rendering.
98+
type BlackFriday struct {
9899
Smartypants bool
99100
SmartypantsQuotesNBSP bool
100101
AngledQuotes bool
@@ -109,7 +110,7 @@ type Blackfriday struct {
109110
}
110111

111112
// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
112-
func (c ContentSpec) NewBlackfriday() *Blackfriday {
113+
func newBlackfriday(config map[string]interface{}) *BlackFriday {
113114
defaultParam := map[string]interface{}{
114115
"smartypants": true,
115116
"angledQuotes": false,
@@ -130,13 +131,13 @@ func (c ContentSpec) NewBlackfriday() *Blackfriday {
130131
siteConfig[k] = v
131132
}
132133

133-
if c.blackfriday != nil {
134-
for k, v := range c.blackfriday {
134+
if config != nil {
135+
for k, v := range config {
135136
siteConfig[k] = v
136137
}
137138
}
138139

139-
combinedConfig := &Blackfriday{}
140+
combinedConfig := &BlackFriday{}
140141
if err := mapstructure.Decode(siteConfig, combinedConfig); err != nil {
141142
jww.FATAL.Printf("Failed to get site rendering config\n%s", err.Error())
142143
}
@@ -434,7 +435,7 @@ type RenderingContext struct {
434435
PageFmt string
435436
DocumentID string
436437
DocumentName string
437-
Config *Blackfriday
438+
Config *BlackFriday
438439
RenderTOC bool
439440
Cfg config.Provider
440441
}

‎helpers/content_renderer_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
// Renders a codeblock using Blackfriday
2626
func (c ContentSpec) render(input string) string {
27-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
27+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
2828
render := c.getHTMLRenderer(0, ctx)
2929

3030
buf := &bytes.Buffer{}
@@ -34,7 +34,7 @@ func (c ContentSpec) render(input string) string {
3434

3535
// Renders a codeblock using Mmark
3636
func (c ContentSpec) renderWithMmark(input string) string {
37-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
37+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
3838
render := c.getMmarkHTMLRenderer(0, ctx)
3939

4040
buf := &bytes.Buffer{}
@@ -128,7 +128,7 @@ END`, true, `<ul class="task-list">
128128
<p>END</p>
129129
`},
130130
} {
131-
blackFridayConfig := c.NewBlackfriday()
131+
blackFridayConfig := c.BlackFriday
132132
blackFridayConfig.TaskLists = this.taskListEnabled
133133
ctx := &RenderingContext{Content: []byte(this.markdown), PageFmt: "markdown", Config: blackFridayConfig}
134134

‎helpers/content_test.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func TestTruncateWordsByRune(t *testing.T) {
159159

160160
func TestGetHTMLRendererFlags(t *testing.T) {
161161
c := newTestContentSpec()
162-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
162+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
163163
renderer := c.getHTMLRenderer(blackfriday.HTML_USE_XHTML, ctx)
164164
flags := renderer.GetFlags()
165165
if flags&blackfriday.HTML_USE_XHTML != blackfriday.HTML_USE_XHTML {
@@ -186,7 +186,7 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {
186186
{blackfriday.HTML_SMARTYPANTS_LATEX_DASHES},
187187
}
188188
defaultFlags := blackfriday.HTML_USE_XHTML
189-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
189+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
190190
ctx.Config.AngledQuotes = true
191191
ctx.Config.Fractions = true
192192
ctx.Config.HrefTargetBlank = true
@@ -209,7 +209,7 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {
209209

210210
func TestGetHTMLRendererAnchors(t *testing.T) {
211211
c := newTestContentSpec()
212-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
212+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
213213
ctx.DocumentID = "testid"
214214
ctx.Config.PlainIDAnchors = false
215215

@@ -233,7 +233,7 @@ func TestGetHTMLRendererAnchors(t *testing.T) {
233233

234234
func TestGetMmarkHTMLRenderer(t *testing.T) {
235235
c := newTestContentSpec()
236-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
236+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
237237
ctx.DocumentID = "testid"
238238
ctx.Config.PlainIDAnchors = false
239239
actualRenderer := c.getMmarkHTMLRenderer(0, ctx)
@@ -257,7 +257,7 @@ func TestGetMmarkHTMLRenderer(t *testing.T) {
257257

258258
func TestGetMarkdownExtensionsMasksAreRemovedFromExtensions(t *testing.T) {
259259
c := newTestContentSpec()
260-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
260+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
261261
ctx.Config.Extensions = []string{"headerId"}
262262
ctx.Config.ExtensionsMask = []string{"noIntraEmphasis"}
263263

@@ -272,7 +272,7 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
272272
testFlag int
273273
}
274274
c := newTestContentSpec()
275-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
275+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
276276
ctx.Config.Extensions = []string{""}
277277
ctx.Config.ExtensionsMask = []string{""}
278278
allExtensions := []data{
@@ -304,7 +304,7 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
304304

305305
func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
306306
c := newTestContentSpec()
307-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
307+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
308308
ctx.Config.Extensions = []string{"definitionLists"}
309309
ctx.Config.ExtensionsMask = []string{""}
310310

@@ -316,7 +316,7 @@ func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
316316

317317
func TestGetMarkdownRenderer(t *testing.T) {
318318
c := newTestContentSpec()
319-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
319+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
320320
ctx.Content = []byte("testContent")
321321
actualRenderedMarkdown := c.markdownRender(ctx)
322322
expectedRenderedMarkdown := []byte("<p>testContent</p>\n")
@@ -327,7 +327,7 @@ func TestGetMarkdownRenderer(t *testing.T) {
327327

328328
func TestGetMarkdownRendererWithTOC(t *testing.T) {
329329
c := newTestContentSpec()
330-
ctx := &RenderingContext{RenderTOC: true, Cfg: c.cfg, Config: c.NewBlackfriday()}
330+
ctx := &RenderingContext{RenderTOC: true, Cfg: c.cfg, Config: c.BlackFriday}
331331
ctx.Content = []byte("testContent")
332332
actualRenderedMarkdown := c.markdownRender(ctx)
333333
expectedRenderedMarkdown := []byte("<nav>\n</nav>\n\n<p>testContent</p>\n")
@@ -342,7 +342,7 @@ func TestGetMmarkExtensions(t *testing.T) {
342342
testFlag int
343343
}
344344
c := newTestContentSpec()
345-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
345+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
346346
ctx.Config.Extensions = []string{"tables"}
347347
ctx.Config.ExtensionsMask = []string{""}
348348
allExtensions := []data{
@@ -371,7 +371,7 @@ func TestGetMmarkExtensions(t *testing.T) {
371371

372372
func TestMmarkRender(t *testing.T) {
373373
c := newTestContentSpec()
374-
ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}
374+
ctx := &RenderingContext{Cfg: c.cfg, Config: c.BlackFriday}
375375
ctx.Content = []byte("testContent")
376376
actualRenderedMarkdown := c.mmarkRender(ctx)
377377
expectedRenderedMarkdown := []byte("<p>testContent</p>\n")

‎hugolib/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func loadDefaultSettingsFor(v *viper.Viper) error {
210210
v.SetDefault("paginate", 10)
211211
v.SetDefault("paginatePath", "page")
212212
v.SetDefault("summaryLength", 70)
213-
v.SetDefault("blackfriday", c.NewBlackfriday())
213+
v.SetDefault("blackfriday", c.BlackFriday)
214214
v.SetDefault("rSSUri", "index.xml")
215215
v.SetDefault("rssLimit", -1)
216216
v.SetDefault("sectionPagesMenu", "")

‎hugolib/page.go‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ type Page struct {
168168
plainWords []string
169169

170170
// rendering configuration
171-
renderingConfig *helpers.Blackfriday
171+
renderingConfig *helpers.BlackFriday
172172

173173
// menus
174174
pageMenus PageMenus
@@ -700,18 +700,20 @@ func (p *Page) renderContent(content []byte) []byte {
700700
Config: p.getRenderingConfig()})
701701
}
702702

703-
func (p *Page) getRenderingConfig() *helpers.Blackfriday {
703+
func (p *Page) getRenderingConfig() *helpers.BlackFriday {
704704
p.renderingConfigInit.Do(func() {
705-
p.renderingConfig = p.s.ContentSpec.NewBlackfriday()
706-
707-
if p.Language() == nil {
708-
panic(fmt.Sprintf("nil language for %s with source lang %s", p.BaseFileName(), p.lang))
709-
}
710-
711705
bfParam := p.GetParam("blackfriday")
712706
if bfParam == nil {
707+
p.renderingConfig = p.s.ContentSpec.BlackFriday
713708
return
714709
}
710+
// Create a copy so we can modify it.
711+
bf := *p.s.ContentSpec.BlackFriday
712+
p.renderingConfig = &bf
713+
714+
if p.Language() == nil {
715+
panic(fmt.Sprintf("nil language for %s with source lang %s", p.BaseFileName(), p.lang))
716+
}
715717

716718
pageParam := cast.ToStringMap(bfParam)
717719
if err := mapstructure.Decode(pageParam, &p.renderingConfig); err != nil {

‎tpl/transform/transform.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) {
9898
Cfg: ns.deps.Cfg,
9999
Content: []byte(ss),
100100
PageFmt: "markdown",
101-
Config: ns.deps.ContentSpec.NewBlackfriday(),
101+
Config: ns.deps.ContentSpec.BlackFriday,
102102
},
103103
)
104104

0 commit comments

Comments
 (0)