I’m trying to truncate/limit my blog posts to a preview, and do so by line count. The aim is to have the front matter as follows:
[params]
preview = true
preview_lines = 7
And then following that, the layout would have code that would conditionally show only 7 lines of content if that preview is set to true.
I’ve come up with code that seems to do most of what I need, but the issue is that it keeps spitting out all markdown lines, but skips over anything wrapped in html code, including simple <p>
s or <span>
s
Here’s the code I have so far. I’ve played around with options with markdownify, but that ends up hiding all lines entirely. Thanks for your help!
{{ if .Params.preview }}
{{ $content := .Content }}
{{ $lines := default 7 .Params.preview_lines }}
{{ $splitContent := split $content "\n" }}
{{ if gt (len $splitContent) $lines }}
{{ $truncated := delimit (first $lines $splitContent) "\n" }}
{{ $truncated | safeHTML }}
<span>...</span>
{{ else }}
{{ $content | safeHTML }}
{{ end }}
{{ else }}
{{ .Content | safeHTML }}
{{ end }}