开发者

jekyll - add stuff to pages automatically, on github pages

开发者 https://www.devze.com 2023-01-01 14:41 出处:网络
Is there a way to obtain the url of a page on Jekyll? By pages I mean non-post textile files, like about.html and download.html on the following hierarchy:

Is there a way to obtain the url of a page on Jekyll? By pages I mean non-post textile files, like about.html and download.html on the following hierarchy:

root
 |
 +- _includes
 |
 +- _layouts
 |
 +- _posts
 |
 +- _config.yml
 |
 +- index.textile
 |
 +- about.textile
 |
 `- download.textile

I'd like to do something like this:

<h1><a href="{{ page.url }}">{{ page.title }}</a></h1>

This works with posts because they have their url included on the post's to_liquid method. Pages on the other hand don't seem to include anything like it - they do have the properties I need (or so it seems), but they don't export them on to_l开发者_如何学Pythoniquid; I'd like to redefine it so it does. Is there a way to do this?

I've found jekyll_extensions, but I don't know if it works on my target environment (github pages).

I'd like to avoid having to include a url parameter on all the yaml heads, if possible.

Thanks a lot!


I think the answer is no. Here's the Jekyll wiki on what's in the global page item:

For Posts, this is the union of the data in the YAML Front Matter and the computed data (such as URL and date). For regular pages, this is just the YAML Front Matter.

Even if you could reconfigure Jekyll's engine to do it for you locally, I'm not sure how you could make that work on Github's own Pages (since they will be using the vanilla Jekyll rather than yours).


It is now possible to monkeypatch jekyll classes via its new plugins system!

Just add this file (page.rb) on your _plugins directory:

module Jekyll
  class Page
    def to_liquid
      self.data.deep_merge({
        "url" => File.join(@dir, self.url),
        "content" => self.content,
        "dir" => self.dir,
        "name" => self.name,
        "ext" => self.ext,
        "basename" => self.basename
      })
    end
  end
end

I've just modified to_liquid here, but I could have also added more methods, etc.


It looks like page.rb to_liquid does now support the "url" data your need...

https://github.com/mojombo/jekyll/blob/master/lib/jekyll/page.rb

0

精彩评论

暂无评论...
验证码 换一张
取 消