Allow passing :strict_variables and :strict_filters options to Liquid's renderer (#6726)

Merge pull request 6726
This commit is contained in:
ashmaroli 2018-03-14 16:20:18 +05:30 committed by jekyllbot
parent 86d86258a8
commit 51bdea1303
4 changed files with 67 additions and 3 deletions

View File

@ -685,6 +685,8 @@ defaults: []
liquid: liquid:
error_mode: warn error_mode: warn
strict_filters: false
strict_variables: false
# Markdown Processors # Markdown Processors
rdiscount: rdiscount:
@ -713,6 +715,14 @@ options are
- `warn` --- Output a warning on the console for each error. - `warn` --- Output a warning on the console for each error.
- `strict` --- Output an error message and stop the build. - `strict` --- Output an error message and stop the build.
You can also configure Liquid's renderer to catch non-assigned variables and
non-existing filters by setting `strict_variables` and / or `strict_filters`
to `true` respectively. {% include docs_version_badge.html version="3.8.0" %}
Do note that while `error_mode` configures Liquid's parser, the `strict_variables`
and `strict_filters` options configure Liquid's renderer and are consequently,
mutually exclusive.
## Markdown Options ## Markdown Options
The various Markdown renderers supported by Jekyll sometimes have extra options The various Markdown renderers supported by Jekyll sometimes have extra options

View File

@ -30,6 +30,50 @@ Feature: Rendering
Then I should get a non-zero exit-status Then I should get a non-zero exit-status
And I should see "Liquid Exception: Liquid error \(.+/_includes/invalid\.html line 1\): wrong number of arguments (\(given 1, expected 2\)|\(1 for 2\)) included in index\.html" in the build output And I should see "Liquid Exception: Liquid error \(.+/_includes/invalid\.html line 1\): wrong number of arguments (\(given 1, expected 2\)|\(1 for 2\)) included in index\.html" in the build output
Scenario: Rendering a default site containing a file with rogue Liquid constructs
Given I have a "index.html" page with title "Simple Test" that contains "{{ page.title | foobar }}\n\n{{ page.author }}"
When I run jekyll build
Then I should get a zero exit-status
And I should not see "Liquid Exception:" in the build output
Scenario: Rendering a custom site containing a file with a non-existent Liquid variable
Given I have a "index.html" file with content:
"""
---
title: Simple Test
---
{{ page.title }}
{{ page.author }}
"""
And I have a "_config.yml" file with content:
"""
liquid:
strict_variables: true
"""
When I run jekyll build
Then I should get a non-zero exit-status
And I should see "Liquid error \(line 3\): undefined variable author in index.html" in the build output
Scenario: Rendering a custom site containing a file with a non-existent Liquid filter
Given I have a "index.html" file with content:
"""
---
author: John Doe
---
{{ page.title }}
{{ page.author | foobar }}
"""
And I have a "_config.yml" file with content:
"""
liquid:
strict_filters: true
"""
When I run jekyll build
Then I should get a non-zero exit-status
And I should see "Liquid error \(line 3\): undefined filter foobar in index.html" in the build output
Scenario: Render Liquid and place in layout Scenario: Render Liquid and place in layout
Given I have a "index.html" page with layout "simple" that contains "Hi there, Jekyll {{ jekyll.environment }}!" Given I have a "index.html" page with layout "simple" that contains "Hi there, Jekyll {{ jekyll.environment }}!"
And I have a simple layout that contains "{{ content }}Ahoy, indeed!" And I have a simple layout that contains "{{ content }}Ahoy, indeed!"

View File

@ -62,6 +62,8 @@ module Jekyll
"liquid" => { "liquid" => {
"error_mode" => "warn", "error_mode" => "warn",
"strict_filters" => false,
"strict_variables" => false,
}, },
"rdiscount" => { "rdiscount" => {

View File

@ -69,7 +69,10 @@ module Jekyll
def render_document def render_document
info = { info = {
:registers => { :site => site, :page => payload["page"] }, :registers => { :site => site, :page => payload["page"] },
:strict_filters => liquid_options["strict_filters"],
:strict_variables => liquid_options["strict_variables"],
} }
output = document.content output = document.content
if document.render_with_liquid? if document.render_with_liquid?
Jekyll.logger.debug "Rendering Liquid:", document.relative_path Jekyll.logger.debug "Rendering Liquid:", document.relative_path
@ -265,5 +268,10 @@ module Jekyll
c.output_ext(document.extname) c.output_ext(document.extname)
end.compact end.compact
end end
private
def liquid_options
@liquid_options ||= site.config["liquid"]
end
end end
end end