Working with Ruby
Hi, I am Jan. This is my old Ruby blog. I still post about Ruby, but I now do it on idiosyncratic-ruby.com. You should also install Irbtools to improve your IRB.

Fixing a little anchor permalinks vs. pagination issue (using javascript and jekyll)

I am using the Jekyll blog generator for some projects (still not sure if I really like it..) and came across the following requirement the other day:

  • The blog should only have an index page, no (visible) sub-posts
  • This index page should use pagination
  • There should be the ability to link to a specific blog post

The problem was: I wanted to use anchor links (https://some.url/#post-identifier), but – because of the pagination – blog posts “change” their index page (e.g. in jekyll, they move from /#example to /page2/#example).

I decided to fix this using this small piece of JavaScript code. If the blog post is not found on the current page, it will redirect the user to the correct page. Nothing special, but why not share? ;)

See this permalink for an example in action (you will be redirected to page 2). The code might easily be adopted for other blogging engines.

Creative Commons License

Felix | December 05, 2011

You want to have permalinks, even if they just redirect to the correct page of your index. Or work for someone who understands that the fragment ID is not a canonical URL and certainly not a valid way to "permalink" to anything.

J-_-L | December 05, 2011

The "links" let you "perma"nently find the right blog posts. OK, maybe, these are not "permalinks" in its original sense, but a valid workaround.

Felix | December 05, 2011

It needs JS on the client and the fragment ID could get normalized away, so it's not a valid permanent representation of the blogpost, period :-) Also, I think you loose some "search engine karma" with stuff like that.