Skip to content

Commit

Permalink
Use Node.ID for anchor ID
Browse files Browse the repository at this point in the history
Fixes #2057
  • Loading branch information
bep committed Apr 11, 2016
1 parent 5ef5229 commit cd55895
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 45 deletions.
19 changes: 10 additions & 9 deletions helpers/content.go
Expand Up @@ -23,16 +23,17 @@ import (
"os/exec"
"unicode/utf8"

"fmt"
"strings"
"sync"

"github.com/miekg/mmark"
"github.com/mitchellh/mapstructure"
"github.com/russross/blackfriday"
"github.com/spf13/cast"
bp "github.com/spf13/hugo/bufferpool"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"

"strings"
"sync"
)

// SummaryLength is the length of the summary that Hugo extracts from a content.
Expand Down Expand Up @@ -167,11 +168,11 @@ func getHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Render
FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
}

b := len(ctx.DocumentID) != 0
b := ctx.DocumentID != 0

if b && !ctx.getConfig().PlainIDAnchors {
renderParameters.FootnoteAnchorPrefix = ctx.DocumentID + ":" + renderParameters.FootnoteAnchorPrefix
renderParameters.HeaderIDSuffix = ":" + ctx.DocumentID
renderParameters.FootnoteAnchorPrefix = fmt.Sprintf("%d:%s", ctx.DocumentID, renderParameters.FootnoteAnchorPrefix)
renderParameters.HeaderIDSuffix = fmt.Sprintf(":%d", ctx.DocumentID)
}

htmlFlags := defaultFlags
Expand Down Expand Up @@ -258,10 +259,10 @@ func getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContext) mmark.Rendere
FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
}

b := len(ctx.DocumentID) != 0
b := ctx.DocumentID != 0

if b && !ctx.getConfig().PlainIDAnchors {
renderParameters.FootnoteAnchorPrefix = ctx.DocumentID + ":" + renderParameters.FootnoteAnchorPrefix
renderParameters.FootnoteAnchorPrefix = fmt.Sprintf("%d:%s", ctx.DocumentID, renderParameters.FootnoteAnchorPrefix)
// renderParameters.HeaderIDSuffix = ":" + ctx.DocumentId
}

