51

I often link to GitHub source code via the #L param in the URI.

e.g. : https://github.com/github/learn.github.com/blob/gh-pages/episodes.yaml#L1

But is there a way to link to lines within a Markdown file?

e.g. https://github.com/github/learn.github.com/blob/gh-pages/README.md#L1 (does not work!)

I know I can link to 'sections', but lines are much better!

e.g. https://github.com/github/learn.github.com/blob/gh-pages/README.md#learngithubcom

Ian Vaughan
  • 20,211
  • 13
  • 59
  • 79
  • It's safe to link to this related issue now that it's been locked: https://github.com/holman/feedback/issues/305 – Dennis Jan 21 '15 at 15:05
  • Side note: [don't link that line number](http://andrew.yurisich.com/work/2014/07/16/dont-link-that-line-number/) – Tobias Kienzler Oct 12 '15 at 18:06

7 Answers7

36

As it is currently not possible, I take the next best option: use blame and then highlight the lines, e.g. https://github.com/rails/rails/blame/master/guides/source/configuring.md#L166

lulalala
  • 17,572
  • 15
  • 110
  • 169
18

Now Supported with ?plain=1

Announced on June 30, 2021, there is now a parameter to disable markdown rendering:

Appending ?plain=1 to the url for any Markdown file will now display the file without rendering. As with other code files, it will also show line numbers, and can be used to link other users to a specific line or lines. For example, appending ?plain=1#L52 will highlight line 52 of a plain text Markdown file.

It sounded like you might want to use line numbers to link to a point in the rendered document. That is still not possible, nor is it a standard feature of markdown renders to add line number anchors like that.

Daniel Himmelstein
  • 1,759
  • 1
  • 21
  • 26
7

For the benefit of the reader: The long answer is yes, it is difficult, but possible.

GitHub allows to inline permalinks to the text portion of Markdown files

However there seems currently to be no way to create such permalinks directly, you must manually create them. As follows:

  • Display the markdown document
  • Above the document on the right click on the shortened SHA of the document
    (you can use "History" and the commit in question as well)
  • The commit's diff is shown.
  • Above the commit on the right click on the the 3 dots ... and select "View file"
  • Now the Mardown is presented again however it is the permanent variant.
  • Above the document on the right click on "Blame"
  • Scroll down to the lines which contain the Markdown representation of the text to cite
  • Select the line or lines in question
  • Copy the URL from the address bar of the browser
  • Paste the URL into the issue etc.
  • This step is only needed within issues: In the URL replace /blame/ by /blob/

In the preview of the Issue you will see the expected direct citing of the text portion of the Markdown.

Drawback of the /blob/-variant

If you click on the URL, you see the full Markdown in the browser, the text portion is not marked as expected.

This is a limitation on how GitHub handles those URLs.

Drawback of the /blame/-variant

It is plain unreadable because of all those annotations and non-rendering of the Markdown.

Example

Note that I had to clone this example to be able to create an issue:

https://github.com/hilbix/learn.github.com/blob/gh-pages/README.md

  • Click on the SHA

https://github.com/hilbix/learn.github.com/commit/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba

  • "View File"

https://github.com/hilbix/learn.github.com/blob/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba/README.md

  • "Blame"

https://github.com/hilbix/learn.github.com/blame/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba/README.md

  • Mark some lines

https://github.com/hilbix/learn.github.com/blame/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba/README.md#L20-L23

  • Change the URL

https://github.com/github/learn.github.com/blob/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba/README.md#L20-L23

  • Paste this URL into some issue

Result: https://github.com/hilbix/learn.github.com/issues/1

This was entered into the issue:

https://github.com/hilbix/learn.github.com/blob/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba/README.md#L20-L23

see https://stackoverflow.com/a/57202063/490291

This renders to something like

learn.github.com/README.md

Lines 20 to 23 in 38034b3
    $ git clone https://github.com/github/learn.github.com
    $ cd learn.github.com
    $ script/bootstrap
    $ jekyll --server

see https://stackoverflow.com/a/57202063/490291

Here is the full HTML of the issue created (sorry, I did not manage to copy the CSS, too):

<td class="d-block comment-body markdown-body  js-comment-body">
          <p></p><div class="border rounded-1 my-2">
  <div class="f6 px-3 py-2 lh-condensed border-bottom bg-gray-light">
    <p class="mb-0 text-bold">
      <a href="https://github.com/hilbix/learn.github.com/blob/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba/README.md#L20-L23">learn.github.com/README.md</a>
    </p>
    <p class="mb-0 text-gray-light">
        Lines 20 to 23
      in
      <a data-pjax="true" class="commit-tease-sha" href="/hilbix/learn.github.com/commit/38034b3aee7f4fb7f46e94b3f9c2b3d3554291ba">38034b3</a>
    </p>
    </div>
    <div itemprop="text" class="blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8">

        <tbody><tr class="border-0">
          <td id="L20" class="blob-num border-0 px-3 py-0 bg-white js-line-number" data-line-number="20"></td>
          <td id="LC20" class="blob-code border-0 px-3 py-0 bg-white blob-code-inner js-file-line"> <span class="pl-s1">$ git clone https://github.com/github/learn.github.com</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L21" class="blob-num border-0 px-3 py-0 bg-white js-line-number" data-line-number="21"></td>
          <td id="LC21" class="blob-code border-0 px-3 py-0 bg-white blob-code-inner js-file-line"> <span class="pl-s1">$ <span class="pl-c1">cd</span> learn.github.com</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L22" class="blob-num border-0 px-3 py-0 bg-white js-line-number" data-line-number="22"></td>
          <td id="LC22" class="blob-code border-0 px-3 py-0 bg-white blob-code-inner js-file-line"> <span class="pl-s1">$ script/bootstrap</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L23" class="blob-num border-0 px-3 py-0 bg-white js-line-number" data-line-number="23"></td>
          <td id="LC23" class="blob-code border-0 px-3 py-0 bg-white blob-code-inner js-file-line"> <span class="pl-s1">$ jekyll --server</span> </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p>see <a rel="nofollow" href="https://stackoverflow.com/a/57202063/490291">https://stackoverflow.com/a/57202063/490291</a></p>
      </td>
Tino
  • 9,583
  • 5
  • 55
  • 60
3

I recently looked for the same. Answer is No, as mentioned in other answers. But I found a good alternative which gives almost the same result we want.

Adding text fragment to url.

For example: https://github.com/github/balanced-employee-ip-agreement#:~:text=FAQ

You just have to append #:~:text={text-that-you-want-to-be-focussed} at the end of the url

read more about text fragment here. https://wicg.github.io/scroll-to-text-fragment/

Ilam
  • 308
  • 3
  • 10
  • 1
    looks like the repo I used in example was archived. guess that has something to do with it. now i have updated with new example, i confirm it works. – Ilam Dec 15 '20 at 08:18
  • 1
    While it works for many use cases where the text is relevant, it's semantically not the same as linking to a line of code where the code is relevant. – DenverCoder9 Dec 16 '20 at 16:04
2

Short answer: no. Markdown is rendered into an HTML document by GitHub, so it's not currently possible to see it in a raw form that also allows you to link to individual lines. Maybe GitHub will implement this kind of functionality sometime in the future, but for now, it's not possible.

2

There's now support for this in the UI

This just dropped: https://twitter.com/github/status/1443572280924147717

  1. Click on README.md in the directory you're looking at
  2. Its URL will look like this: https://github.com/rust-lang/rust/blob/master/README.md
  3. Click the "Display the source blob" button: enter image description here
  4. The URL will change to https://github.com/rust-lang/rust/blob/master/README.md?plain=1, as described in this answer
  5. You can now click on line numbers and obtain links to these lines

There's still no such button to do this for .rst files, but adding ?plain=1 to the URL works: https://github.com/python/cpython/blob/main/README.rst?plain=1

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

When you want to link to a line in a file controlled by you, you can use <a>: https://stackoverflow.com/a/6494918/5053865

Example:

<a name="your_link_name">
Some line which you want to link to
</a>

... some wall of text ...

and here you are able to link to [the line](#your_link_name)

Example on GitHub: https://github.com/evis/markdown-link-to-line (it's in readme file).

This way, you can't refer to a line with a given number, but can refer to a line with needed content (which is exactly what you often need).

Evgeny Veretennikov
  • 3,889
  • 2
  • 16
  • 36