HUGO

Any way to improve accuracy of my character count?

I have the following Markdown content file (the actual text was pasted from a new tweet window to confirm it is 280 characters):

+++
publishDate = 2020-06-05T14:08:12Z
title = "280 Char Lipsum"
+++
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras convallis sodales interdum. Aenean placerat arcu et varius fermentum. Duis vitae tincidunt nunc. Aenean auctor laoreet sem, ac blandit purus finibus ut. Maecenas auctor, leo in suscipit imperdiet, nibh quam ullamcorper

Then I have this testing snippet in a template:

{{- if eq .Section "microposts" -}}
  <p>RuneCount: {{ strings.RuneCount .Content }}</p>
  <p>plainify RuneCount: {{ (plainify .Content) | strings.RuneCount }}</p>
  <p>countrunes: {{ countrunes .Content }}</p>
{{- end -}}

That yields this rendered content:

  • RuneCount: 288
  • plainify RuneCount: 281
  • countrunes: 241

The plainify variant seems to get me closer to 280, I just wondered if there is anything I can do to get the count completely correct every time?

Is .Content the wrong thing to be operating on with the functions?

markdown (content is 2 bytes; X plus trailing newline)

+++
title = "Test"
date = 2021-01-01T00:00:00-00:00
draft = false
+++
X

.Content (9 bytes including trailing newline)

<p>X</p>
2 Likes

Wasn’t aware of the trailing newline. So would this be the best, most reliable option, or would there be a simpler way (I’m not sure of how to access the raw markdown inside the template, .Content is all I can think of)?

<p>Characters: {{ plainify .Content | chomp | strings.RuneCount }}</p>

Yes.

The raw markdown (.RawContent) will include markdown syntax characters, so it is not a good choice. For example, **bold** would be 8 characters instead of 4.

1 Like

Thanks

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.