Retrieving the filename from jekyll #633

Closed
bhardin opened this Issue Aug 30, 2012 · 13 comments

Projects

None yet

7 participants

@bhardin
Contributor
bhardin commented Aug 30, 2012

I have searched for a liquid tag that does this. I know their is page.url is there a filename equivalent?

@dogweather

+1

I'd like this as well: be able to access a page's filename from within the page itself. My goal: to link back to the page's github repo location.

@bhardin
Contributor
bhardin commented Nov 12, 2012

@dogweather You can hack it to get it working... This is what I did:

<a href="https://github.com/bhardin/spotthevuln/blob/gh-pages/_posts/{{ page.date | date: "%Y-%m-%d" }}-{{ page.title | remove: " -" | replace: " ", "-" | downcase }}.md"
@kizu
kizu commented Dec 25, 2012

+1 here, right now I need to use some hakety hacks to get the desired filename:

{% assign filename = page.url %}
{% for item in page.categories %}
    {% capture remove %}{{ item }}/{% endcapture %}
    {% capture filename %}{{ filename | remove:remove }}{% endcapture %}
{% endfor %}

{% capture filename %}{% unless filename == '/en/' or filename == '/index.html' %}_posts/{% endunless %}{% if page.categories contains 'en' %}en/{% endif %}{{ page.date | date: "%Y-%m-%d-" }}{% if filename == '/en/' %}index_{% endif %}{{ filename | remove:'/' | remove_first:'.html' }}{% unless filename == '/en/' or filename == '/index.html' %}.md{% else %}.html{% endunless %}{% endcapture %}

'cause I have /:categories/:title permalink I need to remove all the categories from the page.url at first, then there are a lot of things happen at the capture, 'cause I have multilingual blog and have both .md and .html pages here. And I think I'll tweak this code a bit more in the future, so having a way to retrieve the page's initial url would be really helpful.

@parkr
Member
parkr commented Dec 26, 2012

I can't think of any reason why you should be linking to the filename or need to extract any information that isn't already available to you. Can you provide me with a use case? I don't understand why you're doing that, @kizu.

@kizu
kizu commented Dec 26, 2012

@parkr The reason is to provide a link to the source file on each page, so it would be easy for anyone to go and fork the page to fix or add something. It's somewhat popular — I've seen people making it a few times, like here(see “Jekyll” and “Liquid” sections) or there

@bhardin
Contributor
bhardin commented Dec 27, 2012

This is the same use case I had. I use it on spotthevuln.com. Example:
http://spotthevuln.com/2011/08/boundaries-sql-injection/

@dhcole
Contributor
dhcole commented Mar 5, 2013

Pulled this out into a separate plugin: https://github.com/developmentseed/jekyll-filename

Our use case is that we want to link multilingual posts by file name (2013-05-12-post-title-en.md and 2013-05-12-post-title-es.md). This makes allows us to keep the title and url free to be translated, but still have a way to link posts. We can use the post_url filter to figure out translated post's url.

@guileen
guileen commented Mar 18, 2013

👍 for filename

@guileen
guileen commented Mar 18, 2013

I use this

{% assign filename = page.url | replace_first: '/', '' | replace: '/', '-'  | replace: '.html', '.md' %}
<a href="https://github.com/guileen/guileen.github.com/tree/master/_posts/{{filename}}">View on github</a>
<a href="https://github.com/guileen/guileen.github.com/commits/master/_posts/{{filename}}">History</a>
@dogweather

My use case: Making Jekyll into an un-wiki: each page has a link to its github edit location. For example: http://railsdocs.org/pages/tutorials.html

@guileen
guileen commented Mar 18, 2013

@dogweather How do you generate the Edit link of tutorial page? I didn't found the code in your repo

@dogweather

It looks good, doesn't it? :-) But it's an ugly hack:

In each page's header, I add a filename attribute with the file's path. E.g.

---
layout: page
title: "Tutorials"
verbose_title: "Wikified Ruby and Rails Tutorial Listing"
description: "Tutorials and guides for learning Ruby and Rails"
filename: "pages/tutorials.md"
---

https://raw.github.com/dogweather/railsdocs.org/gh-pages/pages/tutorials.md

Then, my html template pulls that out:

<a href="https://github.com/dogweather/railsdocs.org/edit/gh-pages/{{page.filename}}" class="btn">Edit</a>

https://github.com/dogweather/railsdocs.org/blob/gh-pages/_includes/themes/twitter/page.html#L13

@parkr parkr closed this in d64fd1e Apr 12, 2013
@bamos bamos referenced this issue in bamos/bamos.github.io Dec 6, 2014
@bamos bamos Link to every post's source. 7a65d36
@i2000s i2000s referenced this issue Jul 21, 2016
Closed

post['name'] and post.name changes #5121

8 of 17 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment