Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub relative link in Markdown file

Is there a way to create a URL anchor, <a>, link from within a Markdown file, to another file within the same repository and branch (aka a link relative to the current branch)?

For example, in the master branch I have a README.md file, which I would like do something like:

# My Project is really really cool. My Project has a subdir named myLib, see below.  ## myLib documentation see documentation [here](myLib/README.md) 

This would allow me to link from one .md to another within the same branch and not have to worry about which branch I'm in (avoid having to do an absolute URL that includes the github.com branch name).

Here is a working example of what I mean:

  1. GOTO http://github.com/rynop/testRel, link does not work.
  2. GOTO http://github.com/rynop/testRel/blob/master/README.md, link works.

This is expected because at this point the starting URL is in the branch. Now how do I get it to pick up the current branch in the README.md at the root of the repository?

Update: I opened an issue against GitHub for this feature request.

like image 313
rynop Avatar asked Oct 04 '11 20:10

rynop


People also ask

How do I add a link in Github markdown?

There are two ways to create links. Or leave it empty and use the link text itself. URLs and URLs in angle brackets will automatically get turned into links. http://www.example.com or http://www.example.com and sometimes example.com (but not on Github, for example).

How do you hyperlink in readme MD?

To link inline, type the text you want to link within brackets, "[x]", followed directly by the link URL parentheses, "(y)".

How do I link to a file in Github?

Linking to code On GitHub.com, navigate to the main page of the repository. Locate the code you'd like to link to: To link to code from a file, navigate to the file. To link to code from a pull request, navigate to the pull request and click Files changed.


2 Answers

For example, you have a repo like the following:

project/     text.md     subpro/        subtext.md        subsubpro/            subsubtext.md        subsubpro2/            subsubtext2.md 

The relative link to subtext.md in text.md might look like this:

[this subtext](subpro/subtext.md) 

The relative link to subsubtext.md in text.md might look like this:

[this subsubtext](subpro/subsubpro/subsubtext.md) 

The relative link to subtext.md in subsubtext.md might look like this:

[this subtext](../subtext.md) 

The relative link to subsubtext2.md in subsubtext.md might look like this:

[this subsubtext2](../subsubpro2/subsubtext2.md) 

The relative link to text.md in subsubtext.md might look like this:

[this text](../../text.md) 
like image 33
mathsyouth Avatar answered Nov 27 '22 04:11

mathsyouth


Update 30th, January 2013, 16 months later:

GitHub Blog Post Relative links in markup files:

Starting today, GitHub supports relative links in markup files.
Now you can link directly between different documentation files, whether you view the documentation on GitHub itself, or locally, using a different markup renderer.

You want examples of link definitions and how they work? Here's some Markdown for you.
Instead of an absolute link:

[a link](https://github.com/user/repo/blob/branch/other_file.md) 

…you can use a relative link:

[a relative link](other_file.md) [a relative link](path%20with%20spaces/other_file.md) 

and we'll make sure it gets linked to user/repo/blob/branch/other_file.md.

If you were using a workaround like [a workaround link](repo/blob/master/other_file.md), you'll have to update your documentation to use the new syntax.

This also means your documentation can now easily stand on its own, without always pointing to GitHub.

Marcono1234 adds in the comments

Also useful: When the link starts with a /, it is relative to the root of the repository (regardless of whether the markdown file is nested in subdirectories)


Update December 20th, 2011:

The GitHub markup issue 84 is currently closed by technoweenie, with the comment:

We tried adding a <base> tag for this, but it causes problems with other relative links on the site.


October 12th, 2011:

If you look at the raw source of the README.md of Markdown itself(!), relative paths don't seem to be supported.
You will find references like:

[r2h]: http://github.com/github/markup/tree/master/lib/github/commands/rest2html [r2hc]: http://github.com/github/markup/tree/master/lib/github/markups.rb#L13 

As noted in InvisibleWolf's answer, if the target link is a directory and it has space, then you need to use %20 for each space.

like image 91
VonC Avatar answered Nov 27 '22 05:11

VonC