Markdown 文件中的 GitHub 相对链接
有没有办法创建 URL 锚点 ,从 Markdown 文件内链接到同一存储库和分支内的另一个文件(也称为相对于当前分支的链接)?
例如,在主分支中,我有一个 README.md 文件,我希望执行以下操作:
# My Project
is really really cool. My Project has a subdir named myLib, see below.
## myLib documentation
see documentation [here](myLib/README.md)
这将允许我从一个 .md 链接到同一分支内的另一个 .md,而不必担心我是哪个分支in (避免必须执行包含 github.com 分支名称的绝对 URL)。
这是我的意思的一个有效示例:
- GOTO http://github.com/rynop/testRel,链接不起作用。
- 转到 http://github.com/rynop/testRel/blob/master/README.md ,链接有效。
这是预期的,因为此时起始 URL 位于分支中。现在我如何让它在存储库根目录的 README.md 中获取当前分支?
更新:我针对此功能请求针对 GitHub 提出了问题。
Is there a way to create a URL anchor, <a>
, link from within a Markdown file, to another file within the same repository and branch (aka a link relative to the current branch)?
For example, in the master branch I have a README.md file, which I would like do something like:
# My Project
is really really cool. My Project has a subdir named myLib, see below.
## myLib documentation
see documentation [here](myLib/README.md)
This would allow me to link from one .md to another within the same branch and not have to worry about which branch I'm in (avoid having to do an absolute URL that includes the github.com branch name).
Here is a working example of what I mean:
- GOTO http://github.com/rynop/testRel, link does not work.
- GOTO http://github.com/rynop/testRel/blob/master/README.md, link works.
This is expected because at this point the starting URL is in the branch. Now how do I get it to pick up the current branch in the README.md at the root of the repository?
Update: I opened an issue against GitHub for this feature request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
16 个月后2013 年 1 月 30 日更新:
GitHub 博客文章标记文件中的相对链接:
Marcono1234 添加了 评论
2011 年 12 月 20 日更新:
GitHub 标记问题 84 目前已由 technoweenie,评论如下:
2011 年 10 月 12 日:
如果您查看
的原始源代码Markdown 本身的 >README.md
(!),似乎不支持相对路径。您会发现如下参考:
如 InvisibleWolf 的 回答,如果目标链接是一个目录并且有空格,那么每个空格都需要使用
%20
。Update 30th, January 2013, 16 months later:
GitHub Blog Post Relative links in markup files:
Marcono1234 adds in the comments
Update December 20th, 2011:
The GitHub markup issue 84 is currently closed by technoweenie, with the comment:
October 12th, 2011:
If you look at the raw source of the
README.md
of Markdown itself(!), relative paths don't seem to be supported.You will find references like:
As noted in InvisibleWolf's answer, if the target link is a directory and it has space, then you need to use
%20
for each space.例如,您有如下所示的存储库:
text.md
中subtext.md
的相对链接可能如下所示:subsubtext 的相对链接。
可能如下所示:text.md
中的 mdsubsubtext.md
中subtext.md
的相对链接可能如下所示:相对链接
subsubtext.md
中的subsubtext2.md
可能如下所示:subsubtext.md 中
text.md
的相对链接> 可能看起来像这样:For example, you have a repo like the following:
The relative link to
subtext.md
intext.md
might look like this:The relative link to
subsubtext.md
intext.md
might look like this:The relative link to
subtext.md
insubsubtext.md
might look like this:The relative link to
subsubtext2.md
insubsubtext.md
might look like this:The relative link to
text.md
insubsubtext.md
might look like this:GitHub 可以用最少的工作让这一切变得更好。这是一个解决方法。
我认为你想要一些更像
或指向自述文件本身的东西
GitHub could make this a lot better with minimal work. Here is a work-around.
I think you want something more like
or to point to the README itself
截至 2013 年 1 月 31 日,Github markdown 支持文件的相对链接 。
但是,此评论线程中讨论了一些缺陷。
作为替代方案,您可以使用 Gitdown 构建 存储库的完整 URL 甚至让它们了解分支,例如
Gitdown 是一个 GitHub markdown 预处理器。它简化了与维护 GitHub 存储库文档页面相关的常见任务,例如生成目录(包括变量)、生成 URL 以及在处理输入时获取有关存储库本身的信息。 Gitdown 与您的构建脚本无缝集成。
我是 Gitdown 库的作者。
As of January 31, 2013 Github markdown supports relative links to files.
However, there are a few deficiencies that have been discussed in this comment thread.
As an alternative, you can use Gitdown to construct full URLs to the repository and even make them branch aware, e.g.
Gitdown is a GitHub markdown preprocessor. It streamlines common tasks associated with maintaining a documentation page for a GitHub repository, e.g. generating table of contents, including variables, generating URLs and getting information about the repository itself at the time of processing the input. Gitdown seamlessly integrates with your building scripts.
I am the author of the Gitdown library.
我发现适用于相对文件夹的内容:
其中相关文件夹是您尝试链接到的文件夹。最终文件夹名称后需要斜杠。我尝试在路径中包含项目名称和树/主目录以及之前答案中建议的其他内容,这会导致不存在的页面错误。子文件夹是整个存储库项目文件夹中的一个文件夹。
因此,文件链接的形式如下:
如果所需的文件不在任何文件夹或子文件夹中,则链接很简单:
例如 README 文件不在任何子文件夹中,因此这可能是您用于自述文件。
What I found to work for relative folders:
where relevant folder is the folder you are trying to link to. The slash is needed after the final folder name. I tried including the project name and tree/main in the path and the other things suggested in previous answers, that lead to a nonexistent page error. The subfolder is a folder within the overall repo project folder.
Thus, file links would be in the form of:
If the desired file is not in any folder or subfolder the link is simply:
for example the README file is not in any subfolders, so that could be the exact relative link you use for the README file.
只是想添加此内容,因为如果目标链接是名称中包含空格的目录,则上述解决方案都不起作用。如果目标链接是一个目录并且有空格,那么即使使用 \ 转义空格也不会在 Github 上呈现该链接。唯一对我有用的解决方案是对每个空间使用
%20
。例如:如果目录结构是这样的
要链接到
Top_dir
中 README.md 中的Dir A
,您可以执行以下操作:Just wanted to add this because none of the above solutions worked if target link is directory with spaces in it's name. If target link is a directory and it has space then even escaping space with \ doesn't render the link on Github. Only solution worked for me is using
%20
for each space.e.g.: if directory structure is this
To make link to
Dir A
in README.md present inTop_dir
you can do this:您可以链接到文件,但不能链接到文件夹,请记住,Github 会在相对链接之前添加
/blob/master/
(文件夹缺少该部分,因此无法链接,也无法链接) HTML标签或 Markdown 链接)。
因此,如果我们在
myrepo/src/Test.java
中有一个文件,它将具有如下 url:为了在自述文件中链接它,我们可以使用:
或:
< a href="src/Test.java">这是一个链接
。(我猜,
master
代表master
分支,当文件位于另一个分支时它会有所不同。)You can link to file, but not to folders, and keep in mind that, Github will add
/blob/master/
before your relative link(and folders lacks that part so they cannot be linked, neither with HTML<a>
tags or Markdown link).So, if we have a file in
myrepo/src/Test.java
, it will have a url like:And to link it in the readme file, we can use:
or:
<a href="src/Test.java">This is a link</a>
.(I guess,
master
represents themaster
branch and it differs when the file is in another branch.)这个问题已经很老了,但它似乎仍然很重要,因为将 readme.md 中的相关引用放到 Github 上的 wiki 页面并不容易。
我玩了一下,这个相对链接似乎工作得很好:
[Your wiki page](../../wiki/your-wiki-page)
两个
.. /
将删除/blob/master/
并使用您的基础作为起点。不过,我还没有在 Github 之外的其他存储库上尝试过这个(可能存在兼容性问题)。This question is pretty old, but it still seems important, as it isn't easy to put relative references from readme.md to wiki pages on Github.
I played around a little bit and this relative link seems to work pretty well:
[Your wiki page](../../wiki/your-wiki-page)
The two
../
will remove/blob/master/
and use your base as a starting point. I haven't tried this on other repositories than Github, though (there may be compatibility issues).您可以通过
使用存储库根目录中的相对 URL。假设您的存储库名为
testRel
,请将以下内容放入testRel/README.md
中:You can use relative URLs from the root of your repo with
<a href="">
. Assuming your repo is namedtestRel
, put the following intestRel/README.md
:在
.md
文件中,当您想要创建指向与相同项目文件夹
或基于URL
的链接相关的任何相关文件的链接时:示例:
这一行显示如下:
输入显示名称
.md
文件中添加URL
这看起来像 HeySushil - YouTube
In
.md
file when you want to create a link to any related files which is related to sameproject folder
orURL
based links:Example:
This line show like this:
Enter Showing Name
URLs
on.md
fileThis looks like HeySushil - YouTube
我不确定我是否在这里看到这个选项。您只需在存储库中创建一个
/folder
并直接使用它:不需要 blob、树或存储库名称,并且它的工作方式就像一个魅力。
I am not sure if I see this option here. You can just create a
/folder
in your repository and use it directly:No blob or tree or repository name is needed, and it works like a charm.
如果您想要 GitHub 上 wiki 页面的相对链接,请使用以下内容:
如果您想要存储库中文件的链接,比方说,引用某些头文件,并且 wiki 页面位于 wiki 的根目录,使用这个:
最后一个的基本原理是用“../”跳过“/wiki”路径,并转到存储库树中的主分支,而不指定存储库名称,该名称将来可能会发生变化。
If you want a relative link to your wiki page on GitHub, use this:
If you want a link to a file in the repository, let us say, to reference some header file, and the wiki page is at the root of the wiki, use this:
The rationale for the last is to skip the "/wiki" path with "../", and go to the master branch in the repository tree without specifying the repository name, that may change in the future.
Markdown 文件中 Github 目录链接的行为可能会造成一些麻烦。
所有文件的 url 均以 blob 开头,
而目录的 url 均以 tree 开头:
因此,目录的相对链接不应该起作用。它指向一个以 blob 开头的 URL。神奇的是,Github 自动将一些相对路径更改为树路径。不幸的是,不是包含“..”的网址,
我调查了一些时间来检查 https://github.com/fluencecodes/GitSamples-GIT/tree/github-readme-links。
The behaviour in Github directory links in markdown files could create some headaches.
All files have an url are starting with blob,
whereas directories have an url starting with tree:
So no relative link to a directory should work. It points to an url that starts with a blob. Magically Github changes some relative path automatically to the tree path. Unfortunately not urls containing ".."
I investigate some time to examine the strange behaviour with Github directory links on https://github.com/fluentcodes/GitSamples-GIT/tree/github-readme-links.
只需遵循以下格式即可。
Just follow the format below.
以下是我在 github 的
.md
文件中找到的相对链接:../
的相对链接(又名。链接到同一级别的文件,或在子目录),似乎总是有效。/blob/
的页面中时,包含../
的相对链接才有效。/blob/
的完整网址进行链接。/README.md
之外,当查看任何其他文件时,它们的 url 已经包含/blob/
。../
的相对链接,/README.md
除外。/README.md
位于顶层,因此它不需要包含../
的相对链接。示例(多语言文档之间的链接)
在
/README.md
中,可以使用不带../
的相对链接。在
/doc/cn/README.md
中,由于我们查看时,url中已经包含/blob/
,因此可以使用的相对链接../
.(顺便说一句,示例来自此 git-merge-流程存储库。)
Here is what I found for relative links in github's
.md
files:../
(aka. link to file in the same level, or in sub dir), seems always work.../
works only when you are already in a page whose url contains substring/blob/
./blob/
, to link./README.md
, when view any other files, their url already contains/blob/
.../
in files, except/README.md
./README.md
is in top level, thus it shouldn't need a relative link that contains../
.Example (links among multiple language doc)
In
/README.md
, can use relative links without../
.In
/doc/cn/README.md
, since when we view it, the url already contains/blob/
, thus could use relative links with../
.(BTW, the example come from this git-merge-flow repo.)
接受的答案引用:
......这是我需要的,但这没有提供示例。
以下是基于 OP 链接的存储库的示例。
使用存储库:github.com/rynop/testRel...
对于目录:
https://github.com/rynop/testRel/tree/master/myLib
/myLib
对于文件:
https:// /github.com/rynop/testRel/blob/master/myLib/README.md
/myLib/README.md
注意,目录 URL构建在
tree/master/
上,而文件 URL 构建在blob/master/
上。那没关系。 GitHub 会整理出来的。 (您甚至可以将
tree
与blob
互换,并且 URL 将重定向它们需要去的地方。)只需从
/
在tree/master/
或blob/master/
的末尾,您的相对链接应该可以工作。The accepted answer quotes:
...which I needed, but this does not provide an example.
Here are examples based on the repo linked from the OP.
Using repository: github.com/rynop/testRel...
For directory:
https://github.com/rynop/testRel/tree/master/myLib
/myLib
For file:
https://github.com/rynop/testRel/blob/master/myLib/README.md
/myLib/README.md
Note, directory URLs are build on
tree/master/
, while file URLs are build onblob/master/
.That doesn't matter. GitHub will sort it out. (You might even be able to interchange
tree
withblob
and URLs will redirect where they need to go.)Just start with
/
at the end oftree/master/
orblob/master/
and your relative link should work.您还可以在底部插入链接(相对或绝对)以减少文本混乱。
对于重复链接来说,它也是一个不错的选择。
这是我的链接文本 。
You can also insert your links (relative or absolute) at the bottom to reduce clutter in text.
It can also be a good option for repetitive links.
This is my linked text.