Can I display the title only when different from content?

Hi,

I have some content (tweets imports) with title and content very similar; how can I hide the title when it is very similar (more than 90% the same characters as the content) ?

(I have a LOT of tweets, I could re-import them and rewrite them, but this looks like a lot of work)

Thanks

The easy way is to hide the title all the time. They’re microblog posts, should be fine.

I’ve no other suggestions. :slight_smile:

1 Like

In /layouts/tweet/single.html:

{{ define "main" }}

  {{- $lenTitle := float (len .Title) -}}
  {{- $lenContent := float (len (chomp .RawContent)) -}}
  {{- $ratioLen := div $lenTitle $lenContent -}}
  {{- $ratioThreshold := 0.90 -}}

  {{- if (not (in (lower .Content) (lower .Title))) }}
    <h1>{{ .Title }}</h1>
  {{- else -}}
    {{- if lt $ratioLen $ratioThreshold }}
      <h1>{{ .Title }}</h1>
    {{- end -}}
  {{- end }}

  {{ .Content }}

  <!-- Remove this div when you're done testing. -->
  <div style="color: green;">
    The title is {{ $lenTitle }} characters long.<br>
    The content is {{ $lenContent }} characters long.<br>
    The ratio of title length to content length is {{ printf "%3.1f%%" (mul 100 $ratioLen) }}.<br>
    The defined threshold ratio is {{ printf "%3.1f%%" (mul 100 $ratioThreshold) }}.<br>
    {{- if in (lower .Content) (lower .Title) }}
    The title appears within the content.<br>
    {{- else }}
    The title does not appear within the content.<br>
    {{- end -}}
    {{- if lt $ratioLen $ratioThreshold }}
    The ratio of title length to content length is less than the threshold ratio.
    {{- else }}
    The ratio of title length to content length is greater than or equal ot the threshold ratio.
    {{- end }}
  </div>

{{ end }}

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