Skip to content

Commit 77941d5

Browse files
committed
Notice new files with watch; work with incremental
with compatibility for 3.0+ from mpalmer#18
1 parent 4b30bd6 commit 77941d5

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

static_comments.rb

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,48 @@
1818
# You should have received a copy of the GNU General Public License along
1919
# with this program; if not, see <http://www.gnu.org/licences/>
2020

21-
class Jekyll::Post
22-
alias :to_liquid_without_comments :to_liquid
23-
24-
def to_liquid(*args)
25-
data = to_liquid_without_comments(*args)
26-
data['comments'] = StaticComments::find_for_post(self)
27-
data['comment_count'] = data['comments'].length
28-
data
29-
end
21+
Jekyll::Hooks.register :site, :post_read do |site|
22+
StaticComments::read_comments(site)
23+
24+
site.posts.docs.each do |post|
25+
comments = StaticComments::find_for_post(post.id)
26+
comments.each do |comment|
27+
comment_path = comment["path"]
28+
site.regenerator.add_dependency(post.path, comment_path)
29+
end
30+
end
31+
end
32+
33+
Jekyll::Hooks.register :posts, :pre_render do |post, payload|
34+
id = payload["page"]["id"]
35+
comments = StaticComments::find_for_post(payload["page"]["id"])
36+
payload["page"]["comments"] = comments
37+
payload["page"]["comment_count"] = comments.length
38+
payload["page"]["has_comments"] = (comments.length > 0)
3039
end
3140

3241
module StaticComments
3342
# Find all the comments for a post
34-
def self.find_for_post(post)
35-
@comments ||= read_comments(post.site.source)
36-
@comments[post.id]
43+
def self.find_for_post(post_id)
44+
@comments[post_id]
3745
end
38-
39-
# Read all the comments files in the site, and return them as a hash of
40-
# arrays containing the comments, where the key to the array is the value
41-
# of the 'post_id' field in the YAML data in the comments files.
42-
def self.read_comments(source)
46+
47+
def self.read_comments(site)
48+
source = site.source
4349
comments = Hash.new() { |h, k| h[k] = Array.new }
44-
50+
4551
Dir["#{source}/**/_comments/**/*"].sort.each do |comment|
4652
next unless File.file?(comment) and File.readable?(comment)
4753
yaml_data = YAML::load_file(comment)
4854
post_id = yaml_data.delete('post_id')
55+
yaml_data["path"] = comment
4956
comments[post_id] << yaml_data
5057
end
51-
52-
comments
58+
59+
comments.each_key do |key|
60+
comments[key].sort!{|a,b| a['date'] <=> b['date'] }
61+
end
62+
63+
@comments = comments
5364
end
5465
end

0 commit comments

Comments
 (0)