Skip to content

Commit

Permalink
Correct handling of mailto: links
Browse files Browse the repository at this point in the history
Resolves #2613
  • Loading branch information
Gerrit0 committed Jun 25, 2024
1 parent 22fc83e commit 918d8aa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Unreleased

### Features
### Bug Fixes

- `mailto:` links are no longer incorrectly recognized as relative paths, #2613.
- Added `@since` to the default list of recognized tags, #2614.

## v0.26.2 (2024-06-24)
Expand Down
16 changes: 11 additions & 5 deletions src/lib/converter/comments/textParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function checkMarkdownLink(
if (link.ok) {
// Only make a relative-link display part if it's actually a relative link.
// Discard protocol:// links, unix style absolute paths, and windows style absolute paths.
if (isRelativeLink(link.str)) {
if (isRelativePath(link.str)) {
return {
pos: labelEnd + 2,
end: link.pos,
Expand Down Expand Up @@ -240,7 +240,7 @@ function checkReference(data: TextParserData): RelativeLink | undefined {
);

if (link.ok) {
if (isRelativeLink(link.str)) {
if (isRelativePath(link.str)) {
return {
pos: lookahead,
end: link.pos,
Expand Down Expand Up @@ -284,7 +284,7 @@ function checkAttribute(
) {
parser.step();

if (isRelativeLink(parser.currentAttributeValue)) {
if (isRelativePath(parser.currentAttributeValue)) {
data.pos = parser.pos;
return {
pos: parser.currentAttributeValueStart,
Expand All @@ -302,8 +302,14 @@ function checkAttribute(
}
}

function isRelativeLink(link: string) {
return !/^[a-z]+:\/\/|^\/|^[a-z]:\\|^#/i.test(link);
function isRelativePath(link: string) {
// Lots of edge cases encoded right here!
// Originally, this attempted to match protocol://, but...
// `mailto:example@example.com` is not a relative path
// `C:\foo` is not a relative path
// `/etc/passwd` is not a relative path
// `#anchor` is not a relative path
return !/^[a-z]+:|^\/|^#/i.test(link);
}

function findLabelEnd(text: string, pos: number) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,16 @@ describe("Comment Parser", () => {
] satisfies CommentDisplayPart[]);
});

it("Does not mistake mailto: links as relative paths", () => {
const comment = getComment(`/**
* [1]: mailto:example@example.com
*/`);

equal(comment.summary, [
{ kind: "text", text: "[1]: mailto:example@example.com" },
] satisfies CommentDisplayPart[]);
});

it("Recognizes HTML image links", () => {
const comment = getComment(`/**
* <img width=100 height="200" src="./test.png" >
Expand Down

0 comments on commit 918d8aa

Please sign in to comment.