Skip to content

Commit 627ab69

Browse files
committed
Linkify URLs in plain text output
1 parent b548a43 commit 627ab69

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,34 @@ function ansiToInlineStyle(text) {
3838
}
3939

4040
function inlineBundleToReact(bundle, key) {
41+
const children = bundle.content.split(' ').reduce(
42+
(result, word) => {
43+
// If word is a URL
44+
if (/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(word)) {
45+
return [
46+
...result,
47+
React.createElement('a', {
48+
href: word ,
49+
target: '_blank'
50+
}, `${word} `)
51+
];
52+
}
53+
const lastWord = result.pop();
54+
if (lastWord) {
55+
// If lastWord is a `<a>` element
56+
if (lastWord.type) return [...result, lastWord, word];
57+
// If not, combine lastWord and word into single string
58+
return [...result, [lastWord, word].join(' ')];
59+
}
60+
// If there is no lastWord because word is the first
61+
return [...result, word];
62+
},
63+
[]
64+
);
4165
return React.createElement('span', {
4266
style: bundle.style,
4367
key,
44-
}, bundle.content);
68+
}, children);
4569
}
4670

4771
function Ansi(props) {

0 commit comments

Comments
 (0)