Skip to content

changes for jekyll 3.0 #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions static_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,43 @@
# 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
Jekyll::Hooks.register :posts, :pre_render do |post, payload|
# code to call before Jekyll renders a post
id = payload["page"]["id"]
comments = StaticComments::find_for_post(payload["page"]["id"], payload["site"]["source"])
payload["page"]["comments"] = comments
payload["page"]["comment_count"] = comments.length
payload["page"]["has_comments"] = (comments.length > 0)
end

Jekyll::Hooks.register :site, :pre_render do |post, payload|
payload["site"]["posts"].each do |d|
comments = StaticComments::find_for_post(d.id, payload["site"]["source"])

d.data["comment_count"] = comments.length
end
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, site)
@comments ||= read_comments(site)
@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)
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')
comments[post_id] << yaml_data
end

comments
end
end