Skip to content

Commit

Permalink
Use the create-html-element module
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 17, 2017
1 parent 6a0d9a1 commit 72d8831
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 60 deletions.
27 changes: 6 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
// - const urlRegex = require('url-regex');
const escapeGoat = require('escape-goat');
const createHtmlElement = require('create-html-element');

const urlRegex = () => (/(http(s)?(:\/\/))(www\.)?[a-zA-Z0-9-_.]+(\.[a-zA-Z0-9]{2,})([-a-zA-Z0-9:%_+.~#?&//=]*)/g);

Expand All @@ -9,24 +9,9 @@ module.exports = (input, options) => {
attributes: {}
}, options);

let attributes = Object.keys(options.attributes).map(key => {
const value = options.attributes[key];

if (value === false) {
return null;
}

let ret = `${escapeGoat.escape(key)}`;

if (value === true) {
return ret;
}

ret += `="${escapeGoat.escape(String(value))}"`;

return ret;
}).filter(Boolean);
attributes = attributes.length > 0 ? ' ' + attributes.join(' ') : '';

return input.replace(urlRegex(), match => `<a href="${match}"${attributes}>${match}</a>`);
return input.replace(urlRegex(), match => createHtmlElement({
name: 'a',
attributes: Object.assign({href: ''}, options.attributes, {href: match}),
value: match
}));
};
74 changes: 37 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
{
"name": "linkify-urls",
"version": "1.1.0",
"description": "Linkify URLs in text",
"license": "MIT",
"repository": "sindresorhus/linkify-urls",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"linkify",
"urls",
"url",
"link",
"reference",
"references",
"linkifies",
"href"
],
"dependencies": {
"escape-goat": "^1.0.0",
"url-regex": "^4.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "linkify-urls",
"version": "1.1.0",
"description": "Linkify URLs in text",
"license": "MIT",
"repository": "sindresorhus/linkify-urls",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"linkify",
"urls",
"url",
"link",
"reference",
"references",
"linkifies",
"href"
],
"dependencies": {
"create-html-element": "^1.0.0",
"url-regex": "^4.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ linkifyUrls('See https://sindresorhus.com', {
attributes: {
class: 'unicorn',
one: 1,
foo: true
foo: true,
multiple: ['a', 'b']
}
});
//=> 'See <a href="https://sindresorhus.com" class="unicorn" one="1" foo>https://sindresorhus.com</a>'
//=> 'See <a href="https://sindresorhus.com" class="unicorn" one="1" foo multiple="a b">https://sindresorhus.com</a>'
```


Expand Down

0 comments on commit 72d8831

Please sign in to comment.