content adapter: Handle <!--more--> separator in content.value

Closes #12556
This commit is contained in:
Bjørn Erik Pedersen 2024-06-01 10:27:10 +02:00
parent 74b9b8a229
commit 0221ddb39e
6 changed files with 75 additions and 37 deletions

View file

@ -307,7 +307,10 @@ func (pco *pageContentOutput) RenderString(ctx context.Context, args ...any) (te
if pageparser.HasShortcode(contentToRender) {
contentToRenderb := []byte(contentToRender)
// String contains a shortcode.
parseInfo.itemsStep1, err = pageparser.ParseBytesMain(contentToRenderb, pageparser.Config{})
parseInfo.itemsStep1, err = pageparser.ParseBytes(contentToRenderb, pageparser.Config{
NoFrontMatter: true,
NoSummaryDivider: true,
})
if err != nil {
return "", err
}

View file

@ -643,3 +643,29 @@ Footer: {{ range index site.Menus.footer }}{{ .Name }}|{{ end }}|
"Footer: Footer|p2||",
)
}
func TestPagesFromGoTmplMore(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
[markup.goldmark.renderer]
unsafe = true
-- content/s1/_content.gotmpl --
{{ $page := dict
"content" (dict "mediaType" "text/markdown" "value" "aaa <!--more--> bbb")
"title" "p1"
"path" "p1"
}}
{{ .AddPage $page }}
-- layouts/_default/single.html --
summary: {{ .Summary }}|content: {{ .Content}}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/s1/p1/index.html",
"<p>aaa</p>|content: <p>aaa</p>\n<p>bbb</p>",
)
}

View file

@ -64,6 +64,7 @@ func (l *pageLexer) Input() []byte {
type Config struct {
NoFrontMatter bool
NoSummaryDivider bool
}
// note: the input position here is normally 0 (start), but
@ -72,6 +73,7 @@ func newPageLexer(input []byte, stateStart stateFunc, cfg Config) *pageLexer {
lexer := &pageLexer{
input: input,
stateStart: stateStart,
summaryDivider: summaryDivider,
cfg: cfg,
lexerShortcodeState: lexerShortcodeState{
currLeftDelimItem: tLeftDelimScNoMarkup,
@ -297,6 +299,8 @@ func (s *sectionHandlers) skip() int {
}
func createSectionHandlers(l *pageLexer) *sectionHandlers {
handlers := make([]*sectionHandler, 0, 2)
shortCodeHandler := &sectionHandler{
l: l,
skipFunc: func(l *pageLexer) int {
@ -332,10 +336,13 @@ func createSectionHandlers(l *pageLexer) *sectionHandlers {
},
}
handlers = append(handlers, shortCodeHandler)
if !l.cfg.NoSummaryDivider {
summaryDividerHandler := &sectionHandler{
l: l,
skipFunc: func(l *pageLexer) int {
if l.summaryDividerChecked || l.summaryDivider == nil {
if l.summaryDividerChecked {
return -1
}
return l.index(l.summaryDivider)
@ -355,7 +362,9 @@ func createSectionHandlers(l *pageLexer) *sectionHandlers {
},
}
handlers := []*sectionHandler{shortCodeHandler, summaryDividerHandler}
handlers = append(handlers, summaryDividerHandler)
}
return &sectionHandlers{
l: l,

View file

@ -14,8 +14,6 @@
package pageparser
func lexIntroSection(l *pageLexer) stateFunc {
l.summaryDivider = summaryDivider
LOOP:
for {
r := l.next()

View file

@ -47,15 +47,6 @@ func ParseBytes(b []byte, cfg Config) (Items, error) {
return l.items, l.err
}
// ParseBytesMain parses b starting with the main section.
func ParseBytesMain(b []byte, cfg Config) (Items, error) {
l, err := parseBytes(b, cfg, lexMainSection)
if err != nil {
return nil, err
}
return l.items, l.err
}
type ContentFrontMatter struct {
Content []byte
FrontMatter map[string]any

View file

@ -101,3 +101,14 @@ func BenchmarkHasShortcode(b *testing.B) {
}
})
}
func TestSummaryDividerStartingFromMain(t *testing.T) {
c := qt.New(t)
input := `aaa <!--more--> bbb`
items, err := collectStringMain(input)
c.Assert(err, qt.IsNil)
c.Assert(items, qt.HasLen, 4)
c.Assert(items[1].Type, qt.Equals, TypeLeadSummaryDivider)
}