ref: d9f54a13c14f12ccc8af33e9dbd611c2cd113324
parent: 165edc7f0a84c345f23ee859653701bfb180cfd1
	author: Luca Corbatto <targodan@users.noreply.github.com>
	date: Mon Oct 24 10:40:57 EDT 2016
	
Handle ToC before handling shortcodes Fixes #2433
--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -466,14 +466,14 @@
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" {--- a/hugolib/hugo_sites_test.go
+++ b/hugolib/hugo_sites_test.go
@@ -3,6 +3,7 @@
import (
"bytes"
"fmt"
+ "regexp"
"strings"
"testing"
@@ -172,6 +173,16 @@
}
}
+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 {@@ -659,6 +670,105 @@
assertFileContent(t, "public/fr/sect/doc1/index.html", true, "Single", "Bonjour")
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"
--
⑨