Skip to content

Refactor to linkifyjs! #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions angular-linkify.js

This file was deleted.

6 changes: 0 additions & 6 deletions angular-linkify.min.js

This file was deleted.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-linkify",
"version": "1.2.0",
"main": "angular-linkify.js",
"main": "dist/angular-linkify.js",
"description": "Angular filter to linkify urls, \"@\" usernames, and hashtags.",
"ignore": [
"**/.*",
Expand Down
2 changes: 2 additions & 0 deletions dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
26 changes: 26 additions & 0 deletions lib/directive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var angular = require('angular');
var linkifyElement = require('linkifyjs/element');

angular.module('linkify').directive('linkify',[
'linkify',
function (linkify) {
var defaultOptions = linkify.getOptions() || {};

var link = function (scope, element) {
var options = angular.extend(defaultOptions, scope.options);

scope.$evalAsync(function () {
try {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use a try/catch here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bizarre error on their part where if it finds just one text node that's empty, it errors and breaks the module

linkifyElement(element[0], options);
} catch (e) {}
});
};

return {
link: link,
scope: {
options: '=?linkify'
}
}
}
])
14 changes: 14 additions & 0 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var angular = require('angular');

angular.module('linkify')
.filter('linkify', [
'linkify',
function (linkify) {
return function (str, options) {
if (!str) { return str; }
return linkify.parse(str, options);
};
}
]);
13 changes: 13 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var angular = require('angular');
// add configuration for plugins
var linkifyjs = require('linkifyjs');
require('linkifyjs/plugins/hashtag')(linkifyjs);
require('./plugins/mentions')(linkifyjs);

module.exports = angular.module('linkify', []).name;

require('./filter');
require('./service');
require('./directive');
38 changes: 38 additions & 0 deletions lib/plugins/mentions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function mention(linkify) {
var TT = linkify.scanner.TOKENS,
// Text tokens
MT = linkify.parser.TOKENS,
// Multi tokens
MultiToken = MT.Base,
S_START = linkify.parser.start,
S_AT = undefined,
S_MENTION = undefined;

var MENTION = (function (_MultiToken) {
_inherits(MENTION, _MultiToken);

function MENTION(value) {
_classCallCheck(this, MENTION);

_MultiToken.call(this, value);
this.type = 'mention';
this.isLink = true;
}

return MENTION;
})(MultiToken);

S_AT = new linkify.parser.State();
S_MENTION = new linkify.parser.State(MENTION);

S_START.on(TT.AT, S_AT);
S_AT.on(TT.DOMAIN, S_MENTION);
S_AT.on(TT.TLD, S_MENTION);
}

module.exports = mention;
38 changes: 38 additions & 0 deletions lib/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var linkifyHtml = require('linkifyjs/html');
var angular = require('angular');

angular.module('linkify')
.provider('linkify', function () {
var defaultOptions = {
formatHref: function (value, type) {
if (type === 'hashtag') {
return 'https://twitter.com/hashtag/' + value.substring(1);
}
if (type === 'mention') {
return 'https://www.github.com/' + value.substring(1);
}
return value;
}
};

this.setOptions = function (options) {
defaultOptions = angular.extend(defaultOptions, options);
};

this.$get = [
function () {
return {
parse: function (str, options) {
if (!str) { return str; }
if (!options) { return linkifyHtml(str, defaultOptions); }
return linkifyHtml(str, angular.extend(defaultOptions, options));
},
getOptions: function () {
return defaultOptions;
}
};
}
];
});
29 changes: 19 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "angular-linkify",
"version": "1.2.0",
"description": "Angular filter to linkify urls, \"@\" usernames, and hashtags.",
"main": "angular-linkify.js",
"directories": {
"test": "test"
},
"version": "2.0.0",
"description": "Angular filter to linkify urls, @mentions and #hashtags.",
"main": "lib/index.js",
"scripts": {
"prepublish": "webpack -p --config webpack.prod.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand All @@ -21,13 +19,24 @@
"twitter"
],
"author": "Scott Corgan",
"license": "BSD",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/scottcorgan/angular-linkify/issues"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-uglify": "~0.2.2"
"angular-mocks": "^1.5.0",
"chai": "^3.5.0",
"karma": "^0.13.19",
"karma-chai": "^0.1.0",
"karma-mocha": "^0.2.1",
"karma-webpack": "^1.7.0",
"mocha": "^2.4.5",
"phantomjs": "^2.1.3",
"phantomjs-polyfill": "0.0.1",
"webpack": "^1.12.13"
},
"dependencies": {
"angular": "^1.5.0",
"linkifyjs": "^2.0.0-beta.9"
}
}
34 changes: 34 additions & 0 deletions webpack.base.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var webpack = require('webpack');

module.exports = {
entry: './lib/index.js',

resolve: {
modulesDirectories: ['node_modules', 'bower_components'],
extensions: ['', '.js']
},

externals: [
{
angular: {
root: "angular",
commonjs2: "angular",
commonjs: "angular",
amd: "angular"
}
}
],

output: {
library: 'AngularLinkify',
libraryTarget: 'umd',
path: './dist',
filename: 'angular-linkify.js'
},

module: {
loaders: []
}
};
8 changes: 8 additions & 0 deletions webpack.dev.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'
var baseConfig = require('./webpack.base.config.js');

var config = baseConfig;

config.devtool = "inline-source-map";

module.exports = config;
14 changes: 14 additions & 0 deletions webpack.prod.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

var webpack = require('webpack');
var baseConfig = require('./webpack.base.config.js');

var config = baseConfig;

config.plugins = [
new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') }),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin()
];

module.exports = config;