From 2c7407ebfd922d5dc5330945c26213183fe54e08 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 12 Oct 2016 17:44:57 +1030 Subject: [PATCH] Fix behaviour on empty elements --- smart_truncate.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/smart_truncate.rb b/smart_truncate.rb index 03a8ac1..4154260 100644 --- a/smart_truncate.rb +++ b/smart_truncate.rb @@ -1,3 +1,4 @@ +# coding: utf-8 # smart_truncate: A HTML-aware word-truncating filter for Jekyll/Liquid # Copyright © 2016 RunasSudo (Yingtong Li) # @@ -59,16 +60,23 @@ module Jekyll elsif doc.name == 'table' return smart_truncate_table(doc, num_words) else - count = 0 - doc.children.each do |child| - count += smart_truncate_doc(child, num_words - count) - end - - if doc.children.length == 0 + if num_words > 0 + children_orig = doc.children.length + + count = 0 + doc.children.each do |child| + count += smart_truncate_doc(child, num_words - count) + end + + if doc.children.length == 0 && children_orig != 0 + doc.remove() + end + + return count + else doc.remove() + return 0 end - - return count end end