Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

anchorme

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

anchorme - npm Package Compare versions

Comparing version 0.8.3 to 1.0.0

dist-node/index.d.ts

37

package.json
{
"name": "anchorme",
"version": "0.8.3",
"version": "1.0.0",
"description": "A library to convert URLs to a clickable HTML anchor elements",
"main": "./dist/anchorme.js",
"main": "./dist-node/index.js",
"types": "./dist-node/index.d.ts",
"scripts": {
"test": "mocha test/run.js",
"bench": "node test/bench.js",
"test": "jest",
"bench": "node benchmark/bench",
"build": "node build/build"

@@ -28,10 +29,28 @@ },

"devDependencies": {
"babel-plugin-external-helpers": "^6.8.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-es2015-rollup": "^1.2.0",
"@types/jest": "^18.1.1",
"jest": "^19.0.2",
"rollup": "^0.36.3",
"rollup-plugin-babel": "^2.6.1",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-commonjs": "^7.0.0",
"ts-jest": "^19.0.0",
"typescript": "^2.2.1",
"uglify-js": "^2.7.3"
},
"dependencies": {}
"dependencies": {},
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"\\.(ts)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": ".*spec\\.(ts|js)$",
"globals": {
"__TS_CONFIG__": {
"module": "commonjs"
}
}
}
}

@@ -5,255 +5,37 @@ # Anchorme.js

## Features
## [Getting Started, Documentations, Demos and more](http://alexcorvi.github.io/anchorme.js/)
* Highly sensitive.
* produces the least possible false positives if any.
* Skips HTML, so it doesn't break your HTML if it had a URL as an attribute for an element.
* Links with or without protocols.
* Preserve upper and lower case, although when detecting, it's basically case insensitive.
* Checks against full IANA list of TLDs.
* Works with IPs, FTPs, Emails and files.
* Also works when ports are defined.
* Small in size.
* No RegExp involved, very readable and maintainable.
* Supports setting custom attributes with any values.
* Supports checking IPs only, Emails only, or URLs only.
* Helper methods can be used for other purposes.
## What's Included
## Getting Started
* __Sensitivity__: It's Highly sensitive with the least false positives.
- It validates URLs and Emails against full IANA list
- Validates port numbers (if present)
- Validates IP octet numbers (if present)
* __Robustness__:
- Skips HTML, so it doesn't break your HTML if it had a URL as an attribute for an element.
- Links with or without protocols.
- Works with IPs, FTPs, Emails and files.
- Can detect parenthesis and quotation marks as part of the URL or as a surrounding to the URL.
* __Fast__: It's definitely fast! processing H.G. Wells _The Time Machine_ novel with over 1500 URLs inserted at random places takes only 3.5 seconds.
* __Light Weight__: Although it's a feature rich library with a full IANA list included, it's only __6KB__ when minified and gzipped.
### Download
#### File
## Contributing
Download the library from `dist` folder (either `anchorme.js` or `anchorme.min.js`).
This project is written in Typescript and compiled to JavaScript.
#### NPM
### Prerequisites:
- Typescript installed globally
- Jest installed globally (for testing)
Install via NPM: `npm install anchorme`
### Usage
```javascript
var anchorme = require("anchorme"); // if installed via NPM
var someText = "this is a text with a link www.github.com ..";
var result = anchorme(someText);
// You can also pass few options
anchorme(someText,{
// attributes to add to the anchor tags
attributes:[
// can be objects
{
name:"target",
value:"_blank",
},
// or functions
function(obj){
if(obj.reason === "email") return {name:"class",value:"email"};
else return {name:"class",value:"regular-link"}
}
// read below to know more about this
// and other options
],
})
```
## Demo
To test how this library would work for you, head over to [here](http://alexcorvi.github.io/anchorme.js/) to test it.
## Available options
### Truncation
This will convert a long like like this:
https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js
to this:
[https://raw.githubusercontent.com/alexcorv...](https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js)
**Default Value:** `0` (Won't truncate)
**Example**
```javascript
anchorme(string,{
truncate:40
})
```
### Truncate from the middle
This will make the truncation (as seen above) removing characters from the middle instead of the end. So it will produce a link like this one: [raw.githubusercontent.com/.../hasprotocol.js](https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js)
```javascript
anchorme(string,{
truncate:[26,15],
// means 26 characters from the beginning
// and 15 characters from the end
})
```
### Excluding
You can exclude IPs/Emails/URLs/Files like this:
```javascript
anchorme(string,{
emails:false,
urls:false,
ips:false,
files:false
})
// the example above won't do anything to your string
// since you're excluding every possible change
```
**Default Value:** all are `true`
### Adding attributes
You can add attributes to the links produced by anchorme. using the `attributes` prop in the options. this options should be an array of the attributes you'd like to pass.
Values of this array can be:
* Plain objects
```javascript
anchorme(string,{
attributes:[
{
// attribute name
name:"class",
// attribute value
value:"something"
},
{
name:"target",
value:"blank"
}
]
});
```
* Functions that return an object
```javascript
anchorme(string,{
attributes:[
{
name:"class",
value:"link"
},
function(data){
if(data.reason === "ip") return {name:"class",value:"ip-link"};
},
function(data){
if(data.protocol !== "mailto:") return {name:"target",value:"blank"};
// following conditions can also be used:
// if(data.raw.indexOf("@") > 0) return {name:"target",value:"blank"};
// if(data.reason !== "email") return {name:"target",value:"blank"};
}
]
});
```
Where `data` is an object containing detailed info about the link in question. The example above will add `ip-link` class to all the links that are IPs, and add `target='_blank'` to all the links that are not emails.
If you log the data object you'll get something similar to this:
```javascript
{
// the reason this fragment
// was detected
// possible reasons: "file", "url", "ip", "email"
"reason": "email",
// the protocol that the link came with
// or the protocol that was added to the link
"protocol": "mailto:",
// the link (without any modification)
"raw": "a@b.co",
// the encoded version of the link
// i.e. non-Latin characters -> URI encoding
// also doesn't have a protocol (if it came with any)
"encoded": "a@b.co",
}
```
### Setting default protocol
If the link came without protocol, like `www.google.com` then anchorme will add the `http://` by default. However you can set your own default protocol.
```javascript
anchorme(string,{
defaultProtocol:"ftp://",
// ... or anything
})
```
In some cases, you might want the protocol to be set conditionally. Anchorme allows you to pass a function as the `defaultProtocol` and uses whatever this function returns.
```javascript
anchorme(string,{
defaultProtocol:function(url){
// where url is like: "www.google.com"
if(url.indexOf("secure") > 0) return "https://";
else return "http://";
},
})
```
## Additional functionalities
### Listing all valid URLs
Although anchorme was authored to transform URLs in text strings to a click-able HTML anchor tags, passing `true` to `list` property in options will change the library's behavior and instead of returning a text with an HTML tags it will only return an array of valid URLs.
```javascript
anchorme(myText,{
list:true
})
```
### Validating
it can also be used for validation:
```javascript
anchorme.validate.ip("1.1.1.1:3000/something"); // returns true
anchorme.validate.email("alex@array.com"); // return true
anchorme.validate.url("google.co.uk"); // returns true
```
## Contributing
### How to contribute
- Clone this repository
- `cd anchorme.js && npm install`
- install mocha globally (for running the tests): `mocha test/run`
- ..
- Build `node build/build`
- Test `node test/run`
- ..
- Add unit tests if needed
- Run `npm run test` for testing
- Run `npm run build` for building
* *
-----
License: The MIT License (MIT) - Copyright (c) 2017 Alex Corvi
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc