How to refer to internal pages via ID instead of URLs?

Hi,

I often link to various internal pages including blog posts, etc.

I would like to be able to make a link to a page using some sort of unique page ID instead of the actual URL.
For example, lets say I have a page defining some concept foo. I would then like to be able to link with something like this [Foo](pages.foo) instead of [Foo](/en/products/category/foo.html)
In the latter case with the URL, I will have to go through all pages to update references in the event that I move the URL from /en/products/category/foo.html to, say, /en/products/foo.html.
I could probably make something work by giving each page (or maybe even just specific pages I link to often) a variable id in their preamble and then find some way to look this up using liquid… But I don’t want to reinvent the wheel (badly) if it has already been invented. I think a desire for this functionality would be fairly common.

there are built in link and post urls you can do that sound close to what you want, though if you move or rename a file I am not sure they will work. They are more about taking into consideration the permalink and baseurl as I understand it.

1 Like

I think the link tag (Tags Filters | Jekyll • Simple, blog-aware, static sites) is close to what I want. However not quite. Link seems to be able to translate the local path into an URL. That is all well. However, I want to use something short that is mentioned in the frontmatter as identifier.

So for example, with link I could use
{% link _posts/2016-07-26-name-of-post.md %} or {% link news/index.html %} to generate /2016/07/26/name-of-post.html or /news/ respectively.

What I need is abstracted one level above this. So I want to be able to go into 2016-07-26-name-of-post.md and news/index.html and put in front matter like: id: namepost and id: news and then from anywhere in jekyll be able to do something like:
{% url "namepost" %} or {% url "news" %} and have those generate /2016/07/26/name-of-post.html or /news/ respectively.

I hope this makes sense.

There are obvious use cases for this. One is if I have a term that is described on a specific page and I use this term a lot. Maybe I want to link the first occurrence of this term on each page to where that term is described.
Now, when I start out, maybe the description of this term is a blog entry. Maybe later I decide to make a dedicated (non-blog) page that describes this term. Without the functioanlity I describe above, I would have to go into all pages linking to this page and change all the links. This is solved by what I describe.

It might also be possible that I could hack something up using _data files. Maybe I could have a _data/urlalias.yml and then have a list in there that maps ids to URLs. Then I would just have to change the mapping one place if I rename or move a file. However, I am not clever enough to actually make this work…
I think the _data approach might be the way to go, because that way I could probably also use it for all other kinds of assets. For example if I have some icon that I use all the time, I could easily link to it using <img src="{% url successicon %}> instead of <img src="/assets/images/icons/32x32/success-32x32-optimized.png">.

If I then later want to change that icon into a .webp image or simply use another URL, I could just change this in the _data file.

page.url will get you the url to the page if you loop thru and find the page. So you could do front matter and have an id = 1234 and then do an if statement to find a page where the id = 1234 and then get the url of that page.

But that won’t scale well if you have a lot of pages and I think it would be difficult to manage.

Your idea of using a data file may be better, it shouldn’t be difficult to do though I am not sure it will perform any better - but at least everything would be in one place to edit.

this site has a lot of example of data stuff:

Not sure how this is done, but the polyglot plugin has something like this inside, so it can find which page is the equivalent to that same page in a different language.