版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. linkify-it
  2. ==========
  3. [![CI](https://github.com/markdown-it/linkify-it/actions/workflows/ci.yml/badge.svg)](https://github.com/markdown-it/linkify-it/actions/workflows/ci.yml)
  4. [![NPM version](https://img.shields.io/npm/v/linkify-it.svg?style=flat)](https://www.npmjs.org/package/linkify-it)
  5. [![Coverage Status](https://img.shields.io/coveralls/markdown-it/linkify-it/master.svg?style=flat)](https://coveralls.io/r/markdown-it/linkify-it?branch=master)
  6. [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/markdown-it/linkify-it)
  7. > Links recognition library with FULL unicode support.
  8. > Focused on high quality link patterns detection in plain text.
  9. __[Demo](http://markdown-it.github.io/linkify-it/)__
  10. Why it's awesome:
  11. - Full unicode support, _with astral characters_!
  12. - International domains support.
  13. - Allows rules extension & custom normalizers.
  14. Install
  15. -------
  16. ```bash
  17. npm install linkify-it --save
  18. ```
  19. Browserification is also supported.
  20. Usage examples
  21. --------------
  22. ##### Example 1
  23. ```js
  24. var linkify = require('linkify-it')();
  25. // Reload full tlds list & add unofficial `.onion` domain.
  26. linkify
  27. .tlds(require('tlds')) // Reload with full tlds list
  28. .tlds('onion', true) // Add unofficial `.onion` domain
  29. .add('git:', 'http:') // Add `git:` protocol as "alias"
  30. .add('ftp:', null) // Disable `ftp:` protocol
  31. .set({ fuzzyIP: true }); // Enable IPs in fuzzy links (without schema)
  32. console.log(linkify.test('Site github.com!')); // true
  33. console.log(linkify.match('Site github.com!')); // [ {
  34. // schema: "",
  35. // index: 5,
  36. // lastIndex: 15,
  37. // raw: "github.com",
  38. // text: "github.com",
  39. // url: "http://github.com",
  40. // } ]
  41. ```
  42. ##### Example 2. Add twitter mentions handler
  43. ```js
  44. linkify.add('@', {
  45. validate: function (text, pos, self) {
  46. var tail = text.slice(pos);
  47. if (!self.re.twitter) {
  48. self.re.twitter = new RegExp(
  49. '^([a-zA-Z0-9_]){1,15}(?!_)(?=$|' + self.re.src_ZPCc + ')'
  50. );
  51. }
  52. if (self.re.twitter.test(tail)) {
  53. // Linkifier allows punctuation chars before prefix,
  54. // but we additionally disable `@` ("@@mention" is invalid)
  55. if (pos >= 2 && tail[pos - 2] === '@') {
  56. return false;
  57. }
  58. return tail.match(self.re.twitter)[0].length;
  59. }
  60. return 0;
  61. },
  62. normalize: function (match) {
  63. match.url = 'https://twitter.com/' + match.url.replace(/^@/, '');
  64. }
  65. });
  66. ```
  67. API
  68. ---
  69. __[API documentation](http://markdown-it.github.io/linkify-it/doc)__
  70. ### new LinkifyIt(schemas, options)
  71. Creates new linkifier instance with optional additional schemas.
  72. Can be called without `new` keyword for convenience.
  73. By default understands:
  74. - `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links
  75. - "fuzzy" links and emails (google.com, foo@bar.com).
  76. `schemas` is an object, where each key/value describes protocol/rule:
  77. - __key__ - link prefix (usually, protocol name with `:` at the end, `skype:`
  78. for example). `linkify-it` makes sure that prefix is not preceded with
  79. alphanumeric char.
  80. - __value__ - rule to check tail after link prefix
  81. - _String_ - just alias to existing rule
  82. - _Object_
  83. - _validate_ - either a `RegExp` (start with `^`, and don't include the
  84. link prefix itself), or a validator function which, given arguments
  85. _text_, _pos_, and _self_, returns the length of a match in _text_
  86. starting at index _pos_. _pos_ is the index right after the link prefix.
  87. _self_ can be used to access the linkify object to cache data.
  88. - _normalize_ - optional function to normalize text & url of matched result
  89. (for example, for twitter mentions).
  90. `options`:
  91. - __fuzzyLink__ - recognize URL-s without `http(s)://` head. Default `true`.
  92. - __fuzzyIP__ - allow IPs in fuzzy links above. Can conflict with some texts
  93. like version numbers. Default `false`.
  94. - __fuzzyEmail__ - recognize emails without `mailto:` prefix. Default `true`.
  95. - __---__ - set `true` to terminate link with `---` (if it's considered as long dash).
  96. ### .test(text)
  97. Searches linkifiable pattern and returns `true` on success or `false` on fail.
  98. ### .pretest(text)
  99. Quick check if link MAY BE can exist. Can be used to optimize more expensive
  100. `.test()` calls. Return `false` if link can not be found, `true` - if `.test()`
  101. call needed to know exactly.
  102. ### .testSchemaAt(text, name, offset)
  103. Similar to `.test()` but checks only specific protocol tail exactly at given
  104. position. Returns length of found pattern (0 on fail).
  105. ### .match(text)
  106. Returns `Array` of found link matches or null if nothing found.
  107. Each match has:
  108. - __schema__ - link schema, can be empty for fuzzy links, or `//` for
  109. protocol-neutral links.
  110. - __index__ - offset of matched text
  111. - __lastIndex__ - index of next char after mathch end
  112. - __raw__ - matched text
  113. - __text__ - normalized text
  114. - __url__ - link, generated from matched text
  115. ### .matchAtStart(text)
  116. Checks if a match exists at the start of the string. Returns `Match`
  117. (see docs for `match(text)`) or null if no URL is at the start.
  118. Doesn't work with fuzzy links.
  119. ### .tlds(list[, keepOld])
  120. Load (or merge) new tlds list. Those are needed for fuzzy links (without schema)
  121. to avoid false positives. By default:
  122. - 2-letter root zones are ok.
  123. - biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф are ok.
  124. - encoded (`xn--...`) root zones are ok.
  125. If that's not enough, you can reload defaults with more detailed zones list.
  126. ### .add(key, value)
  127. Add a new schema to the schemas object. As described in the constructor
  128. definition, `key` is a link prefix (`skype:`, for example), and `value`
  129. is a String to alias to another schema, or an Object with `validate` and
  130. optionally `normalize` definitions. To disable an existing rule, use
  131. `.add(key, null)`.
  132. ### .set(options)
  133. Override default options. Missed properties will not be changed.
  134. ## License
  135. [MIT](https://github.com/markdown-it/linkify-it/blob/master/LICENSE)