Add post_convert hook to modify HTML content before layout (#8368)

Co-authored-by: John Hughes <johnkhughes@users.noreply.github.com>
This commit is contained in:
Liam Cooke 2020-09-04 17:01:24 +10:00 committed by GitHub
parent badc9c2d30
commit eac6eb25b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 139 additions and 15 deletions

View File

@ -129,6 +129,17 @@ The complete list of available hooks is below:
<p>Just before rendering a page</p>
</td>
</tr>
<tr>
<td>
<p><code>:pages</code></p>
</td>
<td>
<p><code>:post_convert</code></p>
</td>
<td>
<p>After converting the page content, but before rendering the page layout</p>
</td>
</tr>
<tr>
<td>
<p><code>:pages</code></p>
@ -173,6 +184,17 @@ The complete list of available hooks is below:
<p>Just before rendering a post</p>
</td>
</tr>
<tr>
<td>
<p><code>:posts</code></p>
</td>
<td>
<p><code>:post_convert</code></p>
</td>
<td>
<p>After converting the post content, but before rendering the post layout</p>
</td>
</tr>
<tr>
<td>
<p><code>:posts</code></p>
@ -217,6 +239,17 @@ The complete list of available hooks is below:
<p>Just before rendering a document</p>
</td>
</tr>
<tr>
<td>
<p><code>:documents</code></p>
</td>
<td>
<p><code>:post_convert</code></p>
</td>
<td>
<p>After converting the document content, but before rendering the document layout</p>
</td>
</tr>
<tr>
<td>
<p><code>:documents</code></p>

View File

@ -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:
"""
<h3>Page heading</h3>
{{ 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 "<h3>Page heading</h3>" in "_site/page.html"
And I should see "<h4 id=\"heading\">Heading</h4>" 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:
"""
<h3>Page heading</h3>
{{ 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 "<h3>Page heading</h3>" in "_site/2016/01/01/example.html"
And I should see "<h4 id=\"heading\">Heading</h4>" 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:
"""
<h3>Page heading</h3>
{{ 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 "<h3>Page heading</h3>" in "_site/memes/doc1.html"
And I should see "<h4 id=\"all-your-base\">all your base</h4>" 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:

View File

@ -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 => [],

View File

@ -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.
#