Files
Signal-Desktop/js/modules/link_text.js
Daniel Gasienica 9d41b86162 Remove escaping from linkText
We leverage jQuery’s HTML escaping in `$.html(…)`.
2018-04-11 16:36:42 -04:00

46 lines
934 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Fork of https://github.com/uiureo/link-text with HTML escaping disabled as we leverage
// jQuerys escaping mechanism:
var linkify = require('linkify-it')()
function createLink (url, text, attrs) {
attrs = attrs || {}
var html = []
html.push('<a ')
html.push('href="' + url + '"')
Object.keys(attrs).forEach(function (key) {
html.push(' ' + key + '="' + attrs[key] + '"')
})
html.push('>')
html.push(decodeURIComponent(text))
html.push('</a>')
return html.join('')
}
module.exports = function (text, attrs) {
attrs = attrs || {}
var matchData = linkify.match(text) || []
var result = []
var last = 0
matchData.forEach(function (match) {
if (last < match.index) {
result.push(text.slice(last, match.index))
}
result.push(
createLink(match.url, match.text, attrs)
)
last = match.lastIndex
})
result.push(text.slice(last))
return result.join('')
}