|
|
@ -5,25 +5,31 @@ export const replaceUrlsWithLink = (text: string): boolean | string => { |
|
|
|
} |
|
|
|
} |
|
|
|
const rawText = text.toString() |
|
|
|
const rawText = text.toString() |
|
|
|
|
|
|
|
|
|
|
|
// create a temporary element to sanitise the string
|
|
|
|
// Create a temporary element to sanitize the string by encoding any HTML code
|
|
|
|
// by encoding any html code
|
|
|
|
|
|
|
|
const tempEl = document.createElement('div') |
|
|
|
const tempEl = document.createElement('div') |
|
|
|
tempEl.textContent = rawText |
|
|
|
tempEl.textContent = rawText |
|
|
|
const sanitisedText = tempEl.innerHTML |
|
|
|
const sanitisedText = tempEl.innerHTML |
|
|
|
|
|
|
|
|
|
|
|
let found = false |
|
|
|
const protocolRegex = /^(https?|ftp|mailto|file):\/\// |
|
|
|
const out = sanitisedText.replace(/URI::\(([^)]+)\)(?: LABEL::\(([^)]*)\))?/g, (_, url, label) => { |
|
|
|
|
|
|
|
found = true |
|
|
|
const out = sanitisedText.replace(/URI::\(([^)]*)\)(?: LABEL::\(([^)]*)\))?/g, (_, url, label) => { |
|
|
|
|
|
|
|
if (!url.trim() && !label) { |
|
|
|
|
|
|
|
return ' ' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fullUrl = protocolRegex.test(url) ? url : url.trim() ? `http://${url}` : '' |
|
|
|
|
|
|
|
const anchorLabel = label || url || '' |
|
|
|
|
|
|
|
|
|
|
|
const a = document.createElement('a') |
|
|
|
const a = document.createElement('a') |
|
|
|
a.textContent = label || url // Use the label if it exists and is not empty, otherwise use the URL
|
|
|
|
a.textContent = anchorLabel |
|
|
|
a.setAttribute('href', url) |
|
|
|
a.setAttribute('href', fullUrl) |
|
|
|
a.setAttribute('class', ' nc-cell-field-link') |
|
|
|
a.setAttribute('class', ' nc-cell-field-link') |
|
|
|
a.setAttribute('target', '_blank') |
|
|
|
a.setAttribute('target', '_blank') |
|
|
|
a.setAttribute('rel', 'noopener,noreferrer') |
|
|
|
a.setAttribute('rel', 'noopener,noreferrer') |
|
|
|
return a.outerHTML |
|
|
|
return a.outerHTML |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return found && out |
|
|
|
return out |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const isValidURL = (str: string, extraProps?) => { |
|
|
|
export const isValidURL = (str: string, extraProps?) => { |
|
|
|