Skip to content

Commit a75ae62

Browse files
authored
Merge pull request #45 from kycutler/kycutler/linkifyfix
Fix linkify when links are separated by one space
2 parents 322ccf7 + 20a5813 commit a75ae62

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

__tests__/index.spec.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,36 @@ describe("Ansi", () => {
163163
expect(el.childAt(0).children()).toHaveLength(3);
164164
});
165165

166+
test("can linkify multiple links one after another", () => {
167+
const el = shallow(
168+
React.createElement(
169+
Ansi,
170+
{ linkify: true },
171+
"www.google.com www.google.com www.google.com"
172+
)
173+
);
174+
expect(el).not.toBeNull();
175+
expect(el.text()).toBe("www.google.com www.google.com www.google.com");
176+
expect(el.html()).toBe(
177+
'<code><span><a href="http://www.google.com" target="_blank">www.google.com</a> <a href="http://www.google.com" target="_blank">www.google.com</a> <a href="http://www.google.com" target="_blank">www.google.com</a></span></code>'
178+
);
179+
});
180+
181+
test("can handle URLs inside query parameters", () => {
182+
const el = shallow(
183+
React.createElement(
184+
Ansi,
185+
{ linkify: true },
186+
"www.google.com/?q=https://www.google.com"
187+
)
188+
);
189+
expect(el).not.toBeNull();
190+
expect(el.text()).toBe("www.google.com/?q=https://www.google.com");
191+
expect(el.html()).toBe(
192+
'<code><span><a href="http://www.google.com/?q=https://www.google.com" target="_blank">www.google.com/?q=https://www.google.com</a></span></code>'
193+
);
194+
});
195+
166196
describe("useClasses options", () => {
167197
test("can add the font color class", () => {
168198
const el = shallow(

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ function convertBundleIntoReact(
101101
}
102102

103103
const content: React.ReactNode[] = [];
104-
const linkRegex = /(\s+|^)(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})(\s+|$)/g;
104+
const linkRegex = /(\s+|^)(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/g;
105105

106106
let index = 0;
107107
let match: RegExpExecArray | null;
108108
while ((match = linkRegex.exec(bundle.content)) !== null) {
109-
const [ , pre, url, post ] = match;
109+
const [ , pre, url ] = match;
110110

111111
const startIndex = match.index + pre.length;
112112
if (startIndex > index) {
@@ -128,8 +128,7 @@ function convertBundleIntoReact(
128128
)
129129
);
130130

131-
const endIndex = linkRegex.lastIndex - post.length;
132-
index = endIndex;
131+
index = linkRegex.lastIndex;
133132
}
134133

135134
if (index < bundle.content.length) {

0 commit comments

Comments
 (0)