|
18 | 18 | # You should have received a copy of the GNU General Public License along
|
19 | 19 | # with this program; if not, see <http://www.gnu.org/licences/>
|
20 | 20 |
|
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) |
30 | 39 | end
|
31 | 40 |
|
32 | 41 | module StaticComments
|
33 | 42 | # 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] |
37 | 45 | 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 |
43 | 49 | comments = Hash.new() { |h, k| h[k] = Array.new }
|
44 |
| - |
| 50 | + |
45 | 51 | Dir["#{source}/**/_comments/**/*"].sort.each do |comment|
|
46 | 52 | next unless File.file?(comment) and File.readable?(comment)
|
47 | 53 | yaml_data = YAML::load_file(comment)
|
48 | 54 | post_id = yaml_data.delete('post_id')
|
| 55 | + yaml_data["path"] = comment |
49 | 56 | comments[post_id] << yaml_data
|
50 | 57 | 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 |
53 | 64 | end
|
54 | 65 | end
|
0 commit comments