【问题标题】:Link to a line Markdown file on GitHub链接到 GitHub 上的一行 Markdown 文件
【发布时间】:2013-01-01 13:50:21
【问题描述】:

我经常通过 URI 中的 #L 参数链接到 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

【问题讨论】:

标签: github


【解决方案1】:

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

【讨论】:

  • 只有该行的内容不会在该链接下呈现。但是您已经提供了问题所要求的内容。
  • 虽然不是固定链接。
  • 差不多了,see my answer:不要使用 master,而是使用 SHA,并且 - 如果需要 - 将 /blame/ 替换为 /blob/ 以内联到问题中
【解决方案2】:

现在支持?plain=1

2021 年 6 月 30 日宣布,现在有一个parameter to disable markdown rendering

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

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

【讨论】:

    【解决方案3】:

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

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

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

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

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

    /blob/-variant 的缺点

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

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

    /blame/-variant 的缺点

    由于所有这些注释和 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>

    【讨论】:

      【解决方案4】:

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

      将文本片段添加到 url。

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

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

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

      【讨论】:

      • 看起来我在示例中使用的 repo 已存档。猜想这与它有关。现在我已经更新了新示例,我确认它有效。
      • 虽然它适用于文本相关的许多用例,但它在语义上与链接到代码相关的代码行不同。
      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        现在在 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,如this answer 中所述
        5. 您现在可以单击行号并获取这些行的链接

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

        【讨论】:

          【解决方案7】:

          当你想链接到你控制的文件中的一行时,你可以使用&lt;a&gt;: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(在自述文件中)。

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

          【讨论】:

            猜你喜欢
            • 2011-11-30
            • 1970-01-01
            • 2015-03-14
            • 1970-01-01
            • 1970-01-01
            • 2015-09-10
            • 2017-06-17
            • 1970-01-01
            • 2017-11-04
            相关资源
            最近更新 更多