diff --git a/docs/_docs/liquid/tags.md b/docs/_docs/liquid/tags.md index 3d487459..7140a030 100644 --- a/docs/_docs/liquid/tags.md +++ b/docs/_docs/liquid/tags.md @@ -188,3 +188,25 @@ You can also use this tag to create a link to a post in Markdown as follows: [Name of Link]({% post_url 2010-07-21-name-of-post %}) ``` {% endraw %} + +Now lets say you have a [datafile]({{ '/docs/datafiles/' | relative_url }}) `_data/cool_posts.yaml` used to keep track +of certain posts that you intend to be listed as say *Cool Posts*: + +```yaml +- title: "An Awesome Post" + slug: "2010-07-21-name-of-post" +- title: "Another Awesome Post" + slug: "2016-07-26-name-of-post" +``` + +You may list such posts using the `post_url` tag as well (from {%- include docs_version_badge.html version="4.5.0" -%}): + +{% raw %} +```liquid +Cool posts: + +{%- for cool_post in site.data.cool_posts %} +- [{{ cool_post.title }}]({% post_url {{ cool_post.slug }} %}) +{%- endfor %} +``` +{% endraw %} diff --git a/features/post_url_tag.feature b/features/post_url_tag.feature index ee59b71c..395e9d17 100644 --- a/features/post_url_tag.feature +++ b/features/post_url_tag.feature @@ -157,3 +157,37 @@ Feature: PostUrl Tag But the _site directory should exist And I should see "
" in "_site/index.html" And I should see "" in "_site/index.html" + + Scenario: Calling for a post via a liquid variable + Given I have a _posts directory + And I have the following post: + | title | date | content | + | Hello World | 2019-02-04 | Lorem ipsum dolor | + And I have an "index.md" page with content: + """ + {% assign value='2019-02-04-hello-world' %} + [Welcome]({% post_url {{ value }} %}) + """ + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "" in "_site/index.html" + + Scenario: Calling for posts via a liquid variable in a for tag + Given I have a _posts directory + And I have the following post: + | title | date | content | + | Hello World | 2019-02-04 | Lorem ipsum dolor | + | We Meet Again | 2019-02-05 | Alpha beta gamma | + And I have an "index.md" page with content: + """ + {% assign posts = '2019-02-04-hello-world;2019-02-05-we-meet-again' | split: ';' %} + {%- for slug in posts -%} + [{{ slug }}]({% post_url {{ slug }} %}) + {%- endfor %} + """ + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "2019-02-04-hello-world" in "_site/index.html" + And I should see "2019-02-05-we-meet-again" in "_site/index.html" diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index 8540bb5c..5b38327a 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -66,23 +66,28 @@ module Jekyll def initialize(tag_name, markup, tokens) super @markup = markup.strip - - begin - @post_comparer = PostComparer.new(@markup) - rescue StandardError - raise_markup_parse_error - end + @template = Liquid::Template.parse(@markup) if @markup.include?("{{") # Deprecated instance_variables. # To be removed in Jekyll v5.0. @orig_post = @markup - @post = @post_comparer + @post = nil end def render(context) @context = context + @resolved_markup = @template&.render(@context) || @markup site = context.registers[:site] + begin + @post_comparer = PostComparer.new(@resolved_markup) + rescue StandardError + raise_markup_parse_error + end + # For backwards compatibility only; deprecated instance_variable. + # To be removed in Jekyll v5.0. + @post = @post_comparer + # First pass-through. site.posts.docs.each do |post| return relative_url(post) if @post_comparer == post @@ -104,23 +109,23 @@ module Jekyll def raise_markup_parse_error raise Jekyll::Errors::PostURLError, <<~MSG - Could not parse name of post #{@markup.inspect} in tag 'post_url'. + Could not parse name of post #{@resolved_markup.inspect} in tag 'post_url'. Make sure the correct name is given to the tag. MSG end def raise_post_not_found_error raise Jekyll::Errors::PostURLError, <<~MSG - Could not find post #{@markup.inspect} in tag 'post_url'. + Could not find post #{@resolved_markup.inspect} in tag 'post_url'. Make sure the post exists and the correct name is given to the tag. MSG end def log_legacy_usage_deprecation Jekyll::Deprecator.deprecation_message( - "A call to '{% post_url #{@markup} %}' did not match a post using the new matching " \ - "method of checking name (path-date-slug) equality. Please make sure that you change " \ - "this tag to match the post's name exactly." + "A call to '{% post_url #{@resolved_markup} %}' did not match a post using the new " \ + "matching method of checking name (path-date-slug) equality. Please make sure that " \ + "you change this tag to match the post's name exactly." ) end end