mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
Truncated; .Site.Params; First function
* Add `.Truncated` bool to each page; will be set true if the `.Summary` is truncated and it's worth showing a "more" link of some kind. * Add `Params` to the site config, defining `.Site.Params` accessible to each page; this lets the site maintainer associate arbitrary data with names, on a site-wide basis. * Provide a `First` function to templates: * Use-case: `{{range First 5 .Site.Recent}}` or anything else which is a simple iterable provided by hugolib * Tests by me for `.Truncated` and `First` Also @noahcampbell contributed towards this: * Add UnitTest for `.Site.Params`: > Digging into this test case a bit more, I'm realizing that we need > to create a param test case to ensure that for each type we render > (page, index, homepage, rss, etc.) that the proper fields are > represented. This will help us refactor without fear in the > future. Sample config.yaml: ```yaml title: "Test site" params: Subtitle: "More tests always good" AuthorName: "John Doe" SidebarRecentLimit: 5 ``` Signed-off-by: Noah Campbell <noahcampbell@gmail.com>
This commit is contained in:
committed by
Noah Campbell
parent
6017599a3c
commit
40d05f12a7
@@ -38,6 +38,7 @@ type Page struct {
|
||||
Images []string
|
||||
Content template.HTML
|
||||
Summary template.HTML
|
||||
Truncated bool
|
||||
plain string // TODO should be []byte
|
||||
Params map[string]interface{}
|
||||
contentType string
|
||||
@@ -94,21 +95,26 @@ func (p Page) Plain() string {
|
||||
return p.plain
|
||||
}
|
||||
|
||||
func getSummaryString(content []byte, fmt string) []byte {
|
||||
// nb: this is only called for recognised types; so while .html might work for
|
||||
// creating posts, it results in missing summaries.
|
||||
func getSummaryString(content []byte, pagefmt string) (summary []byte, truncates bool) {
|
||||
if bytes.Contains(content, summaryDivider) {
|
||||
// If user defines split:
|
||||
// Split then render
|
||||
return renderBytes(bytes.Split(content, summaryDivider)[0], fmt)
|
||||
truncates = true // by definition
|
||||
summary = renderBytes(bytes.Split(content, summaryDivider)[0], pagefmt)
|
||||
} else {
|
||||
// If hugo defines split:
|
||||
// render, strip html, then split
|
||||
plain := StripHTML(StripShortcodes(string(renderBytes(content, fmt))))
|
||||
return []byte(TruncateWordsToWholeSentence(plain, summaryLength))
|
||||
plain := strings.TrimSpace(StripHTML(StripShortcodes(string(renderBytes(content, pagefmt)))))
|
||||
summary = []byte(TruncateWordsToWholeSentence(plain, summaryLength))
|
||||
truncates = len(summary) != len(plain)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func renderBytes(content []byte, fmt string) []byte {
|
||||
switch fmt {
|
||||
func renderBytes(content []byte, pagefmt string) []byte {
|
||||
switch pagefmt {
|
||||
default:
|
||||
return blackfriday.MarkdownCommon(content)
|
||||
case "markdown":
|
||||
@@ -522,8 +528,9 @@ func (page *Page) convertMarkdown(lines io.Reader) {
|
||||
b.ReadFrom(lines)
|
||||
content := b.Bytes()
|
||||
page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
|
||||
summary := getSummaryString(content, "markdown")
|
||||
summary, truncated := getSummaryString(content, "markdown")
|
||||
page.Summary = template.HTML(string(summary))
|
||||
page.Truncated = truncated
|
||||
}
|
||||
|
||||
func (page *Page) convertRestructuredText(lines io.Reader) {
|
||||
@@ -531,8 +538,9 @@ func (page *Page) convertRestructuredText(lines io.Reader) {
|
||||
b.ReadFrom(lines)
|
||||
content := b.Bytes()
|
||||
page.Content = template.HTML(getRstContent(content))
|
||||
summary := getSummaryString(content, "rst")
|
||||
summary, truncated := getSummaryString(content, "rst")
|
||||
page.Summary = template.HTML(string(summary))
|
||||
page.Truncated = truncated
|
||||
}
|
||||
|
||||
func (p *Page) TargetPath() (outfile string) {
|
||||
|
Reference in New Issue
Block a user