Liquefied link tag (#6269)

Merge pull request 6269
This commit is contained in:
Alexey Pelykh 2018-05-03 02:58:12 +03:00 committed by jekyllbot
parent c0ae27f068
commit b95151c4a7
3 changed files with 111 additions and 3 deletions

View File

@ -588,6 +588,23 @@ One major benefit of using the `link` or `post_url` tag is link validation. If t
Note you cannot add filters to `link` tags. For example, you cannot append a string using Liquid filters, such as `{% raw %}{% link mypage.html | append: "#section1" %} {% endraw %}`. To link to sections on a page, you will need to use regular HTML or Markdown linking techniques. Note you cannot add filters to `link` tags. For example, you cannot append a string using Liquid filters, such as `{% raw %}{% link mypage.html | append: "#section1" %} {% endraw %}`. To link to sections on a page, you will need to use regular HTML or Markdown linking techniques.
The name of the file you want to link can be specified as a variable instead of an actual file name. For example, suppose you defined a variable in your page's front matter like this:
```yaml
---
title: My page
my_variable: footer_company_a.html
---
```
You could then reference that variable in your link:
```liquid
{% raw %}{% link {{ page.my_variable }} %}{% endraw %}
```
In this example, the link would add link to the file `footer_company_a.html`.
### Linking to posts ### Linking to posts
If you want to include a link to a post on your site, the `post_url` tag will generate the correct permalink URL for the post you specify. If you want to include a link to a post on your site, the `post_url` tag will generate the correct permalink URL for the post you specify.

View File

@ -18,14 +18,17 @@ module Jekyll
def render(context) def render(context)
site = context.registers[:site] site = context.registers[:site]
liquid = site.liquid_renderer.file("(jekyll:link)")
relative_path = liquid.parse(@relative_path).render(context)
site.each_site_file do |item| site.each_site_file do |item|
return item.url if item.relative_path == @relative_path return item.url if item.relative_path == relative_path
# This takes care of the case for static files that have a leading / # This takes care of the case for static files that have a leading /
return item.url if item.relative_path == "/#{@relative_path}" return item.url if item.relative_path == "/#{relative_path}"
end end
raise ArgumentError, <<-MSG raise ArgumentError, <<-MSG
Could not find document '#{@relative_path}' in tag '#{self.class.tag_name}'. Could not find document '#{relative_path}' in tag '#{self.class.tag_name}'.
Make sure the document exists and the path is correct. Make sure the document exists and the path is correct.
MSG MSG

View File

@ -789,6 +789,45 @@ CONTENT
end end
end end
context "simple page with dynamic linking to a page" do
setup do
content = <<CONTENT
---
title: linking
---
{% assign contacts_filename = 'contacts' %}
{% assign contacts_ext = 'html' %}
{% link {{contacts_filename}}.{{contacts_ext}} %}
{% assign info_path = 'info.md' %}
{% link {{\ info_path\ }} %}
{% assign screen_css_path = '/css' %}
{% link {{ screen_css_path }}/screen.css %}
CONTENT
create_post(content, {
"source" => source_dir,
"destination" => dest_dir,
"read_all" => true,
})
end
should "not cause an error" do
refute_match(%r!markdown\-html\-error!, @result)
end
should "have the URL to the 'contacts' item" do
assert_match(%r!/contacts\.html!, @result)
end
should "have the URL to the 'info' item" do
assert_match(%r!/info\.html!, @result)
end
should "have the URL to the 'screen.css' item" do
assert_match(%r!/css/screen\.css!, @result)
end
end
context "simple page with linking" do context "simple page with linking" do
setup do setup do
content = <<CONTENT content = <<CONTENT
@ -815,6 +854,33 @@ CONTENT
end end
end end
context "simple page with dynamic linking" do
setup do
content = <<CONTENT
---
title: linking
---
{% assign yaml_with_dots_path = '_methods/yaml_with_dots.md' %}
{% link {{yaml_with_dots_path}} %}
CONTENT
create_post(content, {
"source" => source_dir,
"destination" => dest_dir,
"collections" => { "methods" => { "output" => true } },
"read_collections" => true,
})
end
should "not cause an error" do
refute_match(%r!markdown\-html\-error!, @result)
end
should "have the URL to the 'yaml_with_dots' item" do
assert_match(%r!/methods/yaml_with_dots\.html!, @result)
end
end
context "simple page with nested linking" do context "simple page with nested linking" do
setup do setup do
content = <<CONTENT content = <<CONTENT
@ -867,6 +933,28 @@ CONTENT
end end
end end
context "simple page with invalid dynamic linking" do
should "cause an error" do
content = <<CONTENT
---
title: Invalid linking
---
{% assign non_existent_path = 'non-existent-collection-item' %}
{% link {{\ non_existent_path\ }} %}
CONTENT
assert_raises ArgumentError do
create_post(content, {
"source" => source_dir,
"destination" => dest_dir,
"collections" => { "methods" => { "output" => true } },
"read_collections" => true,
})
end
end
end
context "include tag with parameters" do context "include tag with parameters" do
context "with symlink'd include" do context "with symlink'd include" do
should "not allow symlink includes" do should "not allow symlink includes" do