Skip to content

Commit 925765f

Browse files
authoredOct 18, 2024··
fix: improve @mention parsing for GitHub usernames (#33)
- Update regex to correctly identify valid GitHub usernames - Prevent false positives for package names and email addresses - Exclude matches within code snippets, URLs, and Markdown links
1 parent 37afa88 commit 925765f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed
 

‎index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ const reduceNewlines = (text) => text.replace(/\n\s*\n/g, (ws) => {
2828
});
2929

3030
/**
31-
* Converts @mentions to GitHub profile links.
31+
* Converts @mentions to GitHub profile links for valid GitHub usernames.
3232
* @param {string} text The input text.
33-
* @returns {string} The text with @mentions converted to links.
33+
* @returns {string} The text with valid @mentions converted to links.
3434
*/
35-
const convertMentionsToLinks = (text) => text.replace(/@(\S+)/g, (match, name) => `[@${name}](https://github.com/${name})`);
35+
const convertMentionsToLinks = (text) => text.replace(
36+
/(?<![/@\w])@((?!-)(?!.*?--)[a-zA-Z0-9](?:-?[a-zA-Z0-9]){0,37})(?![.\w/-])(?!.*\])/g,
37+
(match, name) => `[@${name}](https://github.com/${name})`
38+
);
3639

3740
/**
3841
* Reduces headings to a smaller format if 'reduce_headings' is enabled.

0 commit comments

Comments
 (0)
Please sign in to comment.