Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Nov 7, 2016
0 parents commit 45b570b
Show file tree
Hide file tree
Showing 12 changed files with 417 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"env": {
"build": {
"presets": [ "es2015-rollup" ]
},
"test": {
"presets": [ "es2015" ]
}
}
}
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json}]
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[{package.json}]
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
coverage/
.nyc_output/
dist/
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "holidaycheck/es2015"
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
*.log
coverage/
.nyc_output/
dist/
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
sudo: false
node_js:
- "6"
- "7"
notifications:
email: false
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 HolidayCheck

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[![NPM Version](https://img.shields.io/npm/v/delta-detect-links.svg?style=flat)](https://www.npmjs.org/package/delta-detect-links)
[![Build Status](https://img.shields.io/travis/holidaycheck/delta-detect-links/master.svg?style=flat)](https://travis-ci.org/holidaycheck/delta-detect-links)
[![Dependencies](http://img.shields.io/david/holidaycheck/delta-detect-links.svg?style=flat)](https://david-dm.org/holidaycheck/delta-detect-links)

# delta-detect-links

> Detect links in a delta object.
This small library is based on [`linkify-it`](https://github.com/markdown-it/linkify-it) to detect links in a rich text [`Delta`](https://github.com/ottypes/rich-text) object.
The main purpose for this library is to automatically detect links in [Quill](http://quilljs.com/) documents.

## API

### `detectLinks`

This function detects all links in the given `insert`-only delta object.

```js
const detectLinks = require('delta-detect-links');

detectLinks((new Delta()).insert('Visit www.example.com\n'));
// → [ { insert: 'Visit ' }, { insert: 'www.example.com', attributes: { link: 'http://www.example.com' } }, { insert: '\n' } ]
```

Already existing links in the given delta object will be skipped and not highlighted a second time.

#### Options

`detectLinks` supports an optional second parameter to set the following options:

* `skipTrailingMatch` (default: `false`): This is useful when you want to detect links in real-time while the user types. In order to prevent detection of partial links the link will only be detected after another stop word has been encountered.
* `lastIndex`: This option is only used in combination with `skipTrailingMatch`. This value is used to determine the trailing match. It is useful in scenarios where the current cursor is not at the same position as the end of the document.

### `detectLinksDelta`

Similar to `detectLinks` but only contains the changes delta object.

```js
const detectLinksDelta = require('delta-detect-links').detectLinksDelta;

detectLinksDelta((new Delta()).insert('Visit www.example.com\n'));
// → [ { retain: 6 }, { retain: 15, attributes: { link: 'http://www.example.com' } } ]
```
64 changes: 64 additions & 0 deletions lib/detectLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import linkifyIt from 'linkify-it';
import tlds from 'tlds';
import { Delta } from 'rich-text';

const punctuator = '!';

function getOperationText(op) {
if (typeof op.insert === 'string') {
if (op.attributes && op.attributes.link) {
return (new Array(op.insert.length + 1)).join(punctuator);
}
return op.insert;
}
return punctuator;
}

const linkify = linkifyIt()
.tlds(tlds)
.add('ftp:', null)
.add('mailto:', null)
.set({ fuzzyLink: true })
.set({ fuzzyIP: false })
.set({ fuzzyEmail: false });

function isTrailingLinkMatch(link, lastIndex, text) {
const lastCharacter = text[lastIndex - 1];

if (lastCharacter === '.') {
return link.lastIndex === lastIndex - 1;
}

return link.lastIndex === lastIndex;
}
function detectLinksDelta(delta, options = {}) {
const text = delta.ops.reduce((result, operation) => result + getOperationText(operation), '');
const matchedLinks = linkify.match(text) || [];
let currentIndex = 0;
const links = new Delta();

matchedLinks.forEach(function (link) {
const numberOfCharactersToLinkStart = link.index - currentIndex;
const linkTextLength = link.lastIndex - link.index;

links.retain(numberOfCharactersToLinkStart);

if (options.skipTrailingMatch && isTrailingLinkMatch(link, options.lastIndex, text)) {
links.retain(linkTextLength);
} else {
links.retain(linkTextLength, { link: link.url });
}

currentIndex = link.lastIndex;
});

return links;
}

export default function detectLinks(delta, options = {}) {
const links = detectLinksDelta(delta, options);

return delta.compose(links);
}

detectLinks.detectLinksDelta = detectLinksDelta;
68 changes: 68 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"name": "delta-detect-links",
"version": "1.0.0",
"description": "Detect links in a delta object.",
"main": "./dist/detectLinks.cjs.js",
"scripts": {
"build": "BABEL_ENV=build rollup -c rollup.config.js",
"test:unit": "BABEL_ENV=test ava 'test/**/*Spec.js'",
"lint": "eslint .",
"pretest": "npm run lint",
"test": "nyc npm run test:unit",
"prepublish": "npm run build"
},
"author": "Mathias Schreck <[email protected]>",
"license": "MIT",
"files": [ "README.md", "LICENSE", "dist/" ],
"keywords": [
"delta",
"rich-text",
"quill",
"detect",
"link"
],
"devDependencies": {
"ava": "0.16.0",
"babel-preset-es2015": "6.18.0",
"babel-preset-es2015-rollup": "1.2.0",
"babel-register": "6.18.0",
"eslint": "3.9.1",
"eslint-config-holidaycheck": "0.10.0",
"nyc": "8.4.0",
"rollup": "0.36.3",
"rollup-plugin-babel": "2.6.1",
"rollup-plugin-commonjs": "5.0.5",
"rollup-plugin-node-resolve": "2.0.0"
},
"ava": {
"require": "babel-register",
"babel": {
"babelrc": true
}
},
"nyc": {
"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,
"include": [
"lib/**/*.js"
],
"reporter": [
"lcov",
"text-summary"
],
"cache": true,
"all": true,
"check-coverage": true
},
"dependencies": {
"linkify-it": "2.0.2",
"rich-text": "3.1.0",
"tlds": "1.169.0"
},
"repository": {
"type": "git",
"url": "git://github.com/holidaycheck/delta-detect-links.git"
}
}
20 changes: 20 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env node */

import babel from 'rollup-plugin-babel';
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

const plugins = [
nodeResolve({ jsnext: true, main: true, browser: true, extensions: [ '.js', '.json' ] }),
commonjs({ namedExports: { 'node_modules/rich-text/index.js': [ 'Delta' ] } }),
babel()
];

module.exports = {
entry: 'lib/detectLinks.js',
plugins,
targets: [
{ dest: 'dist/detectLinks.cjs.js', format: 'cjs' },
{ dest: 'dist/detectLinks.amd.js', format: 'amd', moduleId: 'delta-detect-links' }
]
};
Loading

0 comments on commit 45b570b

Please sign in to comment.