hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit d9f54a13c14f12ccc8af33e9dbd611c2cd113324
parent 165edc7f0a84c345f23ee859653701bfb180cfd1
Author: Luca Corbatto <targodan@users.noreply.github.com>
Date:   Mon, 24 Oct 2016 14:40:57 +0200

Handle ToC before handling shortcodes

Fixes #2433 
Diffstat:
Mhugolib/hugo_sites.go | 8++++----
Mhugolib/hugo_sites_test.go | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 4 deletions(-)
diff --git 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
@@ -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"