summaryrefslogtreecommitdiffstats
path: root/helpers/content.go
diff options
context:
space:
mode:
authorJim McDonald <Jim@mcdee.net>2019-04-05 18:11:04 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-05 19:11:04 +0200
commit3a62d54745e2cbfda6772390830042908d725c71 (patch)
treed039be3ac163530fa292c0bfbd50f364fad57808 /helpers/content.go
parentebab291c0e321d23b098684bacaf830a3979e310 (diff)
hugolib: Consider summary in front matter for .Summary
Add the ability to have a `summary` page variable that overrides the auto-generated summary. Logic for obtaining summary becomes: * if summary divider is present in content, use the text above it * if summary variables is present in page metadata, use that * auto-generate summary from first _x_ words of the content Fixes #5800
Diffstat (limited to 'helpers/content.go')
-rw-r--r--helpers/content.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/helpers/content.go b/helpers/content.go
index be5090c21..3892647bb 100644
--- a/helpers/content.go
+++ b/helpers/content.go
@@ -42,6 +42,12 @@ import (
// SummaryDivider denotes where content summarization should end. The default is "<!--more-->".
var SummaryDivider = []byte("<!--more-->")
+var (
+ openingPTag = []byte("<p>")
+ closingPTag = []byte("</p>")
+ paragraphIndicator = []byte("<p")
+)
+
// ContentSpec provides functionality to render markdown content.
type ContentSpec struct {
BlackFriday *BlackFriday
@@ -580,6 +586,21 @@ func (c *ContentSpec) TruncateWordsToWholeSentence(s string) (string, bool) {
return strings.TrimSpace(s[:endIndex]), endIndex < len(s)
}
+// TrimShortHTML removes the <p>/</p> tags from HTML input in the situation
+// where said tags are the only <p> tags in the input and enclose the content
+// of the input (whitespace excluded).
+func (c *ContentSpec) TrimShortHTML(input []byte) []byte {
+ first := bytes.Index(input, paragraphIndicator)
+ last := bytes.LastIndex(input, paragraphIndicator)
+ if first == last {
+ input = bytes.TrimSpace(input)
+ input = bytes.TrimPrefix(input, openingPTag)
+ input = bytes.TrimSuffix(input, closingPTag)
+ input = bytes.TrimSpace(input)
+ }
+ return input
+}
+
func isEndOfSentence(r rune) bool {
return r == '.' || r == '?' || r == '!' || r == '"' || r == '\n'
}