Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Inproved default 2-characters tlds handle in flex links, closes markd…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin authored and César Fernández committed May 22, 2020
1 parent e951342 commit b9b7e24
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ var defaultSchemas = {
}
};

/*eslint-disable max-len*/

// RE pattern for 2-character tlds (autogenerated by ./support/tlds_2char_gen.js)
var tlds_2ch_src_re = 'a[cdefgilmnoqrstuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvwxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eosuw]|s[abcdeghijklmnortuvxyz]|t[cdfghjklmnortvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]';

// DON'T try to make PRs with changes. Extend TLDs with LinkifyIt.tlds() instead
var tlds_default = 'biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф'.split('|');

/*eslint-enable max-len*/

////////////////////////////////////////////////////////////////////////////////

function resetScanCache(self) {
Expand Down Expand Up @@ -125,7 +132,7 @@ function compile(self) {
var tlds = self.__tlds__.slice();

if (!self.__tlds_replaced__) {
tlds.push('[a-z]{2}');
tlds.push(tlds_2ch_src_re);
}
tlds.push(re.src_xn);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"mocha": "*",
"ndoc": "^3.1.0",
"stylus": "~0.50.0",
"tlds": "^1.26.0"
"tlds": "^1.37.0"
}
}
68 changes: 68 additions & 0 deletions support/tlds_2char_gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env node

// Generates list of 2-char english tlds.
//
// Code is dirty, i know, but it's needed only once
//
'use strict';


/*eslint-disable no-console*/

function toRanges(str) {
var ranges = [], i;

str = str.slice(1, -1);

while (str.length) {
for (i = 1; ; i++) {
if (str[i] !== String.fromCharCode(str[i - 1].charCodeAt(0) + 1)) {
if (i < 3) {
ranges.push(str.slice(0, i));
} else {
ranges.push(str[0] + '-' + str[i - 1]);
}
str = str.slice(i);
break;
}
}
}
return '[' + ranges.join('') + ']';
}

var tlds = require('tlds')
.filter(function (name) {
return /^[a-z]{2}$/.test(name);
})
.sort();

//
// group by first letter
//

var result = [];

'abcdefghijklmnopqrstuvwxyz'.split('').forEach(function (letter) {
var list = tlds.filter(function (name) { return name[0] === letter; });

if (!list.length) { return; }

if (list.length < 2) {
result = result.concat(list);
return;
}

result.push(letter + '[' + list.map(function (n) { return n[1]; }).join('') + ']');
});

result = result.join('|');

console.log(result);

//
// Compact ranges
//

result = result.replace(/\[[a-z]+\]/g, toRanges);

// console.log(result);
2 changes: 2 additions & 0 deletions test/fixtures/not_links.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ _example.com
http://example.com_
@example.com

node.js and io.js

http://
http://.
http://..
Expand Down

0 comments on commit b9b7e24

Please sign in to comment.