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:
parent
badc9c2d30
commit
eac6eb25b8
|
@ -129,6 +129,17 @@ The complete list of available hooks is below:
|
||||||
<p>Just before rendering a page</p>
|
<p>Just before rendering a page</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<p><code>:pages</code></p>
|
<p><code>:pages</code></p>
|
||||||
|
@ -173,6 +184,17 @@ The complete list of available hooks is below:
|
||||||
<p>Just before rendering a post</p>
|
<p>Just before rendering a post</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<p><code>:posts</code></p>
|
<p><code>:posts</code></p>
|
||||||
|
@ -217,6 +239,17 @@ The complete list of available hooks is below:
|
||||||
<p>Just before rendering a document</p>
|
<p>Just before rendering a document</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<p><code>:documents</code></p>
|
<p><code>:documents</code></p>
|
||||||
|
|
|
@ -103,6 +103,27 @@ Feature: Hooks
|
||||||
Then I should see "special" in "_site/page1.html"
|
Then I should see "special" in "_site/page1.html"
|
||||||
And I should not see "special" in "_site/page2.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
|
Scenario: Modify page contents before writing to disk
|
||||||
Given I have a _plugins directory
|
Given I have a _plugins directory
|
||||||
And I have a "index.html" page that contains "WRAP ME"
|
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"
|
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"
|
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
|
Scenario: Modify post contents before writing to disk
|
||||||
Given I have a _plugins directory
|
Given I have a _plugins directory
|
||||||
And I have a "_plugins/ext.rb" file with content:
|
And I have a "_plugins/ext.rb" file with content:
|
||||||
|
@ -278,6 +327,41 @@ Feature: Hooks
|
||||||
And the _site directory should exist
|
And the _site directory should exist
|
||||||
And I should see "all your base are belong to us" in "_site/index.html"
|
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
|
Scenario: Update a document after rendering it, but before writing it to disk
|
||||||
Given I have a _plugins directory
|
Given I have a _plugins directory
|
||||||
And I have a "_plugins/ext.rb" file with content:
|
And I have a "_plugins/ext.rb" file with content:
|
||||||
|
|
|
@ -22,22 +22,25 @@ module Jekyll
|
||||||
:post_write => [],
|
:post_write => [],
|
||||||
},
|
},
|
||||||
:pages => {
|
:pages => {
|
||||||
:post_init => [],
|
:post_init => [],
|
||||||
:pre_render => [],
|
:pre_render => [],
|
||||||
:post_render => [],
|
:post_convert => [],
|
||||||
:post_write => [],
|
:post_render => [],
|
||||||
|
:post_write => [],
|
||||||
},
|
},
|
||||||
:posts => {
|
:posts => {
|
||||||
:post_init => [],
|
:post_init => [],
|
||||||
:pre_render => [],
|
:pre_render => [],
|
||||||
:post_render => [],
|
:post_convert => [],
|
||||||
:post_write => [],
|
:post_render => [],
|
||||||
|
:post_write => [],
|
||||||
},
|
},
|
||||||
:documents => {
|
:documents => {
|
||||||
:post_init => [],
|
:post_init => [],
|
||||||
:pre_render => [],
|
:pre_render => [],
|
||||||
:post_render => [],
|
:post_convert => [],
|
||||||
:post_write => [],
|
:post_render => [],
|
||||||
|
:post_write => [],
|
||||||
},
|
},
|
||||||
:clean => {
|
:clean => {
|
||||||
:on_obsolete => [],
|
:on_obsolete => [],
|
||||||
|
|
|
@ -66,7 +66,7 @@ module Jekyll
|
||||||
# Render the document.
|
# Render the document.
|
||||||
#
|
#
|
||||||
# Returns String rendered document output
|
# Returns String rendered document output
|
||||||
# rubocop: disable Metrics/AbcSize
|
# rubocop: disable Metrics/AbcSize, Metrics/MethodLength
|
||||||
def render_document
|
def render_document
|
||||||
info = {
|
info = {
|
||||||
:registers => { :site => site, :page => payload["page"] },
|
:registers => { :site => site, :page => payload["page"] },
|
||||||
|
@ -84,6 +84,10 @@ module Jekyll
|
||||||
output = convert(output.to_s)
|
output = convert(output.to_s)
|
||||||
document.content = output
|
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?
|
if document.place_in_layout?
|
||||||
Jekyll.logger.debug "Rendering Layout:", document.relative_path
|
Jekyll.logger.debug "Rendering Layout:", document.relative_path
|
||||||
output = place_in_layouts(output, payload, info)
|
output = place_in_layouts(output, payload, info)
|
||||||
|
@ -91,7 +95,7 @@ module Jekyll
|
||||||
|
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
# rubocop: enable Metrics/AbcSize
|
# rubocop: enable Metrics/AbcSize, Metrics/MethodLength
|
||||||
|
|
||||||
# Convert the document using the converters which match this renderer's document.
|
# Convert the document using the converters which match this renderer's document.
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in New Issue