Adding in the ability to link to posts internally. Syntax: {% post_url 2010-07-21-name-of-post %}; useful for: [Some Link]({% post_url 2010-07-21-name-of-post %})
This commit is contained in:
parent
72b7b1f056
commit
5cffe5ecb5
|
@ -92,3 +92,15 @@ Feature: Create sites
|
|||
When I debug jekyll
|
||||
Then the _site directory should exist
|
||||
And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html"
|
||||
|
||||
Scenario: Basic site with internal post linking
|
||||
Given I have an "index.html" page that contains "URL: {% post_url 2020-01-31-entry2 %}"
|
||||
And I have a configuration file with "permalink" set to "pretty"
|
||||
And I have a _posts directory
|
||||
And I have the following posts:
|
||||
| title | date | layout | content |
|
||||
| entry1 | 12/31/2007 | post | content for entry1. |
|
||||
| entry2 | 01/31/2020 | post | content for entry2. |
|
||||
When I run jekyll
|
||||
Then the _site directory should exist
|
||||
And I should see "URL: /2020/01/31/entry2/" in "_site/index.html"
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
module Jekyll
|
||||
|
||||
class PostComparer
|
||||
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
|
||||
|
||||
attr_accessor :date, :slug
|
||||
|
||||
def initialize(name)
|
||||
who, cares, date, slug = *name.match(MATCHER)
|
||||
@slug = slug
|
||||
@date = Time.parse(date)
|
||||
end
|
||||
end
|
||||
|
||||
class PostUrl < Liquid::Tag
|
||||
def initialize(tag_name, post, tokens)
|
||||
super
|
||||
@orig_post = post.strip
|
||||
@post = PostComparer.new(@orig_post)
|
||||
end
|
||||
|
||||
def render(context)
|
||||
site = context.registers[:site]
|
||||
|
||||
site.posts.each do |p|
|
||||
if p == @post
|
||||
return p.url
|
||||
end
|
||||
end
|
||||
|
||||
puts "ERROR: post_url: \"#{@orig_post}\" could not be found"
|
||||
|
||||
return "#"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_tag('post_url', Jekyll::PostUrl)
|
|
@ -9,6 +9,11 @@ class TestTags < Test::Unit::TestCase
|
|||
Jekyll::DEFAULTS.merge({'pygments' => true}).merge(override)
|
||||
end
|
||||
site = Site.new(Jekyll.configuration)
|
||||
|
||||
if override['read_posts']
|
||||
site.read_posts('')
|
||||
end
|
||||
|
||||
info = { :filters => [Jekyll::Filters], :registers => { :site => site } }
|
||||
@converter = site.converters.find { |c| c.class == converter_class }
|
||||
payload = { "pygments_prefix" => @converter.pygments_prefix,
|
||||
|
@ -137,4 +142,25 @@ CONTENT
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "simple page with post linking" do
|
||||
setup do
|
||||
content = <<CONTENT
|
||||
---
|
||||
title: Post linking
|
||||
---
|
||||
|
||||
{% post_url 2008-11-21-complex %}
|
||||
CONTENT
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
|
||||
should "not cause an error" do
|
||||
assert_no_match /markdown\-html\-error/, @result
|
||||
end
|
||||
|
||||
should "have the url to the \"complex\" post from 2008-11-21" do
|
||||
assert_match %r{/2008/11/21/complex/}, @result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue