index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. 'use strict';
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Helpers
  4. // Merge objects
  5. //
  6. function assign(obj /*from1, from2, from3, ...*/) {
  7. var sources = Array.prototype.slice.call(arguments, 1);
  8. sources.forEach(function (source) {
  9. if (!source) { return; }
  10. Object.keys(source).forEach(function (key) {
  11. obj[key] = source[key];
  12. });
  13. });
  14. return obj;
  15. }
  16. function _class(obj) { return Object.prototype.toString.call(obj); }
  17. function isString(obj) { return _class(obj) === '[object String]'; }
  18. function isObject(obj) { return _class(obj) === '[object Object]'; }
  19. function isRegExp(obj) { return _class(obj) === '[object RegExp]'; }
  20. function isFunction(obj) { return _class(obj) === '[object Function]'; }
  21. function escapeRE(str) { return str.replace(/[.?*+^$[\]\\(){}|-]/g, '\\$&'); }
  22. ////////////////////////////////////////////////////////////////////////////////
  23. var defaultOptions = {
  24. fuzzyLink: true,
  25. fuzzyEmail: true,
  26. fuzzyIP: false
  27. };
  28. function isOptionsObj(obj) {
  29. return Object.keys(obj || {}).reduce(function (acc, k) {
  30. return acc || defaultOptions.hasOwnProperty(k);
  31. }, false);
  32. }
  33. var defaultSchemas = {
  34. 'http:': {
  35. validate: function (text, pos, self) {
  36. var tail = text.slice(pos);
  37. if (!self.re.http) {
  38. // compile lazily, because "host"-containing variables can change on tlds update.
  39. self.re.http = new RegExp(
  40. '^\\/\\/' + self.re.src_auth + self.re.src_host_port_strict + self.re.src_path, 'i'
  41. );
  42. }
  43. if (self.re.http.test(tail)) {
  44. return tail.match(self.re.http)[0].length;
  45. }
  46. return 0;
  47. }
  48. },
  49. 'https:': 'http:',
  50. 'ftp:': 'http:',
  51. '//': {
  52. validate: function (text, pos, self) {
  53. var tail = text.slice(pos);
  54. if (!self.re.no_http) {
  55. // compile lazily, because "host"-containing variables can change on tlds update.
  56. self.re.no_http = new RegExp(
  57. '^' +
  58. self.re.src_auth +
  59. // Don't allow single-level domains, because of false positives like '//test'
  60. // with code comments
  61. '(?:localhost|(?:(?:' + self.re.src_domain + ')\\.)+' + self.re.src_domain_root + ')' +
  62. self.re.src_port +
  63. self.re.src_host_terminator +
  64. self.re.src_path,
  65. 'i'
  66. );
  67. }
  68. if (self.re.no_http.test(tail)) {
  69. // should not be `://` & `///`, that protects from errors in protocol name
  70. if (pos >= 3 && text[pos - 3] === ':') { return 0; }
  71. if (pos >= 3 && text[pos - 3] === '/') { return 0; }
  72. return tail.match(self.re.no_http)[0].length;
  73. }
  74. return 0;
  75. }
  76. },
  77. 'mailto:': {
  78. validate: function (text, pos, self) {
  79. var tail = text.slice(pos);
  80. if (!self.re.mailto) {
  81. self.re.mailto = new RegExp(
  82. '^' + self.re.src_email_name + '@' + self.re.src_host_strict, 'i'
  83. );
  84. }
  85. if (self.re.mailto.test(tail)) {
  86. return tail.match(self.re.mailto)[0].length;
  87. }
  88. return 0;
  89. }
  90. }
  91. };
  92. /*eslint-disable max-len*/
  93. // RE pattern for 2-character tlds (autogenerated by ./support/tlds_2char_gen.js)
  94. 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]';
  95. // DON'T try to make PRs with changes. Extend TLDs with LinkifyIt.tlds() instead
  96. var tlds_default = 'biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф'.split('|');
  97. /*eslint-enable max-len*/
  98. ////////////////////////////////////////////////////////////////////////////////
  99. function resetScanCache(self) {
  100. self.__index__ = -1;
  101. self.__text_cache__ = '';
  102. }
  103. function createValidator(re) {
  104. return function (text, pos) {
  105. var tail = text.slice(pos);
  106. if (re.test(tail)) {
  107. return tail.match(re)[0].length;
  108. }
  109. return 0;
  110. };
  111. }
  112. function createNormalizer() {
  113. return function (match, self) {
  114. self.normalize(match);
  115. };
  116. }
  117. // Schemas compiler. Build regexps.
  118. //
  119. function compile(self) {
  120. // Load & clone RE patterns.
  121. var re = self.re = require('./lib/re')(self.__opts__);
  122. // Define dynamic patterns
  123. var tlds = self.__tlds__.slice();
  124. self.onCompile();
  125. if (!self.__tlds_replaced__) {
  126. tlds.push(tlds_2ch_src_re);
  127. }
  128. tlds.push(re.src_xn);
  129. re.src_tlds = tlds.join('|');
  130. function untpl(tpl) { return tpl.replace('%TLDS%', re.src_tlds); }
  131. re.email_fuzzy = RegExp(untpl(re.tpl_email_fuzzy), 'i');
  132. re.link_fuzzy = RegExp(untpl(re.tpl_link_fuzzy), 'i');
  133. re.link_no_ip_fuzzy = RegExp(untpl(re.tpl_link_no_ip_fuzzy), 'i');
  134. re.host_fuzzy_test = RegExp(untpl(re.tpl_host_fuzzy_test), 'i');
  135. //
  136. // Compile each schema
  137. //
  138. var aliases = [];
  139. self.__compiled__ = {}; // Reset compiled data
  140. function schemaError(name, val) {
  141. throw new Error('(LinkifyIt) Invalid schema "' + name + '": ' + val);
  142. }
  143. Object.keys(self.__schemas__).forEach(function (name) {
  144. var val = self.__schemas__[name];
  145. // skip disabled methods
  146. if (val === null) { return; }
  147. var compiled = { validate: null, link: null };
  148. self.__compiled__[name] = compiled;
  149. if (isObject(val)) {
  150. if (isRegExp(val.validate)) {
  151. compiled.validate = createValidator(val.validate);
  152. } else if (isFunction(val.validate)) {
  153. compiled.validate = val.validate;
  154. } else {
  155. schemaError(name, val);
  156. }
  157. if (isFunction(val.normalize)) {
  158. compiled.normalize = val.normalize;
  159. } else if (!val.normalize) {
  160. compiled.normalize = createNormalizer();
  161. } else {
  162. schemaError(name, val);
  163. }
  164. return;
  165. }
  166. if (isString(val)) {
  167. aliases.push(name);
  168. return;
  169. }
  170. schemaError(name, val);
  171. });
  172. //
  173. // Compile postponed aliases
  174. //
  175. aliases.forEach(function (alias) {
  176. if (!self.__compiled__[self.__schemas__[alias]]) {
  177. // Silently fail on missed schemas to avoid errons on disable.
  178. // schemaError(alias, self.__schemas__[alias]);
  179. return;
  180. }
  181. self.__compiled__[alias].validate =
  182. self.__compiled__[self.__schemas__[alias]].validate;
  183. self.__compiled__[alias].normalize =
  184. self.__compiled__[self.__schemas__[alias]].normalize;
  185. });
  186. //
  187. // Fake record for guessed links
  188. //
  189. self.__compiled__[''] = { validate: null, normalize: createNormalizer() };
  190. //
  191. // Build schema condition
  192. //
  193. var slist = Object.keys(self.__compiled__)
  194. .filter(function (name) {
  195. // Filter disabled & fake schemas
  196. return name.length > 0 && self.__compiled__[name];
  197. })
  198. .map(escapeRE)
  199. .join('|');
  200. // (?!_) cause 1.5x slowdown
  201. self.re.schema_test = RegExp('(^|(?!_)(?:[><\uff5c]|' + re.src_ZPCc + '))(' + slist + ')', 'i');
  202. self.re.schema_search = RegExp('(^|(?!_)(?:[><\uff5c]|' + re.src_ZPCc + '))(' + slist + ')', 'ig');
  203. self.re.schema_at_start = RegExp('^' + self.re.schema_search.source, 'i');
  204. self.re.pretest = RegExp(
  205. '(' + self.re.schema_test.source + ')|(' + self.re.host_fuzzy_test.source + ')|@',
  206. 'i'
  207. );
  208. //
  209. // Cleanup
  210. //
  211. resetScanCache(self);
  212. }
  213. /**
  214. * class Match
  215. *
  216. * Match result. Single element of array, returned by [[LinkifyIt#match]]
  217. **/
  218. function Match(self, shift) {
  219. var start = self.__index__,
  220. end = self.__last_index__,
  221. text = self.__text_cache__.slice(start, end);
  222. /**
  223. * Match#schema -> String
  224. *
  225. * Prefix (protocol) for matched string.
  226. **/
  227. this.schema = self.__schema__.toLowerCase();
  228. /**
  229. * Match#index -> Number
  230. *
  231. * First position of matched string.
  232. **/
  233. this.index = start + shift;
  234. /**
  235. * Match#lastIndex -> Number
  236. *
  237. * Next position after matched string.
  238. **/
  239. this.lastIndex = end + shift;
  240. /**
  241. * Match#raw -> String
  242. *
  243. * Matched string.
  244. **/
  245. this.raw = text;
  246. /**
  247. * Match#text -> String
  248. *
  249. * Notmalized text of matched string.
  250. **/
  251. this.text = text;
  252. /**
  253. * Match#url -> String
  254. *
  255. * Normalized url of matched string.
  256. **/
  257. this.url = text;
  258. }
  259. function createMatch(self, shift) {
  260. var match = new Match(self, shift);
  261. self.__compiled__[match.schema].normalize(match, self);
  262. return match;
  263. }
  264. /**
  265. * class LinkifyIt
  266. **/
  267. /**
  268. * new LinkifyIt(schemas, options)
  269. * - schemas (Object): Optional. Additional schemas to validate (prefix/validator)
  270. * - options (Object): { fuzzyLink|fuzzyEmail|fuzzyIP: true|false }
  271. *
  272. * Creates new linkifier instance with optional additional schemas.
  273. * Can be called without `new` keyword for convenience.
  274. *
  275. * By default understands:
  276. *
  277. * - `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links
  278. * - "fuzzy" links and emails (example.com, foo@bar.com).
  279. *
  280. * `schemas` is an object, where each key/value describes protocol/rule:
  281. *
  282. * - __key__ - link prefix (usually, protocol name with `:` at the end, `skype:`
  283. * for example). `linkify-it` makes shure that prefix is not preceeded with
  284. * alphanumeric char and symbols. Only whitespaces and punctuation allowed.
  285. * - __value__ - rule to check tail after link prefix
  286. * - _String_ - just alias to existing rule
  287. * - _Object_
  288. * - _validate_ - validator function (should return matched length on success),
  289. * or `RegExp`.
  290. * - _normalize_ - optional function to normalize text & url of matched result
  291. * (for example, for @twitter mentions).
  292. *
  293. * `options`:
  294. *
  295. * - __fuzzyLink__ - recognige URL-s without `http(s):` prefix. Default `true`.
  296. * - __fuzzyIP__ - allow IPs in fuzzy links above. Can conflict with some texts
  297. * like version numbers. Default `false`.
  298. * - __fuzzyEmail__ - recognize emails without `mailto:` prefix.
  299. *
  300. **/
  301. function LinkifyIt(schemas, options) {
  302. if (!(this instanceof LinkifyIt)) {
  303. return new LinkifyIt(schemas, options);
  304. }
  305. if (!options) {
  306. if (isOptionsObj(schemas)) {
  307. options = schemas;
  308. schemas = {};
  309. }
  310. }
  311. this.__opts__ = assign({}, defaultOptions, options);
  312. // Cache last tested result. Used to skip repeating steps on next `match` call.
  313. this.__index__ = -1;
  314. this.__last_index__ = -1; // Next scan position
  315. this.__schema__ = '';
  316. this.__text_cache__ = '';
  317. this.__schemas__ = assign({}, defaultSchemas, schemas);
  318. this.__compiled__ = {};
  319. this.__tlds__ = tlds_default;
  320. this.__tlds_replaced__ = false;
  321. this.re = {};
  322. compile(this);
  323. }
  324. /** chainable
  325. * LinkifyIt#add(schema, definition)
  326. * - schema (String): rule name (fixed pattern prefix)
  327. * - definition (String|RegExp|Object): schema definition
  328. *
  329. * Add new rule definition. See constructor description for details.
  330. **/
  331. LinkifyIt.prototype.add = function add(schema, definition) {
  332. this.__schemas__[schema] = definition;
  333. compile(this);
  334. return this;
  335. };
  336. /** chainable
  337. * LinkifyIt#set(options)
  338. * - options (Object): { fuzzyLink|fuzzyEmail|fuzzyIP: true|false }
  339. *
  340. * Set recognition options for links without schema.
  341. **/
  342. LinkifyIt.prototype.set = function set(options) {
  343. this.__opts__ = assign(this.__opts__, options);
  344. return this;
  345. };
  346. /**
  347. * LinkifyIt#test(text) -> Boolean
  348. *
  349. * Searches linkifiable pattern and returns `true` on success or `false` on fail.
  350. **/
  351. LinkifyIt.prototype.test = function test(text) {
  352. // Reset scan cache
  353. this.__text_cache__ = text;
  354. this.__index__ = -1;
  355. if (!text.length) { return false; }
  356. var m, ml, me, len, shift, next, re, tld_pos, at_pos;
  357. // try to scan for link with schema - that's the most simple rule
  358. if (this.re.schema_test.test(text)) {
  359. re = this.re.schema_search;
  360. re.lastIndex = 0;
  361. while ((m = re.exec(text)) !== null) {
  362. len = this.testSchemaAt(text, m[2], re.lastIndex);
  363. if (len) {
  364. this.__schema__ = m[2];
  365. this.__index__ = m.index + m[1].length;
  366. this.__last_index__ = m.index + m[0].length + len;
  367. break;
  368. }
  369. }
  370. }
  371. if (this.__opts__.fuzzyLink && this.__compiled__['http:']) {
  372. // guess schemaless links
  373. tld_pos = text.search(this.re.host_fuzzy_test);
  374. if (tld_pos >= 0) {
  375. // if tld is located after found link - no need to check fuzzy pattern
  376. if (this.__index__ < 0 || tld_pos < this.__index__) {
  377. if ((ml = text.match(this.__opts__.fuzzyIP ? this.re.link_fuzzy : this.re.link_no_ip_fuzzy)) !== null) {
  378. shift = ml.index + ml[1].length;
  379. if (this.__index__ < 0 || shift < this.__index__) {
  380. this.__schema__ = '';
  381. this.__index__ = shift;
  382. this.__last_index__ = ml.index + ml[0].length;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. if (this.__opts__.fuzzyEmail && this.__compiled__['mailto:']) {
  389. // guess schemaless emails
  390. at_pos = text.indexOf('@');
  391. if (at_pos >= 0) {
  392. // We can't skip this check, because this cases are possible:
  393. // 192.168.1.1@gmail.com, my.in@example.com
  394. if ((me = text.match(this.re.email_fuzzy)) !== null) {
  395. shift = me.index + me[1].length;
  396. next = me.index + me[0].length;
  397. if (this.__index__ < 0 || shift < this.__index__ ||
  398. (shift === this.__index__ && next > this.__last_index__)) {
  399. this.__schema__ = 'mailto:';
  400. this.__index__ = shift;
  401. this.__last_index__ = next;
  402. }
  403. }
  404. }
  405. }
  406. return this.__index__ >= 0;
  407. };
  408. /**
  409. * LinkifyIt#pretest(text) -> Boolean
  410. *
  411. * Very quick check, that can give false positives. Returns true if link MAY BE
  412. * can exists. Can be used for speed optimization, when you need to check that
  413. * link NOT exists.
  414. **/
  415. LinkifyIt.prototype.pretest = function pretest(text) {
  416. return this.re.pretest.test(text);
  417. };
  418. /**
  419. * LinkifyIt#testSchemaAt(text, name, position) -> Number
  420. * - text (String): text to scan
  421. * - name (String): rule (schema) name
  422. * - position (Number): text offset to check from
  423. *
  424. * Similar to [[LinkifyIt#test]] but checks only specific protocol tail exactly
  425. * at given position. Returns length of found pattern (0 on fail).
  426. **/
  427. LinkifyIt.prototype.testSchemaAt = function testSchemaAt(text, schema, pos) {
  428. // If not supported schema check requested - terminate
  429. if (!this.__compiled__[schema.toLowerCase()]) {
  430. return 0;
  431. }
  432. return this.__compiled__[schema.toLowerCase()].validate(text, pos, this);
  433. };
  434. /**
  435. * LinkifyIt#match(text) -> Array|null
  436. *
  437. * Returns array of found link descriptions or `null` on fail. We strongly
  438. * recommend to use [[LinkifyIt#test]] first, for best speed.
  439. *
  440. * ##### Result match description
  441. *
  442. * - __schema__ - link schema, can be empty for fuzzy links, or `//` for
  443. * protocol-neutral links.
  444. * - __index__ - offset of matched text
  445. * - __lastIndex__ - index of next char after mathch end
  446. * - __raw__ - matched text
  447. * - __text__ - normalized text
  448. * - __url__ - link, generated from matched text
  449. **/
  450. LinkifyIt.prototype.match = function match(text) {
  451. var shift = 0, result = [];
  452. // Try to take previous element from cache, if .test() called before
  453. if (this.__index__ >= 0 && this.__text_cache__ === text) {
  454. result.push(createMatch(this, shift));
  455. shift = this.__last_index__;
  456. }
  457. // Cut head if cache was used
  458. var tail = shift ? text.slice(shift) : text;
  459. // Scan string until end reached
  460. while (this.test(tail)) {
  461. result.push(createMatch(this, shift));
  462. tail = tail.slice(this.__last_index__);
  463. shift += this.__last_index__;
  464. }
  465. if (result.length) {
  466. return result;
  467. }
  468. return null;
  469. };
  470. /**
  471. * LinkifyIt#matchAtStart(text) -> Match|null
  472. *
  473. * Returns fully-formed (not fuzzy) link if it starts at the beginning
  474. * of the string, and null otherwise.
  475. **/
  476. LinkifyIt.prototype.matchAtStart = function matchAtStart(text) {
  477. // Reset scan cache
  478. this.__text_cache__ = text;
  479. this.__index__ = -1;
  480. if (!text.length) return null;
  481. var m = this.re.schema_at_start.exec(text);
  482. if (!m) return null;
  483. var len = this.testSchemaAt(text, m[2], m[0].length);
  484. if (!len) return null;
  485. this.__schema__ = m[2];
  486. this.__index__ = m.index + m[1].length;
  487. this.__last_index__ = m.index + m[0].length + len;
  488. return createMatch(this, 0);
  489. };
  490. /** chainable
  491. * LinkifyIt#tlds(list [, keepOld]) -> this
  492. * - list (Array): list of tlds
  493. * - keepOld (Boolean): merge with current list if `true` (`false` by default)
  494. *
  495. * Load (or merge) new tlds list. Those are user for fuzzy links (without prefix)
  496. * to avoid false positives. By default this algorythm used:
  497. *
  498. * - hostname with any 2-letter root zones are ok.
  499. * - biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф
  500. * are ok.
  501. * - encoded (`xn--...`) root zones are ok.
  502. *
  503. * If list is replaced, then exact match for 2-chars root zones will be checked.
  504. **/
  505. LinkifyIt.prototype.tlds = function tlds(list, keepOld) {
  506. list = Array.isArray(list) ? list : [ list ];
  507. if (!keepOld) {
  508. this.__tlds__ = list.slice();
  509. this.__tlds_replaced__ = true;
  510. compile(this);
  511. return this;
  512. }
  513. this.__tlds__ = this.__tlds__.concat(list)
  514. .sort()
  515. .filter(function (el, idx, arr) {
  516. return el !== arr[idx - 1];
  517. })
  518. .reverse();
  519. compile(this);
  520. return this;
  521. };
  522. /**
  523. * LinkifyIt#normalize(match)
  524. *
  525. * Default normalizer (if schema does not define it's own).
  526. **/
  527. LinkifyIt.prototype.normalize = function normalize(match) {
  528. // Do minimal possible changes by default. Need to collect feedback prior
  529. // to move forward https://github.com/markdown-it/linkify-it/issues/1
  530. if (!match.schema) { match.url = 'http://' + match.url; }
  531. if (match.schema === 'mailto:' && !/^mailto:/i.test(match.url)) {
  532. match.url = 'mailto:' + match.url;
  533. }
  534. };
  535. /**
  536. * LinkifyIt#onCompile()
  537. *
  538. * Override to modify basic RegExp-s.
  539. **/
  540. LinkifyIt.prototype.onCompile = function onCompile() {
  541. };
  542. module.exports = LinkifyIt;