content adapter: Fix issue with content starting out with a shortcode
Fixes #12544
This commit is contained in:
parent
7f3061723e
commit
519f41dbd7
4 changed files with 43 additions and 10 deletions
|
@ -57,14 +57,14 @@ type pageContentReplacement struct {
|
||||||
|
|
||||||
func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64) (*contentParseInfo, error) {
|
func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64) (*contentParseInfo, error) {
|
||||||
var (
|
var (
|
||||||
sourceKey string
|
sourceKey string
|
||||||
openSource hugio.OpenReadSeekCloser
|
openSource hugio.OpenReadSeekCloser
|
||||||
hasContent = m.pageConfig.IsFromContentAdapter
|
isFromContentAdapter = m.pageConfig.IsFromContentAdapter
|
||||||
)
|
)
|
||||||
|
|
||||||
if m.f != nil && !hasContent {
|
if m.f != nil && !isFromContentAdapter {
|
||||||
sourceKey = filepath.ToSlash(m.f.Filename())
|
sourceKey = filepath.ToSlash(m.f.Filename())
|
||||||
if !hasContent {
|
if !isFromContentAdapter {
|
||||||
meta := m.f.FileInfo().Meta()
|
meta := m.f.FileInfo().Meta()
|
||||||
openSource = func() (hugio.ReadSeekCloser, error) {
|
openSource = func() (hugio.ReadSeekCloser, error) {
|
||||||
r, err := meta.Open()
|
r, err := meta.Open()
|
||||||
|
@ -74,7 +74,7 @@ func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64) (*contentParseInfo
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if hasContent {
|
} else if isFromContentAdapter {
|
||||||
openSource = m.pageConfig.Content.ValueAsOpenReadSeekCloser()
|
openSource = m.pageConfig.Content.ValueAsOpenReadSeekCloser()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +96,9 @@ func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64) (*contentParseInfo
|
||||||
|
|
||||||
items, err := pageparser.ParseBytes(
|
items, err := pageparser.ParseBytes(
|
||||||
source,
|
source,
|
||||||
pageparser.Config{},
|
pageparser.Config{
|
||||||
|
NoFrontMatter: isFromContentAdapter,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -104,7 +106,7 @@ func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64) (*contentParseInfo
|
||||||
|
|
||||||
pi.itemsStep1 = items
|
pi.itemsStep1 = items
|
||||||
|
|
||||||
if hasContent {
|
if isFromContentAdapter {
|
||||||
// No front matter.
|
// No front matter.
|
||||||
return pi, nil
|
return pi, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -585,3 +585,28 @@ value: data1
|
||||||
|
|
||||||
b.AssertLogNotContains("WARN")
|
b.AssertLogNotContains("WARN")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPagesFromGoTmplShortcodeNoPreceddingCharacterIssue12544(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
files := `
|
||||||
|
-- hugo.toml --
|
||||||
|
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
|
||||||
|
-- content/_content.gotmpl --
|
||||||
|
{{ $content := dict "mediaType" "text/html" "value" "x{{< sc >}}" }}
|
||||||
|
{{ .AddPage (dict "content" $content "path" "a") }}
|
||||||
|
|
||||||
|
{{ $content := dict "mediaType" "text/html" "value" "{{< sc >}}" }}
|
||||||
|
{{ .AddPage (dict "content" $content "path" "b") }}
|
||||||
|
-- layouts/_default/single.html --
|
||||||
|
|{{ .Content }}|
|
||||||
|
-- layouts/shortcodes/sc.html --
|
||||||
|
foo
|
||||||
|
{{- /**/ -}}
|
||||||
|
`
|
||||||
|
|
||||||
|
b := hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/a/index.html", "|xfoo|")
|
||||||
|
b.AssertFileContent("public/b/index.html", "|foo|") // fails
|
||||||
|
}
|
||||||
|
|
|
@ -62,7 +62,9 @@ func (l *pageLexer) Input() []byte {
|
||||||
return l.input
|
return l.input
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct{}
|
type Config struct {
|
||||||
|
NoFrontMatter bool
|
||||||
|
}
|
||||||
|
|
||||||
// note: the input position here is normally 0 (start), but
|
// note: the input position here is normally 0 (start), but
|
||||||
// can be set if position of first shortcode is known
|
// can be set if position of first shortcode is known
|
||||||
|
|
|
@ -36,7 +36,11 @@ var _ Result = (*pageLexer)(nil)
|
||||||
|
|
||||||
// ParseBytes parses the page in b according to the given Config.
|
// ParseBytes parses the page in b according to the given Config.
|
||||||
func ParseBytes(b []byte, cfg Config) (Items, error) {
|
func ParseBytes(b []byte, cfg Config) (Items, error) {
|
||||||
l, err := parseBytes(b, cfg, lexIntroSection)
|
startLexer := lexIntroSection
|
||||||
|
if cfg.NoFrontMatter {
|
||||||
|
startLexer = lexMainSection
|
||||||
|
}
|
||||||
|
l, err := parseBytes(b, cfg, startLexer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue