在GitHub上链接到一个单独行的Markdown文件

52

现在这个问题已经被锁定,链接到这个相关问题是安全的:https://github.com/holman/feedback/issues/305 - Dennis
附注:不要链接那一行代码 - Tobias Kienzler
7个回答

36

只有该行的内容不会在该链接下呈现。但您已经提供了问题所要求的内容。 - Liang Zhang
Not a permalink, though. - Nathaniel M. Beaver
3
快到了,看我的回答:不要使用master,而是使用SHA,如果需要,在问题中内联时将/blame/替换为/blob/ - Tino

18

现在支持?plain=1

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

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

听起来你可能想使用行号来链接到渲染文档中的某个位置。目前仍然不可能实现这一点,也不是Markdown渲染器的标准功能,无法添加类似的行号锚点。


7

为了读者的方便:长话短说,是的,这很困难,但是有可能。

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

然而目前似乎没有直接创建这种永久链接的方法,必须手动创建。如下:

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

在问题的预览中,您将看到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

  • 更改URL

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://dev59.com/mmYq5IYBdhLWcg3wjBQM#57202063

这将呈现为类似于的内容。
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://dev59.com/mmYq5IYBdhLWcg3wjBQM#57202063

这是创建问题时完整的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://dev59.com/mmYq5IYBdhLWcg3wjBQM#57202063">https://dev59.com/mmYq5IYBdhLWcg3wjBQM#57202063</a></p>
      </td>


3

1
看起来我在示例中使用的存储库已被归档。猜想这可能与此有关。现在我已经更新了新的示例,我确认它可以工作。 - Ilam
1
虽然在许多文本相关的用例中它可以工作,但从语义上讲,它与链接到代码行不同,而代码行才是相关的。 - DenverCoder9

2

简短回答:不可以。Markdown会被GitHub渲染成HTML文档,因此目前无法以原始形式查看并链接到单个行。也许在未来,GitHub会实现这种功能,但目前还不可能。


2

现在UI界面支持此功能

刚刚发布了这个消息:https://twitter.com/github/status/1443572280924147717

  1. 点击你正在查看的目录下的README.md
  2. 它的URL将类似于:https://github.com/rust-lang/rust/blob/master/README.md
  3. 点击“显示源代码”按钮: enter image description here
  4. URL将更改为https://github.com/rust-lang/rust/blob/master/README.md?plain=1,如此答案所述
  5. 现在您可以单击行号并获得到这些行的链接

对于.rst文件,仍然没有相应的按钮,但是在URL中添加?plain=1即可实现此功能:https://github.com/python/cpython/blob/main/README.rst?plain=1


0

当您想要链接到由您控制的文件中的某一行时,可以使用<a>标签:https://dev59.com/PnE85IYBdhLWcg3wViE4#6494918

示例:

<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(在 readme 文件中)。

这种方式,您无法引用给定编号的行,但可以引用所需内容的行(这通常是您需要的内容)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接