klaus icon indicating copy to clipboard operation
klaus copied to clipboard

Anchor links in index READMEs are broken

Open cpressey opened this issue 5 years ago • 2 comments

I noticed this and thought it would be a simple fix, but it turns out to be somewhat involved, so I'm opening this issue for it.

The index view handles URLs like

http://git.catseye.tc/SixtyPical/

and it adds a base tag to them that looks like

<base href="/SixtyPical/blob/master/"/>

This is good, because it means a link in the README that looks like

<a href="doc/SixtyPical.md">SixtyPical specification</a>

actually leads the browser to http://git.catseye.tc/SixtyPical/blob/master/doc/SixtyPical.md, which klaus understands, and not http://git.catseye.tc/SixtyPical/doc/SixtyPical.md, which klaus does not understand.

However. Consider what happens when the README contains an anchor link like

<a href="#specification">Jump down to Specification section</a>

The intended behaviour is that this link leads to http://git.catseye.tc/SixtyPical/#specification, but with the base tag, this link leads to http://git.catseye.tc/SixtyPical/blob/master/#specification, which klaus does not understand.

Considering klaus ships with Markdown support that includes the toc extension, this is not an obscure corner case: toc generates anchor links like these.

At first I thought I could just remove the base tag, but then discovered that breaks the non-anchor links. So, I can think of two solutions:

  1. Have http://git.catseye.tc/SixtyPical/blob/master/ redirect to http://git.catseye.tc/SixtyPical/. This is simple, and solves the problem entirely (I think?), but it might be conceptually problematic in some way that I don't see right now.
  2. Rewrite the links when rendering a README in an index view. It is not too hard to write a custom Markdown extension that writes out links to #foo as links to ../../#foo. However, this solution really needs to apply to all supported formats to be called a solution, so the same thing would need to be done for ReST and whatever other formats may be supported in the future.

There might be other solutions too. Anyway, I welcome discussion before I start on trying to fix this.

cpressey avatar Aug 26 '18 12:08 cpressey

What do other sites, e.g. GitHub, do in this case?

jelmer avatar Aug 26 '18 13:08 jelmer

GitHub does not add a base tag. It rewrites internal links to be a full path that includes blob/master/, and leaves anchor links alone.

(So for example, the two a tags I wrote above would appear like this:

<a href="/catseye/SixtyPical/blob/master/doc/SixtyPical.md">SixtyPical specification</a>
<a href="#specification">Jump down to Specification section</a>

It appears to do this for both Markdown and reStructuredText (I had to test the latter here because I don't normally use it.)

I'll try to investigate GitLab or BitBucket the next time I find a few minutes.

cpressey avatar Aug 29 '18 12:08 cpressey