【问题标题】:Create a link to the header of another page in Github在 Github 中创建指向另一个页面标题的链接
【发布时间】:2016-12-23 14:23:39
【问题描述】:

有人知道如何创建指向不同 wiki 页面标题的链接吗?

我知道我是否有一个标题##Header name,我可以使用(#header-name) 作为我的链接在该页面上链接到它,但我想从不同的页面链接到该标题。这可能吗?

即。我想要一个可以链接到每个 wiki 页面的子部分以及页面本身的目录。

编辑:我的意思是除了使用 url 链接之外的一种方法
http://github.com/project/wiki/Wiki-Page#header-name

【问题讨论】:

    标签: github markdown


    【解决方案1】:

    编辑 1:之前完全错了,我只是多读了一点。所以我们在 GitHub Wiki 中也有这个新的支持! (相对较新。)

    你也可以这样做:

    [[ Link text | page_title#header_title ]]
    

    这可能对您更有效! TIL 因为这个答案here。您可以看到我使用 Prerequisite 链接执行此操作,并且您可以看到我的其他链接以其他方式工作。是时候让我做一些更新了!


    编辑 1:仍然有用,但绝对 不是唯一的方法

    所以我之前回答过一个关于这个的问题,你应该避免GitHub上的绝对链接(即https://github.com/user/repo_name/...

    然而,一个很好的方式(也是 Wiki 内部的唯一方式EDIT 1:完全不适合成为唯一的方式)可以这样看:

    [Header link](/user/repository_name/wiki/page_name#title).
    

    不幸的是,Wiki 会支持这种链接。这将更改基于 GitHub 的目录页面。你可以看到它会是

    https://github.com/(the linkage you want to hit)
    

    我实际上已经开始在我工作的 Wiki 中做这样的事情 here。在我的侧边栏内,您可以看到我有一个入门页面,然后其中的一个小节是一个先决条件标题,它将正确地引导人们到他们需要去的地方。您将能够在任何页面上执行相同的操作。这有点冗长,但值得,因为如果需要,您可以轻松地改变事情。这也是区分大小写的,因为它会更改它们的位置,因此请确保在您的链接中,页面是正确的大小写,并且您的标题都是小写的。

    希望这会有所帮助!

    【讨论】:

    • 只有 [Header link](/user/repository_name/wiki/page_name#title) 对我有用,但请注意,如果您不使用前面的斜杠,则该 url 是相对的。
    【解决方案2】:

    您可以通过简单地为 header 分配一个 id 来链接到 header。例如,您在名为 Abc 的页面中有“扩展”标题。 # <a id="extension"></a> Extensions
    你有另一个页面“呼叫中心”,你想去 abc 中的扩展,你可以使用 markdown 的参考链接,即“[扩展] [1]由代理处理” [1]: url-of-abc/#extension

    【讨论】:

      【解决方案3】:

      我在 Edit 1 上测试了 Maxwell 的 “好方法” 到 Github 中另一个页面的 link to the header,它运行良好。

      #[crux-ports Installation](/user/crux-ports/blob/master/README.md#installation)
      

      【讨论】:

      • 不,它不起作用,它只会尝试转到当前页面上的那个标题。
      • 不能在里面放一些 HTML 吗? <a href="example-wiki-url#anchor">Link Text</a> 我的理解是你可以把 HTML 放在 Markdown 文档的任何地方。
      【解决方案4】:

      markdown为heading生成slug并转化为id,例子

      # [ topic ][ color ]
      

      将转换为

      <h1 id="topic--color" data-line="643" class="code-line">[ topic ][ color ]</h1>
      

      因此,要链接它,您可以将其写为[color](#topic--color)

      如果目标锚点在另一个页面上(假设文件名css.md),其路径相对于当前降价页面,那么您可以将其写为[color](css.md#topic--color)

      从 vscode 附加 slugify 函数

      // excerpt from https://github.com/yzhang-gh/vscode-markdown/blob/908d7ba5465a203e4299f346c179211d992ef468/src/util/slugify.ts
      
      const str = '# [ topic ][ color ]';
      
      const slug = encodeURI(
                  str.trim()
                      .replace(/\s+/g, "-") // Replace whitespace with -
                      .replace(/[\]\[\!\'\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~\`。,、;:?!…—·ˉ¨‘’“”々~‖∶"'`|〃〔〕〈〉《》「」『』.〖〗【】()[]{}]/g, "") // Remove known punctuators
                      .replace(/^\-+/, "") // Remove leading -
                      .replace(/\-+$/, "") // Remove trailing -
              );
              
       console.log(slug) // "topic--color"

      【讨论】:

        猜你喜欢
        • 2015-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多