@@ -3,11 +3,12 @@ import { useContext, useEffect, useState } from "react";
3
3
import { Mention } from "./IComment" ;
4
4
import { Text } from "@fluentui/react/lib/Text" ;
5
5
import { LivePersona } from "../../../LivePersona" ;
6
- import { AppContext } from " ../../common" ;
7
- import regexifyString from " regexify-string" ;
8
- import { Stack } from " @fluentui/react/lib/Stack" ;
9
- import { isArray , isObject } from " lodash" ;
6
+ import { AppContext , URL_REGEX } from ' ../../common' ;
7
+ import regexifyString from ' regexify-string' ;
8
+ import { Stack } from ' @fluentui/react/lib/Stack' ;
9
+ import { isArray , isObject } from ' lodash' ;
10
10
import he from 'he' ;
11
+ import { Link } from '@fluentui/react' ;
11
12
export interface ICommentTextProps {
12
13
text : string ;
13
14
mentions : Mention [ ] ;
@@ -46,26 +47,55 @@ export const CommentText: React.FunctionComponent<ICommentTextProps> = (
46
47
setCommentText ( result ) ;
47
48
} , [ ] ) ;
48
49
50
+ const convertTextToLinksAndText = (
51
+ text : string
52
+ ) : ( string | JSX . Element ) [ ] => {
53
+ const parts = text . split ( URL_REGEX ) ;
54
+ return parts . map ( ( part , index ) => {
55
+ if ( part . match ( URL_REGEX ) ) {
56
+ return (
57
+ < Link
58
+ key = { index }
59
+ href = { part }
60
+ target = "_blank"
61
+ rel = "noopener noreferrer"
62
+ style = { { color : theme . link } }
63
+ >
64
+ { part }
65
+ </ Link >
66
+ ) ;
67
+ }
68
+ return part ;
69
+ } ) ;
70
+ } ;
71
+
49
72
return (
50
73
< >
51
74
< Stack wrap horizontal horizontalAlign = "start" verticalAlign = "center" >
52
75
{ isArray ( commentText ) ? (
53
- ( commentText as any [ ] ) . map ( ( el , i ) => { // eslint-disable-line @typescript-eslint/no-explicit-any
76
+ ( commentText as any [ ] ) . map ( ( el , i ) => {
77
+ // eslint-disable-line @typescript-eslint/no-explicit-any
54
78
if ( isObject ( el ) ) {
55
- return < span key = { i } style = { { paddingRight : 5 } } > { el } </ span > ;
79
+ return (
80
+ < span key = { i } style = { { paddingRight : 5 } } >
81
+ { el }
82
+ </ span >
83
+ ) ;
56
84
} else {
57
85
const _el : string = el . trim ( ) ;
58
86
if ( _el . length ) {
59
87
return (
60
88
< Text style = { { paddingRight : 5 } } variant = "small" key = { i } >
61
- { he . decode ( _el ) }
89
+ { convertTextToLinksAndText ( he . decode ( _el ) ) }
62
90
</ Text >
63
91
) ;
64
92
}
65
93
}
66
94
} )
67
95
) : (
68
- < Text variant = "small" > { he . decode ( commentText ) } </ Text >
96
+ < Text variant = "small" >
97
+ { convertTextToLinksAndText ( he . decode ( commentText ) ) }
98
+ </ Text >
69
99
) }
70
100
</ Stack >
71
101
</ >
0 commit comments