ESLint auto-fix link_text

This commit is contained in:
Daniel Gasienica 2018-04-10 15:00:55 -04:00
parent 9d41b86162
commit d7b845326d

View file

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