Skip to content

Commit 02d5928

Browse files
committed
fix: links must have protocol.
As most markdown parses don't parse links without protocols.
1 parent bda54a0 commit 02d5928

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/src/index.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@ import { describe, test } from "vitest";
22
import { autoLinkMd } from "../src";
33

44
describe("react-markdown-autolink", () => {
5-
test("test autolink", ({ expect }) => {
6-
expect(autoLinkMd("this is a link: google.com")).toBe("this is a link: <google.com>");
5+
test("test autolink url", ({ expect }) => {
6+
expect(autoLinkMd("this is a link: https://google.com")).toBe(
7+
"this is a link: <https://google.com>",
8+
);
9+
});
10+
test("test autolink email", ({ expect }) => {
11+
expect(autoLinkMd("this is a email: [email protected]")).toBe(
12+
"this is a email: <[email protected]>",
13+
);
14+
});
15+
test("test autolink with mixed text", ({ expect }) => {
16+
expect(
17+
autoLinkMd(
18+
"this is a email: [email protected] and this is url: https://google.com and this is invalid url: google.com (without protocol)",
19+
),
20+
).toBe(
21+
"this is a email: <[email protected]> and this is url: <https://google.com> and this is invalid url: google.com (without protocol)",
22+
);
723
});
824
});

lib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const autoLinkMd = (str: string) =>
22
str.replace(
3-
/((ftp|https?):\/\/)?[\w_-]+(\.[\w_-]+)+[\w@?^=%&\/~+#-.:,]*[\w@?^=%&\/~+#-]/g,
3+
/(ftp|https?):\/\/[\w_-]+(\.[\w_-]+)+[\w@?^=%&\/~+#-.:,]*[\w@?^=%&\/~+#-]|[\w._%+-]+@[\w.-]+\.[a-zA-Z]{2,}/g,
44
match => `<${match}>`,
55
);

0 commit comments

Comments
 (0)