From 6167c095693edd0ba30c4c5b33bc87daad27acd2 Mon Sep 17 00:00:00 2001 From: Stephen Checkoway Date: Thu, 28 Jul 2016 14:07:03 -0500 Subject: [PATCH] Run hooks in priority order. Low priority hooks are being run before higher priority hooks. This is easy to demonstrate with the following plugin: 1.upto(10).each do |n| Jekyll::Hooks.register :site, :after_reset, priority: Jekyll::Hooks::PRIORITY_MAP[:low] do puts "Low #{n}" end Jekyll::Hooks.register :site, :after_reset, priority: Jekyll::Hooks::PRIORITY_MAP[:normal] do puts "Normal #{n}" end Jekyll::Hooks.register :site, :after_reset, priority: Jekyll::Hooks::PRIORITY_MAP[:high] do puts "High #{n}" end end Sorting by the negative of the priority and then by the order the hook was added does the right thing. --- features/hooks.feature | 6 +++--- lib/jekyll/hooks.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/features/hooks.feature b/features/hooks.feature index 075f632a..163f325f 100644 --- a/features/hooks.feature +++ b/features/hooks.feature @@ -235,7 +235,7 @@ Feature: Hooks owner.output = "1 #{owner.output.chomp}" end Jekyll::Hooks.register :pages, :post_render, priority: :high do |owner| - # high runs last + # high runs first owner.output = "2 #{owner.output.chomp}" end Jekyll::Hooks.register :pages, :post_render do |owner| @@ -243,13 +243,13 @@ Feature: Hooks owner.output = "3 #{owner.output.chomp}" end Jekyll::Hooks.register :pages, :post_render, priority: :low do |owner| - # low runs first + # low runs last owner.output = "4 #{owner.output.chomp}" end """ And I have a "index.html" page that contains "WRAP ME" When I run jekyll build - Then I should see "2 3 1 4 WRAP ME" in "_site/index.html" + Then I should see "4 3 1 2 WRAP ME" in "_site/index.html" Scenario: Alter a document right after it is initialized Given I have a _plugins directory diff --git a/lib/jekyll/hooks.rb b/lib/jekyll/hooks.rb index c9a4f794..cbf65ef6 100644 --- a/lib/jekyll/hooks.rb +++ b/lib/jekyll/hooks.rb @@ -80,7 +80,7 @@ module Jekyll end def self.insert_hook(owner, event, priority, &block) - @hook_priority[block] = "#{priority}.#{@hook_priority.size}".to_f + @hook_priority[block] = [-priority, @hook_priority.size] @registry[owner][event] << block end