Is there a way to get all the internal backlinks to a post in Jekyll?

I have a bunch of posts in which I provide links to other posts that I have written, what I want to be able to achieve is when I am at a page that has links to it in other pages I want to get all the links to that page and display the titles of the pages that have the links i.e., I want to show all the backlinks.

Example::
Let’s say there are 4 Posts, and

  1. Post A has a link to Post B
  2. Post A has a link to Post C
  3. Post C has a link to Post B
  4. Post B has a link to Post D

I want to get all the links to Post B, that is, from Post A and Post C when I am at the page containing Post B.

So If Post B looks like this, I want to append the backlinks to it as shown below:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Links to this Post:
1. Post A
2. Post C

Are there commands or plugins that achieve this? Any help is appreciated.

1 Like

Me too I’m looking for some way to do this!

@raghuveerdotnet have you found a workaround?

You could split the task.

Start with a script that iterates over your posts and outputs links to other posts. Write it in Bash or Python or Ruby etc. You can run it locally manually.

Then output a YAML file like, where the key is the post filename and the value is an array of any links to the post.

postA:
  - postB
  - postC
postB: nil
postC:
   - postD

That should be stored in _data folder in version control as maybe backlinks.yml.

Then the second part is to use that data.

In your post.html layout:

---
layout: default
---

<h2>{{ page.title }}</h2>

{{ content}}


<h3>Links to this post</h3>

{% assign post_links = site.data.backlinks[page.name] %}

<ul>
{% for p in post_links %}
  <li>
    {{ p }}
  </li>
{% endfor %}
</ul>

I used page.name as the key to lookup but you can find what works for you. It will be a value like '2020-01-04-a.md

Also the bullet output is very basic. I just made plain text. You’d could use site.posts or site.pages with the where filter to find the post q and then get q.title for text and p.url for the href URL

1 Like