Module: JekyllHtmlTruncatewords

Extended by:
JekyllHtmlTruncatewords, Liquid::StandardFilters
Included in:
JekyllHtmlTruncatewords
Defined in:
lib/jekyll_html_truncatewords.rb,
lib/jekyll_html_truncatewords/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#html_truncatewords(input, words = 15, truncate_string = "...") ⇒ Object

Truncate HTML input to have ‘words` words when rendered as HTML. Preserve HTML structure so tags are correctly matched.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jekyll_html_truncatewords.rb', line 14

def html_truncatewords(input, words = 15, truncate_string = "...")
  words = words.to_i
  fragment = Nokogiri::HTML.fragment(input)

  if fragment.inner_text.split.length <= words
    return input
  end

  truncate_node(fragment, words, truncate_string)

  fragment.to_html
end