diff options
author | Luca Corbatto <targodan@users.noreply.github.com> | 2016-10-24 14:40:57 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-10-24 14:40:57 +0200 |
commit | d9f54a13c14f12ccc8af33e9dbd611c2cd113324 (patch) | |
tree | 35ffc34a76eea13c612f7d9d40334766688f8cb0 | |
parent | 165edc7f0a84c345f23ee859653701bfb180cfd1 (diff) |
Handle ToC before handling shortcodes
Fixes #2433
-rw-r--r-- | hugolib/hugo_sites.go | 8 | ||||
-rw-r--r-- | hugolib/hugo_sites_test.go | 110 |
2 files changed, 114 insertions, 4 deletions
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 = `<nav id="TableOfContents"> +<ul> +<li><a href="#1">Heading 1</a> +<ul> +<li><a href="#1-1">Subheading 1.1</a></li> +</ul></li> +<li><a href="#2">Heading 2</a> +<ul> +<li><a href="#2-1">Subheading 2.1</a></li> +</ul></li> +</ul> +</nav>` + +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 = `<nav id="TableOfContents"> +<ul> +<li><a href="#1">Heading 1</a> +<ul> +<li><a href="#1-1">Subheading 1.1 Shortcode: Hello</a></li> +</ul></li> +<li><a href="#2">Heading 2 Shortcode: Hello</a> +<ul> +<li><a href="#2-1">Subheading 2.1</a></li> +</ul></li> +</ul> +</nav>` + var multiSiteTOMLConfigTemplate = ` DefaultExtension = "html" baseurl = "http://example.com/blog" |