diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 3075e5092..ae31132c3 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -466,16 +466,16 @@ func (s *Site) preparePagesForRender(cfg BuildCfg, changed whatChanged) { p.rawContentCopy = p.rawContent } - if err := handleShortcodes(p, s.owner.tmpl); err != nil { - jww.ERROR.Printf("Failed to handle shortcodes for page %s: %s", p.BaseFileName(), err) - } - if p.Markup == "markdown" { tmpContent, tmpTableOfContents := helpers.ExtractTOC(p.rawContentCopy) p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents) p.rawContentCopy = tmpContent } + if err := handleShortcodes(p, s.owner.tmpl); err != nil { + jww.ERROR.Printf("Failed to handle shortcodes for page %s: %s", p.BaseFileName(), err) + } + if p.Markup != "html" { // Now we know enough to create a summary of the page and count some words diff --git a/hugolib/hugo_sites_test.go b/hugolib/hugo_sites_test.go index be2ba9803..dbbcd0277 100644 --- a/hugolib/hugo_sites_test.go +++ b/hugolib/hugo_sites_test.go @@ -3,6 +3,7 @@ package hugolib import ( "bytes" "fmt" + "regexp" "strings" "testing" @@ -172,6 +173,16 @@ func assertFileContent(t *testing.T, filename string, defaultInSubDir bool, matc } } +func assertFileContentRegexp(t *testing.T, filename string, defaultInSubDir bool, matches ...string) { + filename = replaceDefaultContentLanguageValue(filename, defaultInSubDir) + content := readDestination(t, filename) + for _, match := range matches { + match = replaceDefaultContentLanguageValue(match, defaultInSubDir) + r := regexp.MustCompile(match) + require.True(t, r.MatchString(content), fmt.Sprintf("File no match for %q in %q: %s", match, filename, content)) + } +} + // func TestMultiSitesBuild(t *testing.T) { for _, config := range []struct { @@ -660,6 +671,105 @@ func TestChangeDefaultLanguage(t *testing.T) { assertFileContent(t, "public/sect/doc2/index.html", true, "Single", "Hello") } +func TestTableOfContentsInShortcodes(t *testing.T) { + testCommonResetState() + + sites := createMultiTestSites(t, testSiteConfig{DefaultContentLanguage: "en"}, multiSiteTOMLConfigTemplate) + + writeSource(t, "layouts/shortcodes/toc.html", tocShortcode) + writeSource(t, "content/post/simple.en.md", tocPageSimple) + writeSource(t, "content/post/withSCInHeading.en.md", tocPageWithShortcodesInHeadings) + + cfg := BuildCfg{} + + err := sites.Build(cfg) + + if err != nil { + t.Fatalf("Failed to build sites: %s", err) + } + + assertFileContent(t, "public/en/post/simple/index.html", true, tocPageSimpleExpected) + assertFileContent(t, "public/en/post/withSCInHeading/index.html", true, tocPageWithShortcodesInHeadingsExpected) +} + +var tocShortcode = ` +{{ .Page.TableOfContents }} +` + +var tocPageSimple = `--- +title: tocTest +publishdate: "2000-01-01" +--- + +{{< toc >}} + +# Heading 1 {#1} + +Some text. + +## Subheading 1.1 {#1-1} + +Some more text. + +# Heading 2 {#2} + +Even more text. + +## Subheading 2.1 {#2-1} + +Lorem ipsum... +` + +var tocPageSimpleExpected = `` + +var tocPageWithShortcodesInHeadings = `--- +title: tocTest +publishdate: "2000-01-01" +--- + +{{< toc >}} + +# Heading 1 {#1} + +Some text. + +## Subheading 1.1 {{< shortcode >}} {#1-1} + +Some more text. + +# Heading 2 {{% shortcode %}} {#2} + +Even more text. + +## Subheading 2.1 {#2-1} + +Lorem ipsum... +` + +var tocPageWithShortcodesInHeadingsExpected = `` + var multiSiteTOMLConfigTemplate = ` DefaultExtension = "html" baseurl = "http://example.com/blog"