1

I have a collection _colletion. In there is a file _collection/path/topic.md and a folder _collection/path/topic/ that includes lots of .md files with content. The permalinks for these files are /path/topic and /path/topic/file-x - so a parent page with a folder with the same name with multiple random pages in it.

Now I want to output a link to /path/topic in all these .md files with the title of topic.md as link text:

---
title: This is the page title defined in topic.md
---

should become <a href="/path/topic">This is the page title defined in topic.md</a>

How do I do that most easily?

Can I somehow access the folder name topic of the .md files and use this to read topic.md and output it's title and also generate a link to it?

janpio
  • 10,645
  • 16
  • 64
  • 107
  • 1
    Isn't 'topic' part of the page.url? – Mr. Hugo Sep 13 '17 at 16:36
  • Yes, `file-x.md` would get `/path/topic/file-x` as its `page.url`. I was hoping to somehow be able to go from this to `topic` and then to the `title` and URL of `topic.md` so I can link there. – janpio Sep 13 '17 at 17:01
  • 1
    It would have been easier if topic.md was called index.md... – Mr. Hugo Sep 13 '17 at 17:03
  • And placed in `/topic/` folder? I would be fine with that if its URL stays `/path/topic` (without trailing slash should work or at least be redirected). – janpio Sep 13 '17 at 17:47

4 Answers4

2

During discussion in the comments on my question and the other answers I noticed that what I wanted to build was actually a very common thing: A breadcrumb navigation! Just a very "small" one, with only one level.

With this newfound knowledge I could google "breadcrumb" plugins for Jekyll:

This solution uses the path of the page to extract the "crumbs":

It uses the folder name for the link text.

Another similar implementation:

Another one:

So no title link text in all of these.


This solution actually reads the page title, but can also read breadcrumb frontmatter from the pages, and uses these as link text:

This one might be a valid solution.


There are also real plugins (that unfortunately don't work with Github Pages):

janpio
  • 10,645
  • 16
  • 64
  • 107
1

My current manual "solution" (or workaround):

Add a parent entry to the frontmatter of all pages in /topic/ that contains the title and relative URL for the topic.md:

parent: ['Topic Title', '../topic']

In the template of the pages:

{% if page.parent %}
  <p>« <a href="{{ page.parent[1] }}">{{ page.parent[0] }}</a></p>
{% endif %}

Works, but of course duplicates this information n times and has to be maintained manually.

janpio
  • 10,645
  • 16
  • 64
  • 107
1

How about this (option 1)?

{% assign pageurl_array = page.url | split: "/" %}
{% assign path = pageurl_array[0] %}
{% assign topic = pageurl_array[1] %}
<p>« <a href="{{ path }}/{{ topic }}/{{ topic }}.html">
  {{ topic | capitalize | replace: "-", " " }}
</a></p>

If you do not mind crazy build times, do this (option 2):

{% assign pageurl_array = page.url | split: "/" %}
{% assign path = pageurl_array[0] %}
{% assign topic = pageurl_array[1] %}
{% capture parent_url %}{{ path }}/{{ topic }}/{{ topic }}.html{% endcapture %}
<p>« <a href="{{ parent_url }}">
  {% for i in site.pages %}
    {% if i.url == parent_url %}
      {{ i.title }}
    {% endif %}
  {% endfor %}
</a></p>

I would go for the first option (much faster) and use this javascript to get the capitals and special characters right:

$('a').each( function() {
  var str = $(this).html();
  str = str.replace('Topic from url', 'Topic from URL');
  $(this).html(str);
});

I admit that the javascript solution is far from pretty, but it solves the build time problem pretty well.

Note that Jekyll is pretty slow. I would advice you to dig into Hugo if you require faster build times.

Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60
  • Oh, very manual ;) Looks like the href could be adapted (`/{{ path }}/{{ topic }}` would be enough in my case) and replace part of my "workaround". But I wanted to get the actual `title` property from the `topic.md` automatically as well... (So link text should not be "Topic" but "This is the page title defined in topic,md") – janpio Sep 14 '17 at 08:42
  • 1
    You could loop over the site.pages and match the url to find this title... but that would (dramatically) increase build time. I would not do that as it seems not really necessary to me. – Mr. Hugo Sep 14 '17 at 09:05
  • Yeah, I was hoping there was a (performant) "give me the title for this file/page" functionality I didn't know about. – janpio Sep 14 '17 at 11:53
0

My solution, based on JoostS code:

{% assign  url = page.url | remove:'.html' | split: "/"  %}
{% assign path = url | pop %}
{% if path.size == 1 %}
    <a class="back" href="/home/">home</a>
{% else %}
    <a class="back" href="/{% for dir in path offset: 1 %}{{ dir | append: "/" }}{% endfor %}">{{ path | last }}</a>
{% endif %}```