Skip to content

Commit

Permalink
simplifying url pattern regex
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandosborne committed Oct 20, 2024
1 parent 3141ba0 commit f074bfe
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions net/web/src/session/conversation/useConversation.hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,15 @@ export function useConversation(cardId, channelId) {
}, [state.contentKey]);

const clickableText = (text) => {
const urlPattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator

const urlPattern = new RegExp('(https?:\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)');
const hostPattern = new RegExp('^https?:\\/\\/', 'i');

let group = '';
let clickable = [];

const words = text === [] ? '' : DOMPurify.sanitize(text).split(' ');

words.forEach((word, index) => {
if (!!urlPattern.test(word)) {
clickable.push(<span key={index}>{ group }</span>);
Expand All @@ -161,6 +158,7 @@ export function useConversation(cardId, channelId) {
group += `${word} `;
}
})

clickable.push(<span key={words.length}>{ group }</span>);
return <p>{ clickable }</p>;
};
Expand Down

0 comments on commit f074bfe

Please sign in to comment.