Skip to content

Commit

Permalink
fix: improve @mention parsing for GitHub usernames (#33)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
drichar authored Oct 18, 2024
1 parent 37afa88 commit 925765f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ const reduceNewlines = (text) => text.replace(/\n\s*\n/g, (ws) => {
});

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

/**
* Reduces headings to a smaller format if 'reduce_headings' is enabled.
Expand Down

0 comments on commit 925765f

Please sign in to comment.