mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-16 20:53:59 +02:00
Make .Content (almost) always available in shortcodes
This resolves some surprising behaviour when reading other pages' content from shortcodes. Before this commit, that behaviour was undefined. Note that this has never been an issue from regular templates. It will still not be possible to get **the current shortcode's page's rendered content**. That would have impressed Einstein. The new and well defined rules are: * `.Page.Content` from a shortcode will be empty. The related `.Page.Truncated` `.Page.Summary`, `.Page.WordCount`, `.Page.ReadingTime`, `.Page.Plain` and `.Page.PlainWords` will also have empty values. * For _other pages_ (retrieved via `.Page.Site.GetPage`, `.Site.Pages` etc.) the `.Content` is there to use as you please as long as you don't have infinite content recursion in your shortcode/content setup. See below. * `.Page.TableOfContents` is good to go (but does not support shortcodes in headlines; this is unchanged) If you get into a situation of infinite recursion, the `.Content` will be empty. Run `hugo -v` for more information. Fixes #4632 Fixes #4653 Fixes #4655
This commit is contained in:
@@ -560,37 +560,22 @@ func (h *HugoSites) setupTranslations() {
|
||||
}
|
||||
|
||||
func (s *Site) preparePagesForRender(cfg *BuildCfg) {
|
||||
|
||||
pageChan := make(chan *Page)
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
numWorkers := getGoMaxProcs() * 4
|
||||
|
||||
for i := 0; i < numWorkers; i++ {
|
||||
wg.Add(1)
|
||||
go func(pages <-chan *Page, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
for p := range pages {
|
||||
if err := p.prepareForRender(cfg); err != nil {
|
||||
s.Log.ERROR.Printf("Failed to prepare page %q for render: %s", p.BaseFileName(), err)
|
||||
|
||||
}
|
||||
}
|
||||
}(pageChan, wg)
|
||||
}
|
||||
|
||||
for _, p := range s.Pages {
|
||||
pageChan <- p
|
||||
p.setContentInit(cfg)
|
||||
// The skip render flag is used in many tests. To make sure that they
|
||||
// have access to the content, we need to manually initialize it here.
|
||||
if cfg.SkipRender {
|
||||
p.initContent()
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range s.headlessPages {
|
||||
pageChan <- p
|
||||
p.setContentInit(cfg)
|
||||
if cfg.SkipRender {
|
||||
p.initContent()
|
||||
}
|
||||
}
|
||||
|
||||
close(pageChan)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
}
|
||||
|
||||
// Pages returns all pages for all sites.
|
||||
@@ -598,7 +583,7 @@ func (h *HugoSites) Pages() Pages {
|
||||
return h.Sites[0].AllPages
|
||||
}
|
||||
|
||||
func handleShortcodes(p *Page, rawContentCopy []byte) ([]byte, error) {
|
||||
func handleShortcodes(p *PageWithoutContent, rawContentCopy []byte) ([]byte, error) {
|
||||
if p.shortcodeState != nil && len(p.shortcodeState.contentShortcodes) > 0 {
|
||||
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", len(p.shortcodeState.contentShortcodes), p.BaseFileName())
|
||||
err := p.shortcodeState.executeShortcodesForDelta(p)
|
||||
|
Reference in New Issue
Block a user