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.
This commit is contained in:
Stephen Checkoway 2016-07-28 14:07:03 -05:00
parent bc7aaf5274
commit 6167c09569
2 changed files with 4 additions and 4 deletions

View File

@ -235,7 +235,7 @@ Feature: Hooks
owner.output = "1 #{owner.output.chomp}" owner.output = "1 #{owner.output.chomp}"
end end
Jekyll::Hooks.register :pages, :post_render, priority: :high do |owner| Jekyll::Hooks.register :pages, :post_render, priority: :high do |owner|
# high runs last # high runs first
owner.output = "2 #{owner.output.chomp}" owner.output = "2 #{owner.output.chomp}"
end end
Jekyll::Hooks.register :pages, :post_render do |owner| Jekyll::Hooks.register :pages, :post_render do |owner|
@ -243,13 +243,13 @@ Feature: Hooks
owner.output = "3 #{owner.output.chomp}" owner.output = "3 #{owner.output.chomp}"
end end
Jekyll::Hooks.register :pages, :post_render, priority: :low do |owner| Jekyll::Hooks.register :pages, :post_render, priority: :low do |owner|
# low runs first # low runs last
owner.output = "4 #{owner.output.chomp}" owner.output = "4 #{owner.output.chomp}"
end end
""" """
And I have a "index.html" page that contains "WRAP ME" And I have a "index.html" page that contains "WRAP ME"
When I run jekyll build 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 Scenario: Alter a document right after it is initialized
Given I have a _plugins directory Given I have a _plugins directory

View File

@ -80,7 +80,7 @@ module Jekyll
end end
def self.insert_hook(owner, event, priority, &block) 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 @registry[owner][event] << block
end end