index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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.pretest = RegExp(
  204. '(' + self.re.schema_test.source + ')|(' + self.re.host_fuzzy_test.source + ')|@',
  205. 'i'
  206. );
  207. //
  208. // Cleanup
  209. //
  210. resetScanCache(self);
  211. }
  212. /**
  213. * class Match
  214. *
  215. * Match result. Single element of array, returned by [[LinkifyIt#match]]
  216. **/
  217. function Match(self, shift) {
  218. var start = self.__index__,
  219. end = self.__last_index__,
  220. text = self.__text_cache__.slice(start, end);
  221. /**
  222. * Match#schema -> String
  223. *
  224. * Prefix (protocol) for matched string.
  225. **/
  226. this.schema = self.__schema__.toLowerCase();
  227. /**
  228. * Match#index -> Number
  229. *
  230. * First position of matched string.
  231. **/
  232. this.index = start + shift;
  233. /**
  234. * Match#lastIndex -> Number
  235. *
  236. * Next position after matched string.
  237. **/
  238. this.lastIndex = end + shift;
  239. /**
  240. * Match#raw -> String
  241. *
  242. * Matched string.
  243. **/
  244. this.raw = text;
  245. /**
  246. * Match#text -> String
  247. *
  248. * Notmalized text of matched string.
  249. **/
  250. this.text = text;
  251. /**
  252. * Match#url -> String
  253. *
  254. * Normalized url of matched string.
  255. **/
  256. this.url = text;
  257. }
  258. function createMatch(self, shift) {
  259. var match = new Match(self, shift);
  260. self.__compiled__[match.schema].normalize(match, self);
  261. return match;
  262. }
  263. /**
  264. * class LinkifyIt
  265. **/
  266. /**
  267. * new LinkifyIt(schemas, options)
  268. * - schemas (Object): Optional. Additional schemas to validate (prefix/validator)
  269. * - options (Object): { fuzzyLink|fuzzyEmail|fuzzyIP: true|false }
  270. *
  271. * Creates new linkifier instance with optional additional schemas.
  272. * Can be called without `new` keyword for convenience.
  273. *
  274. * By default understands:
  275. *
  276. * - `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links
  277. * - "fuzzy" links and emails (example.com, foo@bar.com).
  278. *
  279. * `schemas` is an object, where each key/value describes protocol/rule:
  280. *
  281. * - __key__ - link prefix (usually, protocol name with `:` at the end, `skype:`
  282. * for example). `linkify-it` makes shure that prefix is not preceeded with
  283. * alphanumeric char and symbols. Only whitespaces and punctuation allowed.
  284. * - __value__ - rule to check tail after link prefix
  285. * - _String_ - just alias to existing rule
  286. * - _Object_
  287. * - _validate_ - validator function (should return matched length on success),
  288. * or `RegExp`.
  289. * - _normalize_ - optional function to normalize text & url of matched result
  290. * (for example, for @twitter mentions).
  291. *
  292. * `options`:
  293. *
  294. * - __fuzzyLink__ - recognige URL-s without `http(s):` prefix. Default `true`.
  295. * - __fuzzyIP__ - allow IPs in fuzzy links above. Can conflict with some texts
  296. * like version numbers. Default `false`.
  297. * - __fuzzyEmail__ - recognize emails without `mailto:` prefix.
  298. *
  299. **/
  300. function LinkifyIt(schemas, options) {
  301. if (!(this instanceof LinkifyIt)) {
  302. return new LinkifyIt(schemas, options);
  303. }
  304. if (!options) {
  305. if (isOptionsObj(schemas)) {
  306. options = schemas;
  307. schemas = {};
  308. }
  309. }
  310. this.__opts__ = assign({}, defaultOptions, options);
  311. // Cache last tested result. Used to skip repeating steps on next `match` call.
  312. this.__index__ = -1;
  313. this.__last_index__ = -1; // Next scan position
  314. this.__schema__ = '';
  315. this.__text_cache__ = '';
  316. this.__schemas__ = assign({}, defaultSchemas, schemas);
  317. this.__compiled__ = {};
  318. this.__tlds__ = tlds_default;
  319. this.__tlds_replaced__ = false;
  320. this.re = {};
  321. compile(this);
  322. }
  323. /** chainable
  324. * LinkifyIt#add(schema, definition)
  325. * - schema (String): rule name (fixed pattern prefix)
  326. * - definition (String|RegExp|Object): schema definition
  327. *
  328. * Add new rule definition. See constructor description for details.
  329. **/
  330. LinkifyIt.prototype.add = function add(schema, definition) {
  331. this.__schemas__[schema] = definition;
  332. compile(this);
  333. return this;
  334. };
  335. /** chainable
  336. * LinkifyIt#set(options)
  337. * - options (Object): { fuzzyLink|fuzzyEmail|fuzzyIP: true|false }
  338. *
  339. * Set recognition options for links without schema.
  340. **/
  341. LinkifyIt.prototype.set = function set(options) {
  342. this.__opts__ = assign(this.__opts__, options);
  343. return this;
  344. };
  345. /**
  346. * LinkifyIt#test(text) -> Boolean
  347. *
  348. * Searches linkifiable pattern and returns `true` on success or `false` on fail.
  349. **/
  350. LinkifyIt.prototype.test = function test(text) {
  351. // Reset scan cache
  352. this.__text_cache__ = text;
  353. this.__index__ = -1;
  354. if (!text.length) { return false; }
  355. var m, ml, me, len, shift, next, re, tld_pos, at_pos;
  356. // try to scan for link with schema - that's the most simple rule
  357. if (this.re.schema_test.test(text)) {
  358. re = this.re.schema_search;
  359. re.lastIndex = 0;
  360. while ((m = re.exec(text)) !== null) {
  361. len = this.testSchemaAt(text, m[2], re.lastIndex);
  362. if (len) {
  363. this.__schema__ = m[2];
  364. this.__index__ = m.index + m[1].length;
  365. this.__last_index__ = m.index + m[0].length + len;
  366. break;
  367. }
  368. }
  369. }
  370. if (this.__opts__.fuzzyLink && this.__compiled__['http:']) {
  371. // guess schemaless links
  372. tld_pos = text.search(this.re.host_fuzzy_test);
  373. if (tld_pos >= 0) {
  374. // if tld is located after found link - no need to check fuzzy pattern
  375. if (this.__index__ < 0 || tld_pos < this.__index__) {
  376. if ((ml = text.match(this.__opts__.fuzzyIP ? this.re.link_fuzzy : this.re.link_no_ip_fuzzy)) !== null) {
  377. shift = ml.index + ml[1].length;
  378. if (this.__index__ < 0 || shift < this.__index__) {
  379. this.__schema__ = '';
  380. this.__index__ = shift;
  381. this.__last_index__ = ml.index + ml[0].length;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. if (this.__opts__.fuzzyEmail && this.__compiled__['mailto:']) {
  388. // guess schemaless emails
  389. at_pos = text.indexOf('@');
  390. if (at_pos >= 0) {
  391. // We can't skip this check, because this cases are possible:
  392. // 192.168.1.1@gmail.com, my.in@example.com
  393. if ((me = text.match(this.re.email_fuzzy)) !== null) {
  394. shift = me.index + me[1].length;
  395. next = me.index + me[0].length;
  396. if (this.__index__ < 0 || shift < this.__index__ ||
  397. (shift === this.__index__ && next > this.__last_index__)) {
  398. this.__schema__ = 'mailto:';
  399. this.__index__ = shift;
  400. this.__last_index__ = next;
  401. }
  402. }
  403. }
  404. }
  405. return this.__index__ >= 0;
  406. };
  407. /**
  408. * LinkifyIt#pretest(text) -> Boolean
  409. *
  410. * Very quick check, that can give false positives. Returns true if link MAY BE
  411. * can exists. Can be used for speed optimization, when you need to check that
  412. * link NOT exists.
  413. **/
  414. LinkifyIt.prototype.pretest = function pretest(text) {
  415. return this.re.pretest.test(text);
  416. };
  417. /**
  418. * LinkifyIt#testSchemaAt(text, name, position) -> Number
  419. * - text (String): text to scan
  420. * - name (String): rule (schema) name
  421. * - position (Number): text offset to check from
  422. *
  423. * Similar to [[LinkifyIt#test]] but checks only specific protocol tail exactly
  424. * at given position. Returns length of found pattern (0 on fail).
  425. **/
  426. LinkifyIt.prototype.testSchemaAt = function testSchemaAt(text, schema, pos) {
  427. // If not supported schema check requested - terminate
  428. if (!this.__compiled__[schema.toLowerCase()]) {
  429. return 0;
  430. }
  431. return this.__compiled__[schema.toLowerCase()].validate(text, pos, this);
  432. };
  433. /**
  434. * LinkifyIt#match(text) -> Array|null
  435. *
  436. * Returns array of found link descriptions or `null` on fail. We strongly
  437. * recommend to use [[LinkifyIt#test]] first, for best speed.
  438. *
  439. * ##### Result match description
  440. *
  441. * - __schema__ - link schema, can be empty for fuzzy links, or `//` for
  442. * protocol-neutral links.
  443. * - __index__ - offset of matched text
  444. * - __lastIndex__ - index of next char after mathch end
  445. * - __raw__ - matched text
  446. * - __text__ - normalized text
  447. * - __url__ - link, generated from matched text
  448. **/
  449. LinkifyIt.prototype.match = function match(text) {
  450. var shift = 0, result = [];
  451. // Try to take previous element from cache, if .test() called before
  452. if (this.__index__ >= 0 && this.__text_cache__ === text) {
  453. result.push(createMatch(this, shift));
  454. shift = this.__last_index__;
  455. }
  456. // Cut head if cache was used
  457. var tail = shift ? text.slice(shift) : text;
  458. // Scan string until end reached
  459. while (this.test(tail)) {
  460. result.push(createMatch(this, shift));
  461. tail = tail.slice(this.__last_index__);
  462. shift += this.__last_index__;
  463. }
  464. if (result.length) {
  465. return result;
  466. }
  467. return null;
  468. };
  469. /** chainable
  470. * LinkifyIt#tlds(list [, keepOld]) -> this
  471. * - list (Array): list of tlds
  472. * - keepOld (Boolean): merge with current list if `true` (`false` by default)
  473. *
  474. * Load (or merge) new tlds list. Those are user for fuzzy links (without prefix)
  475. * to avoid false positives. By default this algorythm used:
  476. *
  477. * - hostname with any 2-letter root zones are ok.
  478. * - biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф
  479. * are ok.
  480. * - encoded (`xn--...`) root zones are ok.
  481. *
  482. * If list is replaced, then exact match for 2-chars root zones will be checked.
  483. **/
  484. LinkifyIt.prototype.tlds = function tlds(list, keepOld) {
  485. list = Array.isArray(list) ? list : [ list ];
  486. if (!keepOld) {
  487. this.__tlds__ = list.slice();
  488. this.__tlds_replaced__ = true;
  489. compile(this);
  490. return this;
  491. }
  492. this.__tlds__ = this.__tlds__.concat(list)
  493. .sort()
  494. .filter(function (el, idx, arr) {
  495. return el !== arr[idx - 1];
  496. })
  497. .reverse();
  498. compile(this);
  499. return this;
  500. };
  501. /**
  502. * LinkifyIt#normalize(match)
  503. *
  504. * Default normalizer (if schema does not define it's own).
  505. **/
  506. LinkifyIt.prototype.normalize = function normalize(match) {
  507. // Do minimal possible changes by default. Need to collect feedback prior
  508. // to move forward https://github.com/markdown-it/linkify-it/issues/1
  509. if (!match.schema) { match.url = 'http://' + match.url; }
  510. if (match.schema === 'mailto:' && !/^mailto:/i.test(match.url)) {
  511. match.url = 'mailto:' + match.url;
  512. }
  513. };
  514. /**
  515. * LinkifyIt#onCompile()
  516. *
  517. * Override to modify basic RegExp-s.
  518. **/
  519. LinkifyIt.prototype.onCompile = function onCompile() {
  520. };
  521. module.exports = LinkifyIt;