Skip to content

Commit 918d8aa

Browse files
committed
Correct handling of mailto: links
Resolves #2613
1 parent 22fc83e commit 918d8aa

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

Diff for: CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Unreleased
22

3-
### Features
3+
### Bug Fixes
44

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

78
## v0.26.2 (2024-06-24)

Diff for: src/lib/converter/comments/textParser.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function checkMarkdownLink(
189189
if (link.ok) {
190190
// Only make a relative-link display part if it's actually a relative link.
191191
// Discard protocol:// links, unix style absolute paths, and windows style absolute paths.
192-
if (isRelativeLink(link.str)) {
192+
if (isRelativePath(link.str)) {
193193
return {
194194
pos: labelEnd + 2,
195195
end: link.pos,
@@ -240,7 +240,7 @@ function checkReference(data: TextParserData): RelativeLink | undefined {
240240
);
241241

242242
if (link.ok) {
243-
if (isRelativeLink(link.str)) {
243+
if (isRelativePath(link.str)) {
244244
return {
245245
pos: lookahead,
246246
end: link.pos,
@@ -284,7 +284,7 @@ function checkAttribute(
284284
) {
285285
parser.step();
286286

287-
if (isRelativeLink(parser.currentAttributeValue)) {
287+
if (isRelativePath(parser.currentAttributeValue)) {
288288
data.pos = parser.pos;
289289
return {
290290
pos: parser.currentAttributeValueStart,
@@ -302,8 +302,14 @@ function checkAttribute(
302302
}
303303
}
304304

305-
function isRelativeLink(link: string) {
306-
return !/^[a-z]+:\/\/|^\/|^[a-z]:\\|^#/i.test(link);
305+
function isRelativePath(link: string) {
306+
// Lots of edge cases encoded right here!
307+
// Originally, this attempted to match protocol://, but...
308+
// `mailto:[email protected]` is not a relative path
309+
// `C:\foo` is not a relative path
310+
// `/etc/passwd` is not a relative path
311+
// `#anchor` is not a relative path
312+
return !/^[a-z]+:|^\/|^#/i.test(link);
307313
}
308314

309315
function findLabelEnd(text: string, pos: number) {

Diff for: src/test/comments.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,16 @@ describe("Comment Parser", () => {
14401440
] satisfies CommentDisplayPart[]);
14411441
});
14421442

1443+
it("Does not mistake mailto: links as relative paths", () => {
1444+
const comment = getComment(`/**
1445+
* [1]: mailto:[email protected]
1446+
*/`);
1447+
1448+
equal(comment.summary, [
1449+
{ kind: "text", text: "[1]: mailto:[email protected]" },
1450+
] satisfies CommentDisplayPart[]);
1451+
});
1452+
14431453
it("Recognizes HTML image links", () => {
14441454
const comment = getComment(`/**
14451455
* <img width=100 height="200" src="./test.png" >

0 commit comments

Comments
 (0)