23

My goal is to create links from any published jekyll page back to its location on Github.

And so, I'd need access to a page's pathname when constructing this url. However, I don't see anything in the api for templates that provides this info. I looked at the source for page, but none of the file/path attributes had values, when accessed via the template.

Dogweather
  • 15,512
  • 17
  • 62
  • 81
  • I gather you're looking for the path to the markdown source, rather than the relative URL source? E.g. for the page `http://mysite.com/2012/11/08/mypost.html` you would want something like `http://github.com/myname/mysite/tree/master/_posts/2012-11-08-mypost.md` ? – cboettig Nov 08 '12 at 20:14
  • Correct. Although I'm only only interested in pages not posts. I'm making a wiki-like frontend. See the button bar, http://railsdocs.org. E.g. From a page I want to link to its repo URL. If a page could get its file/path name, I could determine the URL on GitHub. – Dogweather Nov 08 '12 at 23:59

2 Answers2

40

Update: nowadays you can use {{page.path}}.


It seems like you can construct your link just fine using the liquid code: {{ page.url }}

For instance, if you want this page: http://railsdocs.org/pages/get-involved.html to link to this page: https://github.com/dogweather/railsdocs.org/blob/gh-pages/pages/get-involved.md

Seems like you could add something like:

[source](https://github.com/dogweather/railsdocs.org/blob/gh-pages/{{page.url | replace:'.html','.md'}})

to the markdown and get the link you want.

A more general solution:

Alternatively, we could write a generator that allows a page to access its file name directly. Add this to a .rb file in your _plugins directory:

module Jekyll

  class PagePathGenerator < Generator
    safe true
    ## See post.dir and post.base for directory information. 
    def generate(site)
      site.posts.each do |post|
        post.data['path'] = post.name
      end

    end
  end

end

and then we can reliably get the post's filename as {{ page.path }}. This solution is more robust than converting from the URL, since page names can have characters that get 'sanitized' out when converting them to URLs. (Posts would also need their date information, etc added back in). Instead, this plugin gives us direct access to a post's name.

A similar strategy could allow us to get the path to the post if that data is also needed.

mb21
  • 34,845
  • 8
  • 116
  • 142
cboettig
  • 12,377
  • 13
  • 70
  • 113
  • 3
    Almost ... problem is e.g., the .md vs. .html file extension. – Dogweather Nov 09 '12 at 02:28
  • good point, forgot about that. We can use a [liquid filter](https://github.com/Shopify/liquid/wiki/Liquid-for-Designers) to deal with this. A couple of options, I suggested one in my edit above, but could also use remove or something else. – cboettig Nov 09 '12 at 02:36
  • Yeah, that occurred to me, if I wanted to impose that contraint, that every html is an md file. Maybe... – Dogweather Nov 09 '12 at 02:47
  • It's not clear to me why you need to have a mix of `.md` and `.html` extensions for your pages. Most markdown parsers will just leave the html alone, so you could simply give them all `.md` extensions. Would more clearly indicate source file vs output file anyhow. – cboettig Nov 09 '12 at 03:23
  • Otherwise, we can write a [custom converter](https://github.com/mojombo/jekyll/wiki/Plugins) that runs on all pages, greps for the gh-pages link and changes `.md` to `.html` if the input file is `.html`. Converters are the only stage with access to the extension I think. That would seem like a rather more crude hack than the liquid filter and consistent extensions for pages though. – cboettig Nov 09 '12 at 03:27
  • Yes, I don't have any real need for non-md. I'm using Jekyll-Bootstrap, which comes packaged with samples pages which are html. But right, mine are .md. And, *serving* it from Github, you can't use custom plugins. – Dogweather Nov 09 '12 at 04:05
  • I believe custom plugins are fine serving from Github so long as they do not need any additional ruby gem dependencies, so I think you'd be all right there. I just think the custom converter solution feels very clunky, and it feels cleaner to give all pages .md extensions, even if they use a little html inside them for extra formatting. For things like your `categories.html` page, you could even write it in pure markdown+liquid, the html isn't doing much anyhow. – cboettig Nov 09 '12 at 17:58
  • This is a decent solution, but a page could have a different url specified from the one implied by its path (by setting a permalink in the yaml header). – heliotrope Nov 12 '12 at 18:37
  • @cboettig, nowadays Jekyll has been updated to v3.0+ and some syntax has been broken. Like `post.name` should be replaced as `post['name']`. Beyond this question, I guess your famous labnotebook is still on Jekyll ~2.+? I tried to update Jekyll to 3.0, but got some post path messed up when retrieving the git SHA histories. Have you tried to upgrade to Jekyll 3.0? Thanks. – Xiaodong Qi Jul 20 '16 at 23:57
  • Sorry, just did more tests, the `post['name']` command only extract the title part of the post--not including the year/month/day information for the file title in Jekyll 3.0+. How to get the post filename in Jekyll 3.0+? – Xiaodong Qi Jul 21 '16 at 03:19
20

I'm not sure when this was added, but page.path gives the source file's path relative to the Jekyll root directory.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
mikegreiling
  • 1,160
  • 12
  • 21
  • 2
    Yeah, `page.path` is now the best way to access the original, unprocessed file path. See [usage here](http://jekyllrb.com/docs/variables/) under "Page Variables". – Joel Glovier Jan 22 '14 at 17:54
  • Yup, it's great that `{{page.path}}` is now supported natively – cboettig Oct 06 '14 at 20:09