|
41 | 41 |
|
42 | 42 | VARIABLES_RE = re.compile(r'\${([a-zA-Z_]+)}')
|
43 | 43 |
|
| 44 | +# Pattern for matching an auto-linked GitHub username. |
| 45 | +# |
| 46 | +# This is the behavior used by GitHub when detecting usernames, as deduced from |
| 47 | +# the username constraints for new accounts and testing in their Markdown |
| 48 | +# renderer: |
| 49 | +# - Usernames are auto-linked with an @ prefix. |
| 50 | +# - Usernames can only contain alphanumeric characters or hyphens and must be |
| 51 | +# between 1 and 39 characters (inclusive): /@[A-Za-z0-9\-]{1,39}/ |
| 52 | +# - Usernames cannot start with hyphen: /@[A-Za-z0-9][A-Za-z0-9\-]{,38}/ |
| 53 | +# - A username preceded by an alphanumeric character or underscore is not |
| 54 | +# auto-linked: /(?<![A-Za-z0-9_])/ |
| 55 | +# - A username followed by an underscore or slash is not auto-linked: |
| 56 | +# /(?![A-Za-z0-9\-_\/])/ |
| 57 | +# |
| 58 | +# Although usernames cannot contain consecutive hyphens or end with a hyphen, |
| 59 | +# GitHub still auto-links such patterns. |
| 60 | +# |
| 61 | +# The logic for username auto-linking appears to not be open source. |
| 62 | +# github-markup (https://github.com/github/markup), the markup renderer used by |
| 63 | +# GitHub, does not contain this logic, nor do its dependencies. Their README |
| 64 | +# says this is part of their "special sauce". |
| 65 | +# |
| 66 | +# This pattern does not handle Markdown code blocks. |
| 67 | +GITHUB_USERNAME_RE = re.compile(r'(?<![A-Za-z0-9_])(@[A-Za-z0-9][A-Za-z0-9\-]{,38})(?![A-Za-z0-9\-_/])') |
| 68 | + |
44 | 69 | IGNORE_BLOCK_START = '<!-- homu-ignore:start -->'
|
45 | 70 | IGNORE_BLOCK_END = '<!-- homu-ignore:end -->'
|
46 | 71 | IGNORE_BLOCK_RE = re.compile(
|
|
56 | 81 | # Replace @mention with `@mention` to suppress pings in merge commits.
|
57 | 82 | # Note: Don't replace non-mentions like "[email protected]".
|
58 | 83 | def suppress_pings(text):
|
59 |
| - return re.sub(r'\B(@\S+)', r'`\g<1>`', text) # noqa |
| 84 | + return GITHUB_USERNAME_RE.sub(r'`\g<1>`', text) |
60 | 85 |
|
61 | 86 |
|
62 | 87 | # Replace any text between IGNORE_BLOCK_START and IGNORE_BLOCK_END
|
|
0 commit comments