Skip to content

Commit

Permalink
Notice new files with watch; work with incremental
Browse files Browse the repository at this point in the history
with compatibility for 3.0+ from mpalmer#18
  • Loading branch information
mnot committed Feb 15, 2016
1 parent 4b30bd6 commit 77941d5
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions static_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,48 @@
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licences/>

class Jekyll::Post
alias :to_liquid_without_comments :to_liquid

def to_liquid(*args)
data = to_liquid_without_comments(*args)
data['comments'] = StaticComments::find_for_post(self)
data['comment_count'] = data['comments'].length
data
end
Jekyll::Hooks.register :site, :post_read do |site|
StaticComments::read_comments(site)

site.posts.docs.each do |post|
comments = StaticComments::find_for_post(post.id)
comments.each do |comment|
comment_path = comment["path"]
site.regenerator.add_dependency(post.path, comment_path)
end
end
end

Jekyll::Hooks.register :posts, :pre_render do |post, payload|
id = payload["page"]["id"]
comments = StaticComments::find_for_post(payload["page"]["id"])
payload["page"]["comments"] = comments
payload["page"]["comment_count"] = comments.length
payload["page"]["has_comments"] = (comments.length > 0)
end

module StaticComments
# Find all the comments for a post
def self.find_for_post(post)
@comments ||= read_comments(post.site.source)
@comments[post.id]
def self.find_for_post(post_id)
@comments[post_id]
end

# Read all the comments files in the site, and return them as a hash of
# arrays containing the comments, where the key to the array is the value
# of the 'post_id' field in the YAML data in the comments files.
def self.read_comments(source)

def self.read_comments(site)
source = site.source
comments = Hash.new() { |h, k| h[k] = Array.new }

Dir["#{source}/**/_comments/**/*"].sort.each do |comment|
next unless File.file?(comment) and File.readable?(comment)
yaml_data = YAML::load_file(comment)
post_id = yaml_data.delete('post_id')
yaml_data["path"] = comment
comments[post_id] << yaml_data
end

comments

comments.each_key do |key|
comments[key].sort!{|a,b| a['date'] <=> b['date'] }
end

@comments = comments
end
end

0 comments on commit 77941d5

Please sign in to comment.