summaryrefslogtreecommitdiffstats
path: root/helpers
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-04-11 13:17:25 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-04-11 13:17:25 +0200
commitcd558958a0c0ecd06f7560a38e27334fe983e0de (patch)
treeb6840ee6381ed7b053fcdce22e2d27e6a6d9a400 /helpers
parent5ef52294f90c51697bd3f918b3c3ed83baff657a (diff)
Use Node.ID for anchor ID
Fixes #2057
Diffstat (limited to 'helpers')
-rw-r--r--helpers/content.go19
-rw-r--r--helpers/content_test.go10
2 files changed, 15 insertions, 14 deletions
diff --git a/helpers/content.go b/helpers/content.go
index f60349055..3ac91b360 100644
--- a/helpers/content.go
+++ b/helpers/content.go
@@ -23,6 +23,10 @@ import (
"os/exec"
"unicode/utf8"
+ "fmt"
+ "strings"
+ "sync"
+
"github.com/miekg/mmark"
"github.com/mitchellh/mapstructure"
"github.com/russross/blackfriday"
@@ -30,9 +34,6 @@ import (
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.
@@ -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
@@ -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
}
@@ -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
diff --git a/helpers/content_test.go b/helpers/content_test.go
index a89b4992e..347884f0a 100644
--- a/helpers/content_test.go
+++ b/helpers/content_test.go
@@ -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)
@@ -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)