Handle ToC before handling shortcodes

Fixes #2433
This commit is contained in:
Luca Corbatto 2016-10-24 14:40:57 +02:00 committed by Bjørn Erik Pedersen
parent 165edc7f0a
commit d9f54a13c1
2 changed files with 114 additions and 4 deletions

View File

@ -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

View File

@ -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"