-
Notifications
You must be signed in to change notification settings - Fork 106
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
Support custom rules #20
Changes from all commits
638646d
3bea2b4
299600b
8c72dd8
2cf3e99
d8b7ac4
4f72d2a
35512d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,33 @@ import React from 'react'; | |
import LinkifyIt from 'linkify-it'; | ||
import tlds from 'tlds'; | ||
|
||
const linkify = new LinkifyIt(); | ||
linkify.tlds(tlds); | ||
const globalCustomizations = { | ||
add: [], | ||
tlds: [], | ||
set: [] | ||
}; | ||
|
||
export const config = { | ||
add: (...args) => { | ||
globalCustomizations.add.push(args); | ||
return config; | ||
}, | ||
tlds: (...args) => { | ||
globalCustomizations.tlds.push(args); | ||
return config; | ||
}, | ||
set: (...args) => { | ||
globalCustomizations.set.push(args); | ||
return config; | ||
}, | ||
resetAll: () => { | ||
for (let type in globalCustomizations) { | ||
globalCustomizations[type] = []; | ||
} | ||
|
||
return config; | ||
} | ||
}; | ||
|
||
class Linkify extends React.Component { | ||
static MATCH = 'LINKIFY_MATCH' | ||
|
@@ -13,19 +38,62 @@ class Linkify extends React.Component { | |
component: React.PropTypes.any, | ||
properties: React.PropTypes.object, | ||
urlRegex: React.PropTypes.object, | ||
emailRegex: React.PropTypes.object | ||
emailRegex: React.PropTypes.object, | ||
handlers: React.PropTypes.arrayOf( | ||
React.PropTypes.shape({ | ||
prefix: React.PropTypes.string.isRequired, | ||
validate: React.PropTypes.func.isRequired, | ||
normalize: React.PropTypes.func.isRequired | ||
}) | ||
), | ||
fuzzyLink: React.PropTypes.bool, | ||
fuzzyIP: React.PropTypes.bool, | ||
fuzzyEmail: React.PropTypes.bool | ||
} | ||
|
||
static defaultProps = { | ||
className: 'Linkify', | ||
component: 'a', | ||
properties: {}, | ||
handlers: [] | ||
} | ||
|
||
componentDidMount() { | ||
this.addCustomHandlers(); | ||
} | ||
|
||
componentDidUpdate(nextProps) { | ||
this.addCustomHandlers(); | ||
} | ||
|
||
addCustomHandlers() { | ||
const { handlers } = this.props; | ||
|
||
this.linkify = this.linkify || new LinkifyIt(); | ||
this.linkify.tlds(tlds); | ||
|
||
// add global customizations | ||
for (let type in globalCustomizations) { | ||
globalCustomizations[type].forEach(c => this.linkify[type](...c)) | ||
} | ||
|
||
// add instance customizations | ||
(handlers || []).forEach((handler) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.linkify.add(handler.prefix, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens to handlers that are removed in the new props? Removed handlers should be set to |
||
validate: handler.validate, | ||
normalize: handler.normalize | ||
}); | ||
}); | ||
|
||
['fuzzyLink', 'fuzzyIP', 'fuzzyEmail'].forEach(f => { | ||
typeof this.props[f] === 'boolean' && this.linkify.set({ [f]: this.props[f] }) | ||
}) | ||
} | ||
|
||
parseCounter = 0 | ||
|
||
getMatches(string) { | ||
return linkify.match(string); | ||
return this.linkify.match(string); | ||
} | ||
|
||
parseString(string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we move
this.linkify = new LinkifiyIt();
intocomponentDidMount()
so thatthis.linkify
will always be defined when callingaddCustomHandlers()
.