@@ -37,42 +37,54 @@ function ansiToInlineStyle(text) {
37
37
return ansiToJSON ( text ) . map ( ansiJSONtoStyleBundle ) ;
38
38
}
39
39
40
+ function linkifyBundle ( bundle ) {
41
+ return {
42
+ ...bundle ,
43
+ content : bundle . content . split ( ' ' ) . reduce (
44
+ ( result , word ) => {
45
+ // If word is a URL
46
+ if ( / [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & / / = ] * ) / . test ( word ) ) {
47
+ return [
48
+ ...result ,
49
+ ' ' ,
50
+ React . createElement ( 'a' , {
51
+ href : word ,
52
+ target : '_blank'
53
+ } , `${ word } ` )
54
+ ] ;
55
+ }
56
+ const lastWord = result . pop ( ) ;
57
+ if ( lastWord ) {
58
+ // If lastWord is a `<a>` element
59
+ if ( lastWord . type ) return [ ...result , lastWord , ' ' , word ] ;
60
+ // If not, combine lastWord and word into single string
61
+ return [ ...result , [ lastWord , word ] . join ( ' ' ) ] ;
62
+ }
63
+ // If there is no lastWord because word is the first
64
+ return [ ...result , word ] ;
65
+ } ,
66
+ [ ]
67
+ )
68
+ }
69
+ }
70
+
40
71
function inlineBundleToReact ( bundle , key ) {
41
- const children = bundle . content . split ( ' ' ) . reduce (
42
- ( result , word ) => {
43
- // If word is a URL
44
- if ( / [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( [ - a - z A - Z 0 - 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
- ) ;
65
72
return React . createElement ( 'span' , {
66
73
style : bundle . style ,
67
74
key,
68
- } , children ) ;
75
+ } , bundle . content ) ;
69
76
}
70
77
71
78
function Ansi ( props ) {
72
79
return React . createElement (
73
80
'code' ,
74
81
{ } ,
75
- ansiToInlineStyle ( props . children ) . map ( inlineBundleToReact ) ) ;
82
+ props . linkify
83
+ ? ansiToInlineStyle ( props . children )
84
+ . map ( linkifyBundle )
85
+ . map ( inlineBundleToReact )
86
+ : ansiToInlineStyle ( props . children ) . map ( inlineBundleToReact )
87
+ ) ;
76
88
}
77
89
78
90
Ansi . propTypes = {
0 commit comments