Skip to content

Commit aa0df06

Browse files
committed
Add commit support.
1 parent 72df691 commit aa0df06

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,25 @@ The following table provides various examples (with the defaults set as
113113

114114
### Commits
115115

116-
This feature is *not yet implemented*.
116+
Link directly to a GitHub Commit. Note that no verification is made that an
117+
actual commit exists.
118+
119+
Commit links consist of a complete 40 character SHA hash and may optionally be
120+
prefaced by `{user}@` or `{user/project}@`. `{user}` and `{project}` will use
121+
the defaults defined in the configuration options if not provided. To avoid a 40
122+
character hash from being linked, wrap it in a code span.
123+
124+
All issue links are assigned the `gh-commit` class.
125+
126+
The following table provides various examples (with the defaults set as
127+
`user='user', project='project'`):
128+
129+
| shorthand | href | rendered result |
130+
| -------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------|
131+
| `72df691791fb36f00cf5363fefe757c8d3042656` | `https://github.com/user/project/commit/72df691791fb36f00cf5363fefe757c8d3042656` | [72df691](https://github.com/user/project/commit/72df691791fb36f00cf5363fefe757c8d3042656 "GitHub Commit: user/project@72df691791fb36f00cf5363fefe757c8d3042656") |
132+
| `foo@72df691791fb36f00cf5363fefe757c8d3042656` | `https://github.com/foo/project/commit/72df691791fb36f00cf5363fefe757c8d3042656` | [foo@72df691](https://github.com/foo/project/commit/72df691791fb36f00cf5363fefe757c8d3042656 "GitHub Commit: foo/project@72df691791fb36f00cf5363fefe757c8d3042656") |
133+
| `foo/bar@72df691791fb36f00cf5363fefe757c8d3042656` | `https://github.com/foo/bar/commit/72df691791fb36f00cf5363fefe757c8d3042656` | [foo/bar@72df691](https://github.com/foo/bar/commit/72df691791fb36f00cf5363fefe757c8d3042656 "GitHub Commit: foo/bar@72df691791fb36f00cf5363fefe757c8d3042656") |
134+
| `` `72df691791fb36f00cf5363fefe757c8d3042656` `` | | `72df691791fb36f00cf5363fefe757c8d3042656` |
117135

118136
## License
119137

mdx_gh_links.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
URL_BASE = 'https://github.com'
3939
RE_PARTS = dict(
40-
USER=r'[-_\w]+',
40+
USER=r'[-_\w]{1,39}\b',
4141
PROJECT=r'[-_.\w]+\b'
4242
)
4343

@@ -86,6 +86,27 @@ def handleMatch(self, m):
8686
return _build_link(label, title, href, 'gh-link gh-issue')
8787

8888

89+
class CommitPattern(Pattern):
90+
def __init__(self, config, md):
91+
COMMIT_RE = r'((?:({USER})(?:\/({PROJECT}))?@|\b)([a-f0-9]{{40}})\b)'.format(**RE_PARTS)
92+
super(CommitPattern, self).__init__(COMMIT_RE, md)
93+
self.config = config
94+
95+
def handleMatch(self, m):
96+
user = m.group(3)
97+
project = m.group(4) or self.config['project']
98+
commit = m.group(5)
99+
short = commit[:7]
100+
if user:
101+
label = '{0}@{1}'.format(m.group(2).split('@')[0], short)
102+
else:
103+
label = short
104+
user = self.config['user']
105+
title = 'GitHub Commit: {0}/{1}@{2}'.format(user, project, commit)
106+
href = '{0}/{1}/{2}/commit/{3}'.format(URL_BASE, user, project, commit)
107+
return _build_link(label, title, href, 'gh-link gh-commit')
108+
109+
89110
class GithubLinks(Extension):
90111
def __init__(self, *args, **kwargs):
91112
self.config = {
@@ -98,6 +119,7 @@ def extendMarkdown(self, md, md_globals):
98119
md.ESCAPED_CHARS.append('@')
99120
md.inlinePatterns['issue'] = IssuePattern(self.getConfigs(), md)
100121
md.inlinePatterns['mention'] = MentionPattern(self.getConfigs(), md)
122+
md.inlinePatterns['commit'] = CommitPattern(self.getConfigs(), md)
101123

102124

103125
def makeExtension(*args, **kwargs):

test_gh_links.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,38 @@ def test_escape_mention_project(self):
147147
'<p>User @foo/bar.</p>',
148148
)
149149

150+
# Commit Tests
151+
def test_commit(self):
152+
self.assertMarkdownRenders(
153+
'Commit 83fb46b3b7ab8ad4316681fc4637c521da265f1d.',
154+
'<p>Commit <a class="gh-link gh-commit" '
155+
'href="https://github.com/Python-Markdown/github-links/commit/83fb46b3b7ab8ad4316681fc4637c521da265f1d" '
156+
'title="GitHub Commit: Python-Markdown/github-links@83fb46b3b7ab8ad4316681fc4637c521da265f1d"'
157+
'>83fb46b</a>.</p>',
158+
)
159+
160+
def test_commit_user(self):
161+
self.assertMarkdownRenders(
162+
'Commit foo@15abb8b3b02df0e380e9b4c71f3bd206c9751a93.',
163+
'<p>Commit <a class="gh-link gh-commit" '
164+
'href="https://github.com/foo/github-links/commit/15abb8b3b02df0e380e9b4c71f3bd206c9751a93" '
165+
'title="GitHub Commit: foo/github-links@15abb8b3b02df0e380e9b4c71f3bd206c9751a93"''>foo@15abb8b</a>.</p>',
166+
)
167+
168+
def test_commit_user_project(self):
169+
self.assertMarkdownRenders(
170+
'Commit foo/bar@a75944f869d728ca9bc5472daf3f249b6c341308.',
171+
'<p>Commit <a class="gh-link gh-commit" '
172+
'href="https://github.com/foo/bar/commit/a75944f869d728ca9bc5472daf3f249b6c341308" '
173+
'title="GitHub Commit: foo/bar@a75944f869d728ca9bc5472daf3f249b6c341308">foo/bar@a75944f</a>.</p>',
174+
)
175+
176+
def test_escape_commit(self):
177+
self.assertMarkdownRenders(
178+
'Commit `83fb46b3b7ab8ad4316681fc4637c521da265f1d`.',
179+
'<p>Commit <code>83fb46b3b7ab8ad4316681fc4637c521da265f1d</code>.</p>',
180+
)
181+
150182

151183
if __name__ == '__main__':
152184
unittest.main()

0 commit comments

Comments
 (0)