Refactor `highlight` tag to behave like the `raw` tag (#6821)
Merge pull request 6821
This commit is contained in:
parent
d807deb647
commit
36404b9a43
|
@ -31,3 +31,47 @@ Feature: Syntax Highlighting
|
||||||
When I run jekyll build
|
When I run jekyll build
|
||||||
Then I should get a zero exit-status
|
Then I should get a zero exit-status
|
||||||
And I should see "<span class="nc">RewriteCond</span>" in "_site/index.html"
|
And I should see "<span class="nc">RewriteCond</span>" in "_site/index.html"
|
||||||
|
|
||||||
|
Scenario: highlighting an Liquid example
|
||||||
|
Given I have an "inspect.md" file with content:
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
title: Inspect Filter
|
||||||
|
---
|
||||||
|
You can inspect a page's attributes with the `inspect` filter. You may enclose the
|
||||||
|
entire introspection within `<pre></pre>` tags to preserve the formatting:
|
||||||
|
{% highlight html %}
|
||||||
|
<pre id="inspect-filter">
|
||||||
|
{{ page | inspect }}
|
||||||
|
</pre>
|
||||||
|
{% endhighlight %}
|
||||||
|
"""
|
||||||
|
When I run jekyll build
|
||||||
|
Then I should get a zero exit-status
|
||||||
|
And I should see "<code class=\"language-html\" data-lang=\"html\">" in "_site/inspect.html"
|
||||||
|
And I should see "{{ page | inspect }}" in "_site/inspect.html"
|
||||||
|
|
||||||
|
Scenario: highlighting an included snippet
|
||||||
|
Given I have an _includes directory
|
||||||
|
And I have an "_includes/inspector.html" file with content:
|
||||||
|
"""
|
||||||
|
<pre id="inspect-filter">
|
||||||
|
{{ page | inspect }}
|
||||||
|
</pre>
|
||||||
|
"""
|
||||||
|
And I have an "inspect.md" file with content:
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
title: Inspect Filter
|
||||||
|
---
|
||||||
|
You can inspect a page's attributes with the `inspect` filter. You may enclose the
|
||||||
|
entire introspection within `<pre></pre>` tags to preserve the formatting:
|
||||||
|
{% highlight html %}
|
||||||
|
{% include inspector.html %}
|
||||||
|
{% endhighlight %}
|
||||||
|
"""
|
||||||
|
When I run jekyll build
|
||||||
|
Then I should get a zero exit-status
|
||||||
|
And I should see "<code class=\"language-html\" data-lang=\"html\">" in "_site/inspect.html"
|
||||||
|
But I should not see "{{ page | inspect }}" in "_site/inspect.html"
|
||||||
|
But I should see "{% include inspector.html %}" in "_site/inspect.html"
|
||||||
|
|
|
@ -52,6 +52,13 @@ Gem::Specification.new do |s|
|
||||||
* Our `link` tag now comes with the `relative_url` filter incorporated into it.
|
* Our `link` tag now comes with the `relative_url` filter incorporated into it.
|
||||||
You should no longer prepend `{{ site.baseurl }}` to `{% link foo.md %}`
|
You should no longer prepend `{{ site.baseurl }}` to `{% link foo.md %}`
|
||||||
For further details: https://github.com/jekyll/jekyll/pull/6727
|
For further details: https://github.com/jekyll/jekyll/pull/6727
|
||||||
|
|
||||||
|
* Our `highlight` tag no longer parses Liquid and Liquid-like constructs in the
|
||||||
|
tag's content body. While this means you no longer need to enclose the content
|
||||||
|
within a `{% raw %}{% endraw %}` block, it also means that you can no longer
|
||||||
|
do the following as well:
|
||||||
|
`{% highlight html %}{% include snippet.html %}{% endhighlight %}`
|
||||||
|
For further details: https://github.com/jekyll/jekyll/pull/6821
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
MSG
|
MSG
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
module Tags
|
module Tags
|
||||||
class HighlightBlock < Liquid::Block
|
class HighlightBlock < Liquid::Raw
|
||||||
include Liquid::StandardFilters
|
include Liquid::StandardFilters
|
||||||
|
|
||||||
# The regular expression syntax checker. Start with the language specifier.
|
# The regular expression syntax checker. Start with the language specifier.
|
||||||
|
@ -28,10 +28,12 @@ module Jekyll
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
LEADING_OR_TRAILING_LINE_TERMINATORS = %r!\A(\n|\r)+|(\n|\r)+\z!.freeze
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
prefix = context["highlighter_prefix"] || ""
|
prefix = context["highlighter_prefix"] || ""
|
||||||
suffix = context["highlighter_suffix"] || ""
|
suffix = context["highlighter_suffix"] || ""
|
||||||
code = super.to_s.gsub(%r!\A(\n|\r)+|(\n|\r)+\z!, "")
|
code = @body.gsub(LEADING_OR_TRAILING_LINE_TERMINATORS, "")
|
||||||
|
|
||||||
output =
|
output =
|
||||||
case context.registers[:site].highlighter
|
case context.registers[:site].highlighter
|
||||||
|
@ -101,6 +103,8 @@ module Jekyll
|
||||||
"<figure class=\"highlight\"><pre><code #{code_attributes}>"\
|
"<figure class=\"highlight\"><pre><code #{code_attributes}>"\
|
||||||
"#{code.chomp}</code></pre></figure>"
|
"#{code.chomp}</code></pre></figure>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ensure_valid_markup(tag_name, markup, parse_context); end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -69,6 +69,30 @@ class TestTags < JekyllUnitTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "highlight tag" do
|
||||||
|
setup do
|
||||||
|
create_post <<~CONTENT
|
||||||
|
---
|
||||||
|
title: This is a test
|
||||||
|
---
|
||||||
|
|
||||||
|
This is not yet highlighted
|
||||||
|
|
||||||
|
{% highlight html %}
|
||||||
|
<h1>{% unregistered_tag %}</h1>
|
||||||
|
{{ page | inspect }}
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
This should not be highlighted, right?
|
||||||
|
CONTENT
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not parse Liquid constructs in the tag markup" do
|
||||||
|
assert_match "{% unregistered_tag %}", @result
|
||||||
|
assert_match "{{ page | inspect }}", @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "highlight tag in unsafe mode" do
|
context "highlight tag in unsafe mode" do
|
||||||
should "set the no options with just a language name" do
|
should "set the no options with just a language name" do
|
||||||
tag = highlight_block_with_opts("ruby ")
|
tag = highlight_block_with_opts("ruby ")
|
||||||
|
|
Loading…
Reference in New Issue