Skip to content

Commit a4c26d4

Browse files
author
Nick Frasser
authored
Tickets plugin (#156)
* Next jump method for TokenState class Used for resolving ambiguous states transitions between plugins. Includes tests * Hashtag and Mention plugins should use `jump` method from start state * Complete ticket plugin implementation Includes tests and build templates * A few tests and enhancemenets in response to ticket-related changes
1 parent 1b0424c commit a4c26d4

File tree

14 files changed

+196
-25
lines changed

14 files changed

+196
-25
lines changed

plugins/ticket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../lib/linkify/plugins/ticket').default;

src/linkify/core/state.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ BaseState.prototype = {
6464
@return {State} state Returns false if no jumps are available
6565
*/
6666
next(item) {
67-
6867
for (let i = 0; i < this.j.length; i++) {
69-
7068
let jump = this.j[i];
7169
let symbol = jump[0]; // Next item to check for
7270
let state = jump[1]; // State to jump to if items match
@@ -148,6 +146,26 @@ const CharacterState = inherits(BaseState, createStateClass(), {
148146
*/
149147
const TokenState = inherits(BaseState, createStateClass(), {
150148

149+
/**
150+
* Similar to `on`, but returns the state the results in the transition from
151+
* the given item
152+
* @method jump
153+
* @param {Mixed} item
154+
* @param {Token} [token]
155+
* @return state
156+
*/
157+
jump(token, tClass = null) {
158+
var state = this.next(new token('')); // dummy temp token
159+
if (state === this.defaultTransition) {
160+
// Make a new state!
161+
state = new this.constructor(tClass);
162+
this.on(token, state);
163+
} else if (tClass) {
164+
state.T = tClass;
165+
}
166+
return state;
167+
},
168+
151169
/**
152170
Is the given token an instance of the given token class?
153171

src/linkify/plugins/hashtag.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
*/
44
export default function hashtag(linkify) {
55
let TT = linkify.scanner.TOKENS; // Text tokens
6-
let MT = linkify.parser.TOKENS; // Multi tokens
7-
let MultiToken = MT.Base;
6+
let MultiToken = linkify.parser.TOKENS.Base; // Base Multi token class
87
let S_START = linkify.parser.start;
9-
let S_HASH, S_HASHTAG;
108

119
function HASHTAG(value) {
1210
this.v = value;
@@ -17,10 +15,10 @@ export default function hashtag(linkify) {
1715
isLink: true
1816
});
1917

20-
S_HASH = new linkify.parser.State();
21-
S_HASHTAG = new linkify.parser.State(HASHTAG);
18+
const S_HASH = S_START.jump(TT.POUND);
19+
const S_HASHTAG = new linkify.parser.State(HASHTAG);
2220

23-
S_START.on(TT.POUND, S_HASH);
2421
S_HASH.on(TT.DOMAIN, S_HASHTAG);
2522
S_HASH.on(TT.TLD, S_HASHTAG);
23+
S_HASH.on(TT.LOCALHOST, S_HASHTAG);
2624
}

src/linkify/plugins/mention.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
Quick Mention parser plugin for linkify
2+
Mention parser plugin for linkify
33
*/
44
export default function mention(linkify) {
55
const TT = linkify.scanner.TOKENS; // Text tokens
@@ -26,15 +26,12 @@ export default function mention(linkify) {
2626
}
2727
});
2828

29-
const S_AT = new State();
29+
const S_AT = S_START.jump(TT.AT); // @
3030
const S_AT_SYMS = new State();
3131
const S_MENTION = new State(MENTION);
3232
const S_MENTION_SLASH = new State();
3333
const S_MENTION_SLASH_SYMS = new State();
3434

35-
// @
36-
S_START.on(TT.AT, S_AT);
37-
3835
// @_,
3936
S_AT.on(TT_UNDERSCORE, S_AT_SYMS);
4037

src/linkify/plugins/ticket.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
Ticket number detector
3+
TODO: Add cross-repo style tickets? e.g., SoapBox/linkifyjs#42
4+
Is that even feasible?
5+
*/
6+
export default function ticket(linkify) {
7+
let TT = linkify.scanner.TOKENS; // Base Multi token class
8+
let MultiToken = linkify.parser.TOKENS.Base;
9+
let S_START = linkify.parser.start;
10+
11+
function TICKET(value) {
12+
this.v = value;
13+
}
14+
15+
linkify.inherits(MultiToken, TICKET, {
16+
type: 'ticket',
17+
isLink: true
18+
});
19+
20+
const S_HASH = S_START.jump(TT.POUND);
21+
const S_TICKET = new linkify.parser.State(TICKET);
22+
23+
S_HASH.on(TT.NUM, S_TICKET);
24+
}

tasks/uglify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ exports.unmangleableProps = [
4949
'ignoreTags',
5050
'inherits',
5151
'isLink',
52+
'jump',
5253
'key',
5354
'linkAttributes',
5455
'linkClass',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<%= contents %>
2+
require(['linkify', 'linkify/plugins/ticket'], function (linkify, ticket) {
3+
ticket(linkify);
4+
});

templates/linkify/plugins/ticket.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
;(function (linkify) {
2+
<%= contents %>
3+
plugin(linkify);
4+
})(linkify);

test/amd.conf.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ module.exports = {
1818
{pattern: 'test/qunit/react.amd.js', included: false, served: true},
1919
'dist/linkify-polyfill.min.js',
2020
'dist/linkify.amd.min.js',
21+
// 'dist/linkify.amd.js', // Uncompressed
2122
'test/qunit/ie8.js',
2223
{pattern: 'dist/linkify-react.amd.min.js', included: false, served: true},
24+
// {pattern: 'dist/linkify-react.amd.js', included: false, served: true}, // Uncompressed
2325
'dist/*.amd.min.js',
26+
// 'dist/*.amd.js', // Uncompressed
2427
'test/qunit/amd.js',
2528
'test/qunit/main.js'
2629
],
2730

2831
// list of files to exclude
29-
exclude: [],
32+
exclude: [
33+
// '*.min.js', // Uncompressed
34+
],
3035

3136
// QUnit configuration
3237
client: {

test/conf.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ module.exports = {
1414
{pattern: 'node_modules/react-dom/dist/react-dom.js', watched: false, included: false, served: true},
1515
'dist/linkify-polyfill.min.js',
1616
'dist/linkify.min.js',
17+
// 'dist/linkify.js', // Uncompressed
1718
'test/qunit/ie8.js',
1819
{pattern: 'dist/linkify-react.min.js', included: false, served: true},
20+
// {pattern: 'dist/linkify-reactjs', included: false, served: true}, // Uncompressed
1921
'dist/*.min.js',
22+
// 'dist/*.js', // Uncompressed
2023
'test/qunit/globals.js',
2124
'test/qunit/main.js'
2225
],
2326

2427
// list of files to exclude
2528
exclude: [
26-
'dist/*.amd.min.js'
29+
'dist/*.amd.min.js',
30+
// 'dist/*.amd.js', // Uncompressed
31+
// 'dist/*.min.js', // Uncompressed
2732
],
2833

2934
// QUnit configuration

0 commit comments

Comments
 (0)