用于在 Markdown 中模拟 GitHub 自动链接引用的正则表达式

Regex to emulate GitHub autolink references in Markdown

模拟 GitHub's autolinked references 的正则表达式是什么?

输入 Markdown 并输出丰富的 Markdown,其中像 #123 这样的字符串被转换为 [#123](https://github.com/owner/repo/issues/123).

这些是我希望正则表达式执行的一些转换示例:

输入:

1. #123
2. https://github.com/owner/repo/issues/123
3. https://github.com/shoptet/sofa/pull/456
4. owner/repo#123
5. https://github.com/owner/repo/issues/123#issuecomment-123456789

输出:

1. [#123](https://github.com/owner/repo/issues/123)
2. [#123](https://github.com/owner/repo/issues/123)
3. [#123](https://github.com/owner/repo/pull/456)
4. [owner/repo#123](https://github.com/owner/repo/issues/123)
5. [#123 (comment)](https://github.com/owner/repo/issues/123#issuecomment-123456789)

如果可能的话,我更喜欢一个巨大的正则表达式(我知道它不会很好,但可以让我直接在几个我最喜欢的编辑器中处理 Markdown)。

我仍然更喜欢(复杂的)正则表达式,但如果有人像我一样正在寻找相同的 post-processing,这个包可以在 Node.js 脚本中解决它:

https://github.com/remarkjs/remark-github

如果您不介意稍微更改一下格式(使用 [#123-comment] 而不是 [#123 (comment)] 作为注释),您可以使用这个:

(?:(owner/repo)?#(\d+)\b|https?://github\.com/([^/]+/[^/]+/(?:issues|pull))/(\d+)(#issue(comment)(-)\d+)?)

替换为:[#](https://github.com/owner/repo/issues/)

您有一个演示 here