hugolib: Fix ref/relref anhcor handling

Fixes #6481
This commit is contained in:
Bjørn Erik Pedersen 2019-11-06 09:20:59 +01:00
parent 8483b53aef
commit c26d00db64
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 67 additions and 4 deletions

View file

@ -16,9 +16,10 @@ package hugolib
import ( import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"reflect" "reflect"
"github.com/spf13/viper"
"github.com/gohugoio/hugo/parser/pageparser" "github.com/gohugoio/hugo/parser/pageparser"
"github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page"
@ -1195,3 +1196,65 @@ Get: {{ printf "%v (%T)" $b1 $b1 | safeHTML }}
"types string: - 0: true (string) - 1: trues (string) - 2: 33 (string) - 3: 3.14 (string) ", "types string: - 0: true (string) - 1: trues (string) - 2: 33 (string) - 3: 3.14 (string) ",
) )
} }
func TestShortcodeRef(t *testing.T) {
for _, plainIDAnchors := range []bool{false, true} {
plainIDAnchors := plainIDAnchors
t.Run(fmt.Sprintf("plainIDAnchors=%t", plainIDAnchors), func(t *testing.T) {
t.Parallel()
v := viper.New()
v.Set("baseURL", "https://example.org")
v.Set("blackfriday", map[string]interface{}{
"plainIDAnchors": plainIDAnchors,
})
builder := newTestSitesBuilder(t).WithViper(v)
for i := 1; i <= 2; i++ {
builder.WithContent(fmt.Sprintf("page%d.md", i), `---
title: "Hugo Rocks!"
---
[Page 1]({{< ref "page1.md" >}})
[Page 1 with anchor]({{< relref "page1.md#doc" >}})
[Page 2]({{< ref "page2.md" >}})
[Page 2 with anchor]({{< relref "page2.md#doc" >}})
## Doc
`)
}
builder.Build(BuildCfg{})
if plainIDAnchors {
builder.AssertFileContent("public/page2/index.html",
`
<a href="/page1/#doc">Page 1 with anchor</a>
<a href="https://example.org/page2/">Page 2</a>
<a href="/page2/#doc">Page 2 with anchor</a></p>
<h2 id="doc">Doc</h2>
`,
)
} else {
builder.AssertFileContent("public/page2/index.html",
`
<p><a href="https://example.org/page1/">Page 1</a>
<a href="/page1/#doc:45ca767ba77bc1445a0acab74f80812f">Page 1 with anchor</a>
<a href="https://example.org/page2/">Page 2</a>
<a href="/page2/#doc:8e3cdf52fa21e33270c99433820e46bd">Page 2 with anchor</a></p>
<h2 id="doc:8e3cdf52fa21e33270c99433820e46bd">Doc</h2>
`,
)
}
})
}
}

View file

@ -719,12 +719,12 @@ func (s *siteRefLinker) refLink(ref string, source interface{}, relative bool, o
var link string var link string
if refURL.Path != "" { if refURL.Path != "" {
target, err := s.s.getPageNew(p, refURL.Path) var err error
target, err = s.s.getPageNew(p, refURL.Path)
var pos text.Position var pos text.Position
if err != nil || target == nil { if err != nil || target == nil {
if p, ok := source.(text.Positioner); ok { if p, ok := source.(text.Positioner); ok {
pos = p.Position() pos = p.Position()
} }
} }
@ -758,8 +758,8 @@ func (s *siteRefLinker) refLink(ref string, source interface{}, relative bool, o
} }
if refURL.Fragment != "" { if refURL.Fragment != "" {
_ = target
link = link + "#" + refURL.Fragment link = link + "#" + refURL.Fragment
if pctx, ok := target.(pageContext); ok && !target.File().IsZero() && !pctx.getRenderingConfig().PlainIDAnchors { if pctx, ok := target.(pageContext); ok && !target.File().IsZero() && !pctx.getRenderingConfig().PlainIDAnchors {
if refURL.Path != "" { if refURL.Path != "" {
link = link + ":" + target.File().UniqueID() link = link + ":" + target.File().UniqueID()