mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Moved TOC test into page_test. Some general page_test cleanup.
This commit is contained in:
parent
9666f33e2f
commit
f45c6bc38a
2 changed files with 89 additions and 60 deletions
|
@ -131,13 +131,77 @@ Some more text
|
|||
SIMPLE_PAGE_WITH_LONG_CONTENT = `---
|
||||
title: Simple
|
||||
---
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
|
||||
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
|
||||
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
||||
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
|
||||
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
|
||||
culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit
|
||||
amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
|
||||
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
|
||||
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
|
||||
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
||||
officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet,
|
||||
consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
|
||||
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
|
||||
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
|
||||
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
|
||||
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
|
||||
deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur
|
||||
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
|
||||
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
|
||||
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
|
||||
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
|
||||
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
|
||||
id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
|
||||
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
|
||||
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
|
||||
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
|
||||
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
|
||||
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
|
||||
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
|
||||
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
|
||||
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
||||
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
|
||||
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
|
||||
culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit
|
||||
amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
|
||||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
|
||||
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
|
||||
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
|
||||
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
||||
officia deserunt mollit anim id est laborum.`
|
||||
|
||||
PAGE_WITH_TOC = `---
|
||||
title: TOC
|
||||
---
|
||||
For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
|
||||
|
||||
## AA
|
||||
|
||||
I have no idea, of course, how long it took me to reach the limit of the plain,
|
||||
but at last I entered the foothills, following a pretty little canyon upward
|
||||
toward the mountains. Beside me frolicked a laughing brooklet, hurrying upon
|
||||
its noisy way down to the silent sea. In its quieter pools I discovered many
|
||||
small fish, of four-or five-pound weight I should imagine. In appearance,
|
||||
except as to size and color, they were not unlike the whale of our own seas. As
|
||||
I watched them playing about I discovered, not only that they suckled their
|
||||
young, but that at intervals they rose to the surface to breathe as well as to
|
||||
feed upon certain grasses and a strange, scarlet lichen which grew upon the
|
||||
rocks just above the water line.
|
||||
|
||||
### AAA
|
||||
|
||||
I remember I felt an extraordinary persuasion that I was being played with,
|
||||
that presently, when I was upon the very verge of safety, this mysterious
|
||||
death--as swift as the passage of light--would leap after me from the pit about
|
||||
the cylinder and strike me down. ## BB
|
||||
|
||||
### BBB
|
||||
|
||||
"You're a great Granser," he cried delightedly, "always making believe them little marks mean something."
|
||||
`
|
||||
)
|
||||
|
||||
|
@ -184,13 +248,19 @@ func checkPageTitle(t *testing.T, page *Page, title string) {
|
|||
|
||||
func checkPageContent(t *testing.T, page *Page, content string) {
|
||||
if page.Content != template.HTML(content) {
|
||||
t.Fatalf("Page content mismatch\nexp: %q\ngot: %q", content, page.Content)
|
||||
t.Fatalf("Page content is: %q\nExpected: %q", page.Content, content)
|
||||
}
|
||||
}
|
||||
|
||||
func checkPageTOC(t *testing.T, page *Page, toc string) {
|
||||
if page.TableOfContents != template.HTML(toc) {
|
||||
t.Fatalf("Page TableOfContents is: %q.\nExpected %q", page.TableOfContents, toc)
|
||||
}
|
||||
}
|
||||
|
||||
func checkPageSummary(t *testing.T, page *Page, summary string) {
|
||||
if page.Summary != template.HTML(summary) {
|
||||
t.Fatalf("Page summary is: `%s`. Expected `%s`", page.Summary, summary)
|
||||
t.Fatalf("Page summary is: %q.\nExpected %q", page.Summary, summary)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -267,6 +337,16 @@ func TestPageWithShortCodeInSummary(t *testing.T) {
|
|||
checkPageLayout(t, p, "page/single.html", "single.html")
|
||||
}
|
||||
|
||||
func TestTableOfContents(t *testing.T) {
|
||||
p, err := ReadFrom(strings.NewReader(PAGE_WITH_TOC), "tocpage.md")
|
||||
p.Convert()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||
}
|
||||
checkPageContent(t, p, "\n\n<p>For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.</p>\n\n<h2 id=\"toc_0\">AA</h2>\n\n<p>I have no idea, of course, how long it took me to reach the limit of the plain,\nbut at last I entered the foothills, following a pretty little canyon upward\ntoward the mountains. Beside me frolicked a laughing brooklet, hurrying upon\nits noisy way down to the silent sea. In its quieter pools I discovered many\nsmall fish, of four-or five-pound weight I should imagine. In appearance,\nexcept as to size and color, they were not unlike the whale of our own seas. As\nI watched them playing about I discovered, not only that they suckled their\nyoung, but that at intervals they rose to the surface to breathe as well as to\nfeed upon certain grasses and a strange, scarlet lichen which grew upon the\nrocks just above the water line.</p>\n\n<h3 id=\"toc_1\">AAA</h3>\n\n<p>I remember I felt an extraordinary persuasion that I was being played with,\nthat presently, when I was upon the very verge of safety, this mysterious\ndeath—as swift as the passage of light—would leap after me from the pit about\nthe cylinder and strike me down. ## BB</p>\n\n<h3 id=\"toc_2\">BBB</h3>\n\n<p>“You’re a great Granser,” he cried delightedly, “always making believe them little marks mean something.”</p>\n")
|
||||
checkPageTOC(t, p, "<nav id=\"TableOfContents\">\n<ul>\n<li>\n<ul>\n<li><a href=\"#toc_0\">AA</a>\n<ul>\n<li><a href=\"#toc_1\">AAA</a></li>\n<li><a href=\"#toc_2\">BBB</a></li>\n</ul></li>\n</ul></li>\n</ul>\n</nav>")
|
||||
}
|
||||
|
||||
func TestPageWithMoreTag(t *testing.T) {
|
||||
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple.md")
|
||||
p.Convert()
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
package hugolib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTableOfContents(t *testing.T) {
|
||||
text := `
|
||||
Blah blah blah blah blah.
|
||||
|
||||
## AA
|
||||
|
||||
Blah blah blah blah blah.
|
||||
|
||||
### AAA
|
||||
|
||||
Blah blah blah blah blah.
|
||||
|
||||
## BB
|
||||
|
||||
Blah blah blah blah blah.
|
||||
|
||||
### BBB
|
||||
|
||||
Blah blah blah blah blah.
|
||||
`
|
||||
|
||||
markdown := RemoveSummaryDivider([]byte(text))
|
||||
toc := string(tableOfContentsFromBytes(markdown))
|
||||
|
||||
expected := `<nav>
|
||||
<ul>
|
||||
<li>
|
||||
<ul>
|
||||
<li><a href="#toc_0">AA</a>
|
||||
<ul>
|
||||
<li><a href="#toc_1">AAA</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#toc_2">BB</a>
|
||||
<ul>
|
||||
<li><a href="#toc_3">BBB</a></li>
|
||||
</ul></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
</nav>
|
||||
`
|
||||
|
||||
if toc != expected {
|
||||
t.Errorf("Expected table of contents: %s, got: %s", expected, toc)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue