{"id":22308771,"url":"https://github.com/ratson/linkize","last_synced_at":"2025-03-26T01:28:55.976Z","repository":{"id":143886308,"uuid":"87565680","full_name":"ratson/linkize","owner":"ratson","description":null,"archived":false,"fork":false,"pushed_at":"2017-04-12T14:59:28.000Z","size":3241,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-30T22:51:28.640Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ratson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-07T16:26:27.000Z","updated_at":"2017-04-07T16:28:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"ff52bbd7-7d0e-425d-920e-2486ded5f58c","html_url":"https://github.com/ratson/linkize","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratson%2Flinkize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratson%2Flinkize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratson%2Flinkize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratson%2Flinkize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ratson","download_url":"https://codeload.github.com/ratson/linkize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245571267,"owners_count":20637312,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-03T20:15:14.690Z","updated_at":"2025-03-26T01:28:55.969Z","avatar_url":"https://github.com/ratson.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# linkize\n\n## This is a fork\n\nDiffernce for upstream\n\n- Allow customize phone RegExp\n\n## Installation\n\n```shell\nnpm install linkize --save\n```\n\n# Autolinker.js\n\nBecause I had so much trouble finding a good auto-linking implementation out in\nthe wild, I decided to roll my own. It  seemed that everything I found out there\nwas either an implementation that didn't cover every case, or was just limited\nin one way or another.\n\nSo, this utility attempts to handle everything. It:\n\n- Autolinks URLs, whether or not they start with the protocol (i.e. 'http://').\n  In other words, it will automatically link the text \"google.com\", as well as\n  \"http://google.com\".\n- Will properly handle URLs with special characters\n- Will properly handle URLs with query parameters or a named anchor (i.e. hash)\n- Will autolink email addresses.\n- Will autolink phone numbers.\n- Will autolink mentions (Twitter, Instagram).\n- Will autolink hashtags.\n- Will properly handle HTML input. The utility will not change the `href`\n  attribute inside anchor (\u0026lt;a\u0026gt;) tags (or any other tag/attribute),\n  and will not accidentally wrap the inner text of an anchor tag with a\n  new one (which would cause doubly-nested anchor tags).\n\nHope that this utility helps you as well!\n\nFull API Docs: [http://gregjacobs.github.io/Autolinker.js/api/](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker)\u003cbr\u003e\nLive Example: [http://gregjacobs.github.io/Autolinker.js/examples/live-example/](http://gregjacobs.github.io/Autolinker.js/examples/live-example/)\n\n\n## v1.0 released. Breaking Changes from 0.x -\u003e 1.x\n\n1. `twitter` option removed, replaced with `mention` (which accepts 'twitter'\n   and 'instagram' values)\n2. Matching mentions (previously the `twitter` option) now defaults to\n   being turned off. Previously, Twitter handle matching was on by\n   default.\n3. `replaceFn` option now called with just one argument: the `Match`\n   object (previously was called with two arguments: `autolinker` and\n   `match`)\n4. (Used inside the `replaceFn`) `TwitterMatch` replaced with\n   `MentionMatch`, and `MentionMatch.getType()` now returns `'mention'`\n   instead of `'twitter'`\n5. (Used inside the `replaceFn`) `TwitterMatch.getTwitterHandle()` -\u003e\n   `MentionMatch.getMention()`\n\n\n## Installation\n\n#### Download\n\nSimply clone or download the zip of the project, and link to either\n`dist/Autolinker.js` or `dist/Autolinker.min.js` with a script tag:\n\n```html\n\u003cscript src=\"path/to/Autolinker.min.js\"\u003e\u003c/script\u003e\n```\n\n#### Using with the [Bower](http://bower.io) package manager:\n\nCommand line:\n\n```shell\nbower install Autolinker.js --save\n```\n\n#### Using with [Node.js](http://nodejs.org) via [npm](https://www.npmjs.org/):\n\nCommand Line:\n\n```shell\nnpm install autolinker --save\n```\n\nJavaScript:\n\n```javascript\nvar Autolinker = require( 'autolinker' );\n// note: npm wants an all-lowercase package name, but the utility is a class and\n// should be aliased with a capital letter\n```\n\n\n## Usage\n\nUsing the static [link()](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-static-method-link)\nmethod:\n\n```javascript\nvar linkedText = Autolinker.link( textToAutolink[, options] );\n```\n\nUsing as a class:\n\n```javascript\nvar autolinker = new Autolinker( [ options ] );\n\nvar linkedText = autolinker.link( textToAutoLink );\n```\n\nNote: if using the same options to autolink multiple pieces of html/text, it is\nslightly more efficient to create a single Autolinker instance, and run the\n[link()](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-method-link)\nmethod repeatedly (i.e. use the \"class\" form above).\n\n\n#### Example:\n\n```javascript\nvar linkedText = Autolinker.link( \"Check out google.com\", { className: \"myLink\" } );\n// Produces: \"Check out \u003ca class=\"myLink myLink-url\" href=\"http://google.com\" target=\"_blank\"\u003egoogle.com\u003c/a\u003e\"\n```\n\n## Options\n\nThese are the options which may be specified for linking. These are specified by\nproviding an Object as the second parameter to [Autolinker.link()](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-static-method-link). These include:\n\n- [newWindow](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-newWindow) : Boolean\u003cbr /\u003e\n  `true` to have the links should open in a new window when clicked, `false`\n  otherwise. Defaults to `true`.\u003cbr /\u003e\u003cbr /\u003e\n- [urls](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-urls) : Boolean/Object\u003cbr /\u003e\n  `true` to have URLs auto-linked, `false` to skip auto-linking of URLs.\n  Defaults to `true`.\u003cbr\u003e\n\n  This option also accepts an Object form with 3 properties to allow for\n  more customization of what exactly gets linked. All default to `true`:\n\n    - schemeMatches (Boolean): `true` to match URLs found prefixed with a scheme,\n      i.e. `http://google.com`, or `other+scheme://google.com`, `false` to\n      prevent these types of matches.\n    - wwwMatches (Boolean): `true` to match urls found prefixed with `'www.'`,\n      i.e. `www.google.com`. `false` to prevent these types of matches. Note\n      that if the URL had a prefixed scheme, and `schemeMatches` is true, it\n      will still be linked.\n    - tldMatches: `true` to match URLs with known top level domains (.com, .net,\n      etc.) that are not prefixed with a scheme or `'www.'`. Ex: `google.com`,\n      `asdf.org/?page=1`, etc. `false` to prevent these types of matches.\n      \u003cbr /\u003e\n\n  Example usage: `urls: { schemeMatches: true, wwwMatches: true, tldMatches: false }`\n\n- [email](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-email) : Boolean\u003cbr /\u003e\n  `true` to have email addresses auto-linked, `false` to skip auto-linking of\n  email addresses. Defaults to `true`.\u003cbr /\u003e\u003cbr /\u003e\n- [phone](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-phone) : Boolean\u003cbr /\u003e\n  `true` to have phone numbers auto-linked, `false` to skip auto-linking of\n  phone numbers. Defaults to `true`.\u003cbr /\u003e\u003cbr /\u003e\n- [mention](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-mention) : String\u003cbr /\u003e\n  A string for the service name to have mentions (@username) auto-linked to. Supported\n  values at this time are 'twitter', and 'instagram'. Pass `false` to skip\n  auto-linking of mentions. Defaults to `false`.\u003cbr /\u003e\u003cbr /\u003e\n- [hashtag](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-hashtag) : Boolean/String\u003cbr /\u003e\n  A string for the service name to have hashtags auto-linked to. Supported\n  values at this time are 'twitter', 'facebook' and 'instagram'. Pass `false` to skip\n  auto-linking of hashtags. Defaults to `false`.\u003cbr /\u003e\u003cbr /\u003e\n- [stripPrefix](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-stripPrefix) : Boolean\u003cbr /\u003e\n  `true` to have the `'http://'` (or `'https://'`) and/or the `'www.'`\n  stripped from the beginning of displayed links, `false` otherwise.\n  Defaults to `true`.\u003cbr /\u003e\n\n  This option also accepts an Object form with 2 properties to allow for\n  more customization of what exactly is prevented from being displayed.\n  Both default to `true`:\n\n    - scheme (Boolean): `true` to prevent the scheme part of a URL match\n      from being displayed to the user. Example: `'http://google.com'`\n      will be displayed as `'google.com'`. `false` to not strip the\n      scheme. NOTE: Only an `'http://'` or `'https://'` scheme will be\n      removed, so as not to remove a potentially dangerous scheme (such\n      as `'file://'` or `'javascript:'`).\n    - www (Boolean): `true` to prevent the `'www.'` part of a URL match\n      from being displayed to the user. Ex: `'www.google.com'` will be\n      displayed as `'google.com'`. `false` to not strip the `'www'`.\n\n  \u003cbr /\u003e\u003cbr /\u003e\n- [stripTrailingSlash](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-stripTrailingSlash) : Boolean\u003cbr /\u003e\n  `true` to remove the trailing slash from URL matches, `false` to keep\n  the trailing slash. Example when `true`: `http://google.com/` will be\n  displayed as `http://google.com`. Defaults to `true`.\n- [truncate](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-truncate) : Number/Object\u003cbr /\u003e\n  A number for how many characters long URLs/emails/Twitter handles/Twitter\n  hashtags should be truncated to inside the text of a link. If the match is\n  over the number of characters, it will be truncated to this length by\n  replacing the end of the string with a two period ellipsis ('..').\u003cbr /\u003e\u003cbr /\u003e\n\n  Example: a url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated\n  to 25 characters may look like this: 'yahoo.com/some/long/pat..'\u003cbr /\u003e\u003cbr /\u003e\n\n  In the object form, both `length` and `location` may be specified to perform\n  truncation. Available options for `location` are: 'end' (default), 'middle',\n  or 'smart'. Example usage:\n\n    ```javascript\n    truncate: { length: 32, location: 'middle' }\n    ```\n\n  The 'smart' truncation option is for URLs where the algorithm attempts to\n  strip out unnecessary parts of the URL (such as the 'www.', then URL scheme,\n  hash, etc.) before trying to find a good point to insert the ellipsis if it is\n  still too long. For details, see source code of:\n  [TruncateSmart](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker.truncate.TruncateSmart)\n- [className](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-className) : String\u003cbr /\u003e\n  A CSS class name to add to the generated anchor tags. This class will be added\n  to all links, as well as this class plus \"url\"/\"email\"/\"phone\"/\"hashtag\"/\"mention\"/\"twitter\"/\"instagram\"\n  suffixes for styling url/email/phone/hashtag/mention links differently.\n\n  For example, if this config is provided as \"myLink\", then:\n\n  1) URL links will have the CSS classes: \"myLink myLink-url\"\u003cbr /\u003e\n  2) Email links will have the CSS classes: \"myLink myLink-email\"\u003cbr /\u003e\n  3) Phone links will have the CSS classes: \"myLink myLink-phone\"\u003cbr /\u003e\n  4) Twitter mention links will have the CSS classes: \"myLink myLink-mention myLink-twitter\"\u003cbr /\u003e\n  5) Instagram mention links will have the CSS classes: \"myLink myLink-mention myLink-instagram\"\u003cbr /\u003e\n  5) Hashtag links will have the CSS classes: \"myLink myLink-hashtag\"\u003cbr /\u003e\n- [replaceFn](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-replaceFn) : Function\u003cbr /\u003e\n  A function to use to programmatically make replacements of matches in the\n  input string, one at a time. See the section\n  \u003ca href=\"#custom-replacement-function\"\u003eCustom Replacement Function\u003c/a\u003e for\n  more details.\n\nFor example, if you wanted to disable links from opening in [new windows](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-newWindow), you could do:\n\n```javascript\nvar linkedText = Autolinker.link( \"Check out google.com\", { newWindow: false } );\n// Produces: \"Check out \u003ca href=\"http://google.com\"\u003egoogle.com\u003c/a\u003e\"\n```\n\nAnd if you wanted to truncate the length of URLs (while also not opening in a new window), you could do:\n\n```javascript\nvar linkedText = Autolinker.link( \"http://www.yahoo.com/some/long/path/to/a/file\", { truncate: 25, newWindow: false } );\n// Produces: \"\u003ca href=\"http://www.yahoo.com/some/long/path/to/a/file\"\u003eyahoo.com/some/long/pat..\u003c/a\u003e\"\n```\n\n## More Examples\nOne could update an entire DOM element that has unlinked text to auto-link them\nas such:\n\n```javascript\nvar myTextEl = document.getElementById( 'text' );\nmyTextEl.innerHTML = Autolinker.link( myTextEl.innerHTML );\n```\n\nUsing the same pre-configured [Autolinker](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker)\ninstance in multiple locations of a codebase (usually by dependency injection):\n\n```javascript\nvar autolinker = new Autolinker( { newWindow: false, truncate: 25 } );\n\n//...\n\nautolinker.link( \"Check out http://www.yahoo.com/some/long/path/to/a/file\" );\n// Produces: \"Check out \u003ca href=\"http://www.yahoo.com/some/long/path/to/a/file\"\u003eyahoo.com/some/long/pat..\u003c/a\u003e\"\n\n//...\n\nautolinker.link( \"Go to www.google.com\" );\n// Produces: \"Go to \u003ca href=\"http://www.google.com\"\u003egoogle.com\u003c/a\u003e\"\n\n```\n\n\n## Custom Replacement Function\n\nA custom replacement function ([replaceFn](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-replaceFn))\nmay be provided to replace url/email/phone/mention/hashtag matches on an\nindividual basis, based on the return from this function.\n\n#### Full example, for purposes of documenting the API:\n\n```javascript\nvar input = \"...\";  // string with URLs, Email Addresses, Mentions (Twitter, Instagram), and Hashtags\n\nvar linkedText = Autolinker.link( input, {\n    replaceFn : function( match ) {\n        console.log( \"href = \", match.getAnchorHref() );\n        console.log( \"text = \", match.getAnchorText() );\n\n        switch( match.getType() ) {\n            case 'url' :\n                console.log( \"url: \", match.getUrl() );\n\n                return true;  // let Autolinker perform its normal anchor tag replacement\n\n            case 'email' :\n                var email = match.getEmail();\n                console.log( \"email: \", email );\n\n                if( email === \"my@own.address\" ) {\n                    return false;  // don't auto-link this particular email address; leave as-is\n                } else {\n                    return;  // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)\n                }\n\n            case 'phone' :\n                console.log( \"Phone Number: \", match.getNumber() );\n\n                return '\u003ca href=\"http://newplace.to.link.phone.numbers.to/\"\u003e' + match.getNumber() + '\u003c/a\u003e';\n\n            case 'mention' :\n                console.log( \"Mention: \", match.getMention() );\n                console.log( \"Mention Service Name: \", match.getServiceName() );\n\n                return '\u003ca href=\"http://newplace.to.link.mention.handles.to/\"\u003e' + match.getMention() + '\u003c/a\u003e';\n\n            case 'hashtag' :\n                console.log( \"Hashtag: \", match.getHashtag() );\n\n                return '\u003ca href=\"http://newplace.to.link.hashtag.handles.to/\"\u003e' + match.getHashtag() + '\u003c/a\u003e';\n        }\n    }\n} );\n```\n\n#### Modifying the default generated anchor tag\n\n```javascript\nvar input = \"...\";  // string with URLs, Email Addresses, Mentions (Twitter, Instagram), and Hashtags\n\nvar linkedText = Autolinker.link( input, {\n    replaceFn : function( match ) {\n        console.log( \"href = \", match.getAnchorHref() );\n        console.log( \"text = \", match.getAnchorText() );\n\n        var tag = match.buildTag();         // returns an `Autolinker.HtmlTag` instance for an \u003ca\u003e tag\n        tag.setAttr( 'rel', 'nofollow' );   // adds a 'rel' attribute\n        tag.addClass( 'external-link' );    // adds a CSS class\n        tag.setInnerHtml( 'Click here!' );  // sets the inner html for the anchor tag\n\n        return tag;\n    }\n} );\n```\n\n\nThe `replaceFn` is provided one argument:\n\n1. An [Autolinker.match.Match](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker.match.Match)\n   object which details the match that is to be replaced.\n\n\nA replacement of the match is made based on the return value of the function.\nThe following return values may be provided:\n\n1. No return value (`undefined`), or `true` (Boolean): Delegate back to\n   Autolinker to replace the match as it normally would.\n2. `false` (Boolean): Do not replace the current match at all - leave as-is.\n3. Any String: If a string is returned from the function, the string will be used\n   directly as the replacement HTML for the match.\n4. An [Autolinker.HtmlTag](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker.HtmlTag)\n   instance, which can be used to build/modify an HTML tag before writing out its\n   HTML text.\n\n\n## Full API Docs\n\nThe full API docs for Autolinker may be referenced at:\n[http://gregjacobs.github.io/Autolinker.js/api/](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker)\n\n## Live Example\n\n[http://gregjacobs.github.io/Autolinker.js/examples/live-example/](http://gregjacobs.github.io/Autolinker.js/examples/live-example/)\n\n\n\n## Contributing\n\nPull requests definitely welcome.\n\n- Make sure to add tests to cover your new functionality/bugfix.\n- Run the `gulp test` command to build/test (or alternatively, open the\n  `tests/index.html` file to run the tests).\n- When committing, please omit checking in the files in the `dist/`\n  folder after building/testing. These are only committed to the\n  repository for users downloading Autolinker via Bower. I will build\n  these files and assign them a version number when merging your PR.\n- Please use tabs for indents! Tabs are better for everybody\n  (individuals can set their editors to different tab sizes based on\n  their visual preferences).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fratson%2Flinkize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fratson%2Flinkize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fratson%2Flinkize/lists"}