Expand Down Expand Up @@ -343,7 +344,7 @@ func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
type RenderingContext struct {
Content []byte
PageFmt string
DocumentID string
DocumentID int
Config *Blackfriday
FileResolver FileResolverFunc
LinkResolver LinkResolverFunc
Expand Down
10 changes: 5 additions & 5 deletions helpers/content_test.go
Expand Up @@ -172,15 +172,15 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {

func TestGetHTMLRendererAnchors(t *testing.T) {
ctx := &RenderingContext{}
ctx.DocumentID = "testid"
ctx.DocumentID = 123
ctx.Config = ctx.getConfig()
ctx.Config.PlainIDAnchors = false

actualRenderer := getHTMLRenderer(0, ctx)
headerBuffer := &bytes.Buffer{}
footnoteBuffer := &bytes.Buffer{}
expectedFootnoteHref := []byte("href=\"#fn:testid:href\"")
expectedHeaderID := []byte("<h1 id=\"id:testid\"></h1>\n")
expectedFootnoteHref := []byte("href=\"#fn:123:href\"")
expectedHeaderID := []byte("<h1 id=\"id:123\"></h1>\n")

actualRenderer.Header(headerBuffer, func() bool { return true }, 1, "id")
actualRenderer.FootnoteRef(footnoteBuffer, []byte("href"), 1)
Expand All @@ -196,14 +196,14 @@ func TestGetHTMLRendererAnchors(t *testing.T) {

func TestGetMmarkHTMLRenderer(t *testing.T) {
ctx := &RenderingContext{}
ctx.DocumentID = "testid"
ctx.DocumentID = 321
ctx.Config = ctx.getConfig()
ctx.Config.PlainIDAnchors = false
actualRenderer := getMmarkHTMLRenderer(0, ctx)

headerBuffer := &bytes.Buffer{}
footnoteBuffer := &bytes.Buffer{}
expectedFootnoteHref := []byte("href=\"#fn:testid:href\"")
expectedFootnoteHref := []byte("href=\"#fn:321:href\"")
expectedHeaderID := []byte("<h1 id=\"id\"></h1>")

actualRenderer.FootnoteRef(footnoteBuffer, []byte("href"), 1)
Expand Down
10 changes: 5 additions & 5 deletions hugolib/handler_test.go
Expand Up @@ -25,8 +25,8 @@ import (
)

func TestDefaultHandler(t *testing.T) {
viper.Reset()
defer viper.Reset()
setUp()
defer tearDown()

hugofs.InitMemFs()
sources := []source.ByteSource{
Expand Down Expand Up @@ -63,14 +63,14 @@ func TestDefaultHandler(t *testing.T) {
doc string
expected string
}{
{filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title:5d74edbb89ef198cd37882b687940cda\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title:42\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"},
{filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3:28c75a9e2162b8eccda73a1ab9ce80b4\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
{filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3:42\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
{filepath.FromSlash("sect/doc3/img1.png"), string([]byte("‰PNG  ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚"))},
{filepath.FromSlash("sect/img2.gif"), string([]byte("GIF89a��€��ÿÿÿ���,�������D�;"))},
{filepath.FromSlash("sect/img2.spf"), string([]byte("****FAKE-FILETYPE****"))},
{filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"},
{filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title:0ae308ad73e2f37bd09874105281b5d8\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title:42\">title</h1>\n\n<p>some <em>content</em></p>\n"},
}

for _, test := range tests {
Expand Down
34 changes: 34 additions & 0 deletions hugolib/hugolib_test.go
@@ -0,0 +1,34 @@
// Copyright 2016 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
"github.com/spf13/viper"
)

var stableNodeIDProvider nodeIDProviderFunc = func(n *Node) int {
return 42
}

// common test setup.
func setUp() {
viper.Reset()
nodeIDProvider = stableNodeIDProvider
}

// common test cleanup.
func tearDown() {
viper.Reset()
nodeIDProvider = defaultNodeIDProvider
}
12 changes: 10 additions & 2 deletions hugolib/node.go
Expand Up @@ -47,6 +47,15 @@ type Node struct {
// but that would lead to massive changes; do it simple for now.
var nodeIDCounter uint64

type nodeIDProviderFunc func(n *Node) int

var defaultNodeIDProvider nodeIDProviderFunc = func(n *Node) int {
n.idInit.Do(func() { n.id = nextNodeID() })
return n.id
}

var nodeIDProvider nodeIDProviderFunc = defaultNodeIDProvider

func nextNodeID() int {
return int(atomic.AddUint64(&nodeIDCounter, 1))
}
Expand All @@ -55,8 +64,7 @@ func nextNodeID() int {
// This is unique for a given Hugo build, but must not be considered stable.
// See UniqueID on Page for an identify that is stable for repeated builds.
func (n *Node) ID() int {
n.idInit.Do(func() { n.id = nextNodeID() })
return n.id
return nodeIDProvider(n)
}

func (n *Node) Now() time.Time {
Expand Down
17 changes: 7 additions & 10 deletions hugolib/node_test.go
Expand Up @@ -43,23 +43,20 @@ func TestNodeSimpleMethods(t *testing.T) {
}

func TestNodeID(t *testing.T) {
t.Parallel()

n1 := &Node{}
n2 := &Node{}

assert.True(t, n1.ID() > 0)
assert.Equal(t, n1.ID(), n1.ID())
assert.True(t, n2.ID() > n1.ID())

var wg sync.WaitGroup

for i := 1; i <= 10; i++ {
wg.Add(1)
go func(j int) {
for k := 0; k < 10; k++ {
n := &Node{}
assert.True(t, n.ID() > 0)
n1 := &Node{}
n2 := &Node{}

assert.True(t, n1.ID() > 0)
assert.Equal(t, n1.ID(), n1.ID())
assert.True(t, n2.ID() > n1.ID())

}
wg.Done()
}(i)
Expand Down
4 changes: 2 additions & 2 deletions hugolib/page.go
Expand Up @@ -265,7 +265,7 @@ func (p *Page) renderBytes(content []byte) []byte {
}
return helpers.RenderBytes(
&helpers.RenderingContext{Content: content, PageFmt: p.determineMarkupType(),
DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
DocumentID: p.ID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
}

func (p *Page) renderContent(content []byte) []byte {
Expand All @@ -280,7 +280,7 @@ func (p *Page) renderContent(content []byte) []byte {
}
}
return helpers.RenderBytesWithTOC(&helpers.RenderingContext{Content: content, PageFmt: p.determineMarkupType(),
DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
DocumentID: p.ID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
}

func (p *Page) getRenderingConfig() *helpers.Blackfriday {
Expand Down
7 changes: 5 additions & 2 deletions hugolib/page_test.go
Expand Up @@ -605,14 +605,17 @@ func TestPageWithAdditionalExtension(t *testing.T) {
}

func TestTableOfContents(t *testing.T) {
setUp()
defer tearDown()

p, _ := NewPage("tocpage.md")
_, err := p.ReadFrom(strings.NewReader(pageWithToC))
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=\"aa:90b9174a5bdb091a9625b04adac96ca6\">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=\"aaa:90b9174a5bdb091a9625b04adac96ca6\">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&ndash;as swift as the passage of light&ndash;would leap after me from the pit about\nthe cylinder and strike me down. ## BB</p>\n\n<h3 id=\"bbb:90b9174a5bdb091a9625b04adac96ca6\">BBB</h3>\n\n<p>&ldquo;You&rsquo;re a great Granser,&rdquo; he cried delightedly, &ldquo;always making believe them little marks mean something.&rdquo;</p>\n")
checkPageTOC(t, p, "<nav id=\"TableOfContents\">\n<ul>\n<li>\n<ul>\n<li><a href=\"#aa:90b9174a5bdb091a9625b04adac96ca6\">AA</a>\n<ul>\n<li><a href=\"#aaa:90b9174a5bdb091a9625b04adac96ca6\">AAA</a></li>\n<li><a href=\"#bbb:90b9174a5bdb091a9625b04adac96ca6\">BBB</a></li>\n</ul></li>\n</ul></li>\n</ul>\n</nav>")
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=\"aa:42\">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=\"aaa:42\">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&ndash;as swift as the passage of light&ndash;would leap after me from the pit about\nthe cylinder and strike me down. ## BB</p>\n\n<h3 id=\"bbb:42\">BBB</h3>\n\n<p>&ldquo;You&rsquo;re a great Granser,&rdquo; he cried delightedly, &ldquo;always making believe them little marks mean something.&rdquo;</p>\n")
checkPageTOC(t, p, "<nav id=\"TableOfContents\">\n<ul>\n<li>\n<ul>\n<li><a href=\"#aa:42\">AA</a>\n<ul>\n<li><a href=\"#aaa:42\">AAA</a></li>\n<li><a href=\"#bbb:42\">BBB</a></li>\n</ul></li>\n</ul></li>\n</ul>\n</nav>")
}

func TestPageWithMoreTag(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/shortcode.go
Expand Up @@ -235,7 +235,7 @@ func renderShortcode(sc shortcode, parent *ShortcodeWithPage, p *Page, t tpl.Tem
if sc.doMarkup {
newInner := helpers.RenderBytes(&helpers.RenderingContext{
Content: []byte(inner), PageFmt: p.determineMarkupType(),
DocumentID: p.UniqueID(), Config: p.getRenderingConfig()})
DocumentID: p.ID(), Config: p.getRenderingConfig()})

// If the type is “unknown” or “markdown”, we assume the markdown
// generation has been performed. Given the input: `a line`, markdown
Expand Down
9 changes: 7 additions & 2 deletions hugolib/shortcode_test.go
Expand Up @@ -168,6 +168,9 @@ func TestInnerSC(t *testing.T) {
}

func TestInnerSCWithMarkdown(t *testing.T) {
setUp()
defer tearDown()

tem := tpl.New()
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)

Expand All @@ -176,10 +179,12 @@ func TestInnerSCWithMarkdown(t *testing.T) {
[link](http://spf13.com) and text
{{% /inside %}}`, "<div><h1 id=\"more-here:bec3ed8ba720b9073ab75abcf3ba5d97\">More Here</h1>\n\n<p><a href=\"http://spf13.com\">link</a> and text</p>\n</div>", tem)
{{% /inside %}}`, "<div><h1 id=\"more-here:42\">More Here</h1>\n\n<p><a href=\"http://spf13.com\">link</a> and text</p>\n</div>", tem)
}

func TestInnerSCWithAndWithoutMarkdown(t *testing.T) {
setUp()
defer tearDown()
tem := tpl.New()
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)

Expand All @@ -198,7 +203,7 @@ And then:
This is **plain** text.
{{< /inside >}}
`, "<div><h1 id=\"more-here:bec3ed8ba720b9073ab75abcf3ba5d97\">More Here</h1>\n\n<p><a href=\"http://spf13.com\">link</a> and text</p>\n</div>\n\nAnd then:\n\n<div>\n# More Here\n\nThis is **plain** text.\n\n</div>\n", tem)
`, "<div><h1 id=\"more-here:42\">More Here</h1>\n\n<p><a href=\"http://spf13.com\">link</a> and text</p>\n</div>\n\nAnd then:\n\n<div>\n# More Here\n\nThis is **plain** text.\n\n</div>\n", tem)
}

func TestEmbeddedSC(t *testing.T) {
Expand Down
17 changes: 10 additions & 7 deletions hugolib/site_test.go
Expand Up @@ -142,14 +142,17 @@ func NopCloser(w io.Writer) io.WriteCloser {
}

func TestRenderThing(t *testing.T) {
setUp()
defer tearDown()

tests := []struct {
content string
template string
expected string
}{
{pageSimpleTitle, templateTitle, "simple template"},
{pageSimpleTitle, templateFunc, "simple-template"},
{pageWithMd, templateContent, "\n\n<h1 id=\"heading-1:91b5c4a22fc6103c73bb91e4a40568f8\">heading 1</h1>\n\n<p>text</p>\n\n<h2 id=\"heading-2:91b5c4a22fc6103c73bb91e4a40568f8\">heading 2</h2>\n\n<p>more text</p>\n"},
{pageWithMd, templateContent, "\n\n<h1 id=\"heading-1:42\">heading 1</h1>\n\n<p>text</p>\n\n<h2 id=\"heading-2:42\">heading 2</h2>\n\n<p>more text</p>\n"},
{simplePageRFC3339Date, templateDate, "2013-05-17 16:59:30 &#43;0000 UTC"},
}

Expand Down Expand Up @@ -556,8 +559,8 @@ func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {

}
func TestSkipRender(t *testing.T) {
viper.Reset()
defer viper.Reset()
setUp()
defer tearDown()

hugofs.InitMemFs()
sources := []source.ByteSource{
Expand Down Expand Up @@ -593,14 +596,14 @@ func TestSkipRender(t *testing.T) {
doc string
expected string
}{
{filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title:5d74edbb89ef198cd37882b687940cda\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title:42\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"},
{filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3:28c75a9e2162b8eccda73a1ab9ce80b4\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
{filepath.FromSlash("sect/doc4.html"), "\n\n<h1 id=\"doc4:f8e6806123f341b8975509637645a4d3\">doc4</h1>\n\n<p><em>some content</em></p>\n"},
{filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3:42\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
{filepath.FromSlash("sect/doc4.html"), "\n\n<h1 id=\"doc4:42\">doc4</h1>\n\n<p><em>some content</em></p>\n"},
{filepath.FromSlash("sect/doc5.html"), "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
{filepath.FromSlash("sect/doc6.html"), "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
{filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"},
{filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title:0ae308ad73e2f37bd09874105281b5d8\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title:42\">title</h1>\n\n<p>some <em>content</em></p>\n"},
}

for _, test := range tests {
Expand Down

0 comments on commit cd55895

Please sign in to comment.