42

#L我经常通过URI 中的参数链接到 GitHub 源代码。

例如:https ://github.com/github/learn.github.com/blob/gh-pages/episodes.yaml#L1

但是有没有办法链接到 Markdown 文件中的行?

例如https://github.com/github/learn.github.com/blob/gh-pages/README.md#L1 (不起作用!)

我知道我可以链接到“部分”,但线条要好得多!

例如https://github.com/github/learn.github.com/blob/gh-pages/README.md#learngithubcom

4

7 回答 7

32

由于目前不可能,我采取了下一个最佳选择:使用责备,然后突出显示这些行,例如https://github.com/rails/rails/blame/master/guides/source/configuring.md#L166

于 2014-09-17T06:30:17.923 回答
8

现在支持?plain=1

2021 年 6 月 30 日宣布,现在有一个参数可以禁用 markdown 渲染

附加?plain=1到任何 Markdown 文件的 url 现在将在不渲染的情况下显示文件。与其他代码文件一样,它也会显示行号,并可用于将其他用户链接到特定的一行或多行。例如,追加?plain=1#L52将突出显示纯文本 Markdown 文件的第 52 行。

听起来您可能想使用行号链接到渲染文档中的某个点。这仍然是可能的,也不是 Markdown 渲染的标准功能来添加这样的行号锚点。

于 2021-07-07T21:01:46.817 回答
7

为了读者的利益:长答案是肯定的,这很困难,但可能。

GitHub 允许将永久链接内联到 Markdown 文件的文本部分

但是,目前似乎无法直接创建此类永久链接,您必须手动创建它们。如下:

  • 显示降价文档
  • 在文档上方右键单击文档的缩短SHA
    (您也可以使用“历史记录”和有问题的提交)
  • 显示提交的差异。
  • 在提交上方右键单击 3 个点...并选择“查看文件”
  • 现在 Mardown 再次出现,但它是永久变体。
  • 在文档上方右键单击“Blame”
  • 向下滚动到包含要引用的文本的 Markdown 表示的行
  • 选择有问题的一行或多行
  • 从浏览器的地址栏中复制 URL
  • 将 URL 粘贴到问题等中。
  • 此步骤仅在问题中需要:在 URL 中替换/blame//blob/

在问题的预览中,您将看到预期的对 Markdown 文本部分的直接引用。

/blob/-变体的缺点

如果单击 URL,您会在浏览器中看到完整的 Markdown,文本部分未按预期标记。

这是 GitHub 如何处理这些 URL 的限制。

/blame/-变体的缺点

由于所有这些注释和 Markdown 的非渲染,它是完全不可读的。

例子

请注意,我必须克隆此示例才能创建问题:

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

  • 单击 SHA

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

  • “查看文件”

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

  • “责备”

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

  • 标记一些行

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

  • 更改网址

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

  • 将此 URL 粘贴到某个问题中

结果:https ://github.com/hilbix/learn.github.com/issues/1

这是进入问题的:

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

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

这呈现为类似

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

这是所创建问题的完整 HTML(抱歉,我也没有设法复制 CSS):

<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>

于 2019-07-25T12:33:05.297 回答
3

我最近也在寻找同样的东西。如其他答案所述,答案是否定的。但我找到了一个很好的替代方案,它给出了我们想要的几乎相同的结果。

将文本片段添加到 url。

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

您只需在网址末尾附加#:~:text={text-that-you-want-to-be-focussed}

在此处阅读有关文本片段的更多信息。 https://wigg.github.io/scroll-to-text-fragment/

于 2020-07-31T18:34:53.083 回答
2

简短的回答:没有。Markdown 由 GitHub 呈现为 HTML 文档,因此目前无法以原始形式查看它,它还允许您链接到各个行。也许 GitHub 将来某个时候会实现这种功能,但目前还不可能。

于 2014-04-21T19:08:39.890 回答
1

现在在 UI 中支持此功能

这刚刚下降:https ://twitter.com/github/status/1443572280924147717

  1. 在您正在查看README.md 的目录中单击
  2. 它的 URL 如下所示:https ://github.com/rust-lang/rust/blob/master/README.md
  3. 单击“显示源 blob”按钮: 在此处输入图像描述
  4. URL 将更改为https://github.com/rust-lang/rust/blob/master/README.md?plain=1,如本答案中所述
  5. 您现在可以单击行号并获取这些行的链接

仍然没有这样的按钮来为.rst文件执行此操作,但添加?plain=1到 URL 有效:https ://github.com/python/cpython/blob/main/README.rst?plain=1

于 2021-09-30T15:50:33.360 回答
0

当您想链接到您控制的文件中的一行时,您可以使用<a>https ://stackoverflow.com/a/6494918/5053865

例子:

<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)

GitHub 上的示例:https ://github.com/evis/markdown-link-to-line (在自述文件中)。

这样,您不能引用具有给定数字的行,但可以引用具有所需内容的行(这正是您经常需要的)。

于 2022-01-11T11:43:46.867 回答