diff --git a/docs/_docs/plugins/hooks.md b/docs/_docs/plugins/hooks.md index 721a8c97..413ecdec 100644 --- a/docs/_docs/plugins/hooks.md +++ b/docs/_docs/plugins/hooks.md @@ -129,6 +129,17 @@ The complete list of available hooks is below:

Just before rendering a page

+ + +

:pages

+ + +

:post_convert

+ + +

After converting the page content, but before rendering the page layout

+ +

:pages

@@ -173,6 +184,17 @@ The complete list of available hooks is below:

Just before rendering a post

+ + +

:posts

+ + +

:post_convert

+ + +

After converting the post content, but before rendering the post layout

+ +

:posts

@@ -217,6 +239,17 @@ The complete list of available hooks is below:

Just before rendering a document

+ + +

:documents

+ + +

:post_convert

+ + +

After converting the document content, but before rendering the document layout

+ +

:documents

diff --git a/features/hooks.feature b/features/hooks.feature index 163f325f..f8c5f8fa 100644 --- a/features/hooks.feature +++ b/features/hooks.feature @@ -103,6 +103,27 @@ Feature: Hooks Then I should see "special" in "_site/page1.html" And I should not see "special" in "_site/page2.html" + Scenario: Modify the converted HTML content of a page before rendering layout + Given I have a _layouts directory + And I have a "_layouts/page.html" file with content: + """ +

Page heading

+ {{ content }} + """ + And I have a "page.md" page with layout "page" that contains "### Heading" + And I have a _plugins directory + And I have a "_plugins/ext.rb" file with content: + """ + Jekyll::Hooks.register :pages, :post_convert do |page| + page.content = page.content.gsub('h3', 'h4') + end + """ + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "

Page heading

" in "_site/page.html" + And I should see "

Heading

" in "_site/page.html" + Scenario: Modify page contents before writing to disk Given I have a _plugins directory And I have a "index.html" page that contains "WRAP ME" @@ -170,6 +191,34 @@ Feature: Hooks Then I should see "old post" in "_site/2015/03/14/entry1.html" And I should see "new post" in "_site/2015/03/15/entry2.html" + Scenario: Modify the converted HTML content of a post before rendering layout + Given I have a _layouts directory + And I have a "_layouts/post.html" file with content: + """ +

Page heading

+ {{ content }} + """ + And I have a _posts directory + And I have a "_posts/2016-01-01-example.md" file with content: + """ + --- + layout: post + --- + ### Heading + """ + And I have a _plugins directory + And I have a "_plugins/ext.rb" file with content: + """ + Jekyll::Hooks.register :posts, :post_convert do |post| + post.content = post.content.gsub('h3', 'h4') + end + """ + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "

Page heading

" in "_site/2016/01/01/example.html" + And I should see "

Heading

" in "_site/2016/01/01/example.html" + Scenario: Modify post contents before writing to disk Given I have a _plugins directory And I have a "_plugins/ext.rb" file with content: @@ -243,7 +292,7 @@ Feature: Hooks owner.output = "3 #{owner.output.chomp}" end Jekyll::Hooks.register :pages, :post_render, priority: :low do |owner| - # low runs last + # low runs last owner.output = "4 #{owner.output.chomp}" end """ @@ -278,6 +327,41 @@ Feature: Hooks And the _site directory should exist And I should see "all your base are belong to us" in "_site/index.html" + Scenario: Modify the converted HTML content of a document before rendering layout + Given I have a _layouts directory + And I have a "_layouts/meme.html" file with content: + """ +

Page heading

+ {{ content }} + """ + And I have a "_config.yml" file with content: + """ + collections: + memes: + output: true + """ + And I have a _memes directory + And I have a "_memes/doc1.md" file with content: + """ + --- + layout: meme + text: all your base + --- + ### {{ page.text }} + """ + And I have a _plugins directory + And I have a "_plugins/ext.rb" file with content: + """ + Jekyll::Hooks.register :documents, :post_convert do |document| + document.content = document.content.gsub('h3', 'h4') + end + """ + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "

Page heading

" in "_site/memes/doc1.html" + And I should see "

all your base

" in "_site/memes/doc1.html" + Scenario: Update a document after rendering it, but before writing it to disk Given I have a _plugins directory And I have a "_plugins/ext.rb" file with content: diff --git a/lib/jekyll/hooks.rb b/lib/jekyll/hooks.rb index 47cdae4c..e3ddca8b 100644 --- a/lib/jekyll/hooks.rb +++ b/lib/jekyll/hooks.rb @@ -22,22 +22,25 @@ module Jekyll :post_write => [], }, :pages => { - :post_init => [], - :pre_render => [], - :post_render => [], - :post_write => [], + :post_init => [], + :pre_render => [], + :post_convert => [], + :post_render => [], + :post_write => [], }, :posts => { - :post_init => [], - :pre_render => [], - :post_render => [], - :post_write => [], + :post_init => [], + :pre_render => [], + :post_convert => [], + :post_render => [], + :post_write => [], }, :documents => { - :post_init => [], - :pre_render => [], - :post_render => [], - :post_write => [], + :post_init => [], + :pre_render => [], + :post_convert => [], + :post_render => [], + :post_write => [], }, :clean => { :on_obsolete => [], diff --git a/lib/jekyll/renderer.rb b/lib/jekyll/renderer.rb index e7a7af87..d81e0d6e 100644 --- a/lib/jekyll/renderer.rb +++ b/lib/jekyll/renderer.rb @@ -66,7 +66,7 @@ module Jekyll # Render the document. # # Returns String rendered document output - # rubocop: disable Metrics/AbcSize + # rubocop: disable Metrics/AbcSize, Metrics/MethodLength def render_document info = { :registers => { :site => site, :page => payload["page"] }, @@ -84,6 +84,10 @@ module Jekyll output = convert(output.to_s) document.content = output + Jekyll.logger.debug "Post-Convert Hooks:", document.relative_path + document.trigger_hooks(:post_convert) + output = document.content + if document.place_in_layout? Jekyll.logger.debug "Rendering Layout:", document.relative_path output = place_in_layouts(output, payload, info) @@ -91,7 +95,7 @@ module Jekyll output end - # rubocop: enable Metrics/AbcSize + # rubocop: enable Metrics/AbcSize, Metrics/MethodLength # Convert the document using the converters which match this renderer's document. #