Skip to content

Commit

Permalink
Link email addresses in markup.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Aug 2, 2024
1 parent a3142c8 commit b8ab7f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Dates should be in`YYYY-MM-DD` format and versions are in [semantic versioning](
### Added

- Added ability to move how items up and down in a list.
- Link email addresses in markup.

### Fixed

Expand Down
23 changes: 22 additions & 1 deletion src/markup/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Heading from './Heading';
import Quote from './Quote';

const BulletPrefixes = ['* ', '• ', '- '];
const EmailRegex =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/;

function isBullets(line: string): boolean {
return BulletPrefixes.some((b) => line.startsWith(b));
Expand Down Expand Up @@ -99,7 +101,26 @@ export function parseSegments(line: string): Segment[] {
line = line.substring(index + 1);
// Add a new link.
segments.push(new Link(label ?? '', url ?? ''));
} // Add to accumulator.
} else if (EmailRegex.test(next + line)) {
// Save what's accumulated.
if (characters.length > 0) {
segments.push(new Characters('', characters));
characters = '';
}
// Get the match
const match = EmailRegex.exec(next + line);

if (match) {
const email = match[0];
const index = email.length;

// Move to after the end of the link
line = line.substring(index - 1);
// Add a new link.
segments.push(new Link(email, `mailto:${email}`));
} else characters = characters + next;
}
// Add to accumulator if none of the above matched.
else characters = characters + next;
}

Expand Down

0 comments on commit b8ab7f7

Please sign in to comment.