linkify-react
Advanced tools
Comparing version
var Linkify = (function (React, linkifyjs) { | ||
'use strict'; | ||
'use strict'; | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
} | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
} | ||
var React__namespace = /*#__PURE__*/_interopNamespace(React); | ||
var React__namespace = /*#__PURE__*/_interopNamespace(React); | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
/** | ||
* Given a string, converts to an array of valid React components | ||
* (which may include strings) | ||
* @param {string} str | ||
* @param {Options} opts | ||
* @returns {React.ReactNodeArray} | ||
*/ | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function stringToElements(str, opts, parentElementId) { | ||
var tokens = linkifyjs.tokenize(str); | ||
var elements = []; | ||
var nlId = 0; | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} | ||
for (var i = 0; i < tokens.length; i++) { | ||
var token = tokens[i]; | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
if (token.t === 'nl' && opts.get('nl2br')) { | ||
elements.push(React__namespace.createElement('br', { | ||
key: "__linkify-el-".concat(parentElementId, "-nl-").concat(nlId++) | ||
})); | ||
} else if (!token.isLink || !opts.check(token)) { | ||
// Regular text | ||
elements.push(token.toString()); | ||
} else { | ||
elements.push(opts.render(token)); | ||
} | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
return elements; | ||
} // Recursively linkify the contents of the given React Element instance | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
/** | ||
* @template P | ||
* @template {string | React.JSXElementConstructor<P>} T | ||
* @param {React.ReactElement<P, T>} element | ||
* @param {Options} opts | ||
* @param {number} elementId | ||
* @returns {React.ReactElement<P, T>} | ||
*/ | ||
return arr2; | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
function linkifyReactElement(element, opts) { | ||
var elementId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; | ||
/** | ||
* Given a string, converts to an array of valid React components | ||
* (which may include strings) | ||
* @param {string} str | ||
* @param {any} opts | ||
* @returns {React.ReactNodeArray} | ||
*/ | ||
if (React__namespace.Children.count(element.props.children) === 0) { | ||
// No need to clone if the element had no children | ||
return element; | ||
} | ||
function stringToElements(str, opts) { | ||
var tokens = linkifyjs.tokenize(str); | ||
var elements = []; | ||
var linkId = 0; | ||
var children = []; | ||
React__namespace.Children.forEach(element.props.children, function (child) { | ||
if (typeof child === 'string') { | ||
// ensure that we always generate unique element IDs for keys | ||
children.push.apply(children, stringToElements(child, opts, elementId)); | ||
} else if (React__namespace.isValidElement(child)) { | ||
if (typeof child.type === 'string' && opts.ignoreTags.indexOf(child.type.toUpperCase()) >= 0) { | ||
// Don't linkify this element | ||
children.push(child); | ||
} else { | ||
children.push(linkifyReactElement(child, opts, elementId + 1)); | ||
} | ||
} else { | ||
// Unknown element type, just push | ||
children.push(child); | ||
} | ||
}); // Set a default unique key, copy over remaining props | ||
for (var i = 0; i < tokens.length; i++) { | ||
var token = tokens[i]; | ||
var newProps = linkifyjs.options.assign({ | ||
key: "__linkify-el-".concat(elementId) | ||
}, element.props); | ||
return React__namespace.cloneElement(element, newProps, children); | ||
} | ||
/** | ||
* @template P | ||
* @template {string | React.JSXElementConstructor<P>} T | ||
* @param {P & { tagName?: T, options?: any, children?: React.ReactNode}} props | ||
* @returns {React.ReactElement<P, T>} | ||
*/ | ||
if (token.t === 'nl' && opts.nl2br) { | ||
elements.push(React__namespace.createElement('br', { | ||
key: "linkified-".concat(++linkId) | ||
})); | ||
continue; | ||
} else if (!token.isLink || !opts.check(token)) { | ||
// Regular text | ||
elements.push(token.toString()); | ||
continue; | ||
} | ||
var _opts$resolve = opts.resolve(token), | ||
formatted = _opts$resolve.formatted, | ||
formattedHref = _opts$resolve.formattedHref, | ||
tagName = _opts$resolve.tagName, | ||
className = _opts$resolve.className, | ||
target = _opts$resolve.target, | ||
rel = _opts$resolve.rel, | ||
attributes = _opts$resolve.attributes; | ||
var Linkify = function Linkify(props) { | ||
// Copy over all non-linkify-specific props | ||
var linkId = 0; | ||
var props = { | ||
key: "linkified-".concat(++linkId), | ||
href: formattedHref | ||
}; | ||
var defaultLinkRender = function defaultLinkRender(_ref) { | ||
var tagName = _ref.tagName, | ||
attributes = _ref.attributes, | ||
content = _ref.content; | ||
attributes.key = "__linkify-lnk-".concat(++linkId); | ||
if (className) { | ||
props.className = className; | ||
} | ||
if (attributes.class) { | ||
attributes.className = attributes.class; | ||
delete attributes.class; | ||
} | ||
if (target) { | ||
props.target = target; | ||
} | ||
return React__namespace.createElement(tagName, attributes, content); | ||
}; | ||
if (rel) { | ||
props.rel = rel; | ||
} // Build up additional attributes | ||
// Support for events via attributes hash | ||
var newProps = { | ||
key: '__linkify-wrapper' | ||
}; | ||
for (var prop in props) { | ||
if (prop !== 'options' && prop !== 'tagName' && prop !== 'children') { | ||
newProps[prop] = props[prop]; | ||
} | ||
} | ||
if (attributes) { | ||
for (var attr in attributes) { | ||
props[attr] = attributes[attr]; | ||
} | ||
} | ||
var opts = new linkifyjs.Options(props.options, defaultLinkRender); | ||
var tagName = props.tagName || React__namespace.Fragment || 'span'; | ||
var children = props.children; | ||
var element = React__namespace.createElement(tagName, newProps, children); | ||
return linkifyReactElement(element, opts, 0); | ||
}; | ||
elements.push(React__namespace.createElement(tagName, props, formatted)); | ||
} | ||
return Linkify; | ||
return elements; | ||
} // Recursively linkify the contents of the given React Element instance | ||
/** | ||
* @template P | ||
* @template {string | React.JSXElementConstructor<P>} T | ||
* @param {React.ReactElement<P, T>} element | ||
* @param {Object} opts | ||
* @param {number} elementId | ||
* @returns {React.ReactElement<P, T>} | ||
*/ | ||
function linkifyReactElement(element, opts) { | ||
var elementId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; | ||
if (React__namespace.Children.count(element.props.children) === 0) { | ||
// No need to clone if the element had no children | ||
return element; | ||
} | ||
var children = []; | ||
React__namespace.Children.forEach(element.props.children, function (child) { | ||
if (typeof child === 'string') { | ||
// ensure that we always generate unique element IDs for keys | ||
elementId = elementId + 1; | ||
children.push.apply(children, _toConsumableArray(stringToElements(child, opts))); | ||
} else if (React__namespace.isValidElement(child)) { | ||
if (typeof child.type === 'string' && opts.ignoreTags.indexOf(child.type.toUpperCase()) >= 0) { | ||
// Don't linkify this element | ||
children.push(child); | ||
} else { | ||
children.push(linkifyReactElement(child, opts, ++elementId)); | ||
} | ||
} else { | ||
// Unknown element type, just push | ||
children.push(child); | ||
} | ||
}); // Set a default unique key, copy over remaining props | ||
var newProps = { | ||
key: "linkified-element-".concat(elementId) | ||
}; | ||
for (var prop in element.props) { | ||
newProps[prop] = element.props[prop]; | ||
} | ||
return React__namespace.cloneElement(element, newProps, children); | ||
} | ||
/** | ||
* @template P | ||
* @template {string | React.JSXElementConstructor<P>} T | ||
* @param {P & { tagName?: T, options?: any, children?: React.ReactNode}} props | ||
* @returns {React.ReactElement<P, T>} | ||
*/ | ||
var Linkify = function Linkify(props) { | ||
// Copy over all non-linkify-specific props | ||
var newProps = { | ||
key: 'linkified-element-wrapper' | ||
}; | ||
for (var prop in props) { | ||
if (prop !== 'options' && prop !== 'tagName' && prop !== 'children') { | ||
newProps[prop] = props[prop]; | ||
} | ||
} | ||
var opts = new linkifyjs.Options(props.options); | ||
var tagName = props.tagName || React__namespace.Fragment || 'span'; | ||
var children = props.children; | ||
var element = React__namespace.createElement(tagName, newProps, children); | ||
return linkifyReactElement(element, opts, 0); | ||
}; | ||
return Linkify; | ||
})(React, linkify); |
@@ -1,1 +0,1 @@ | ||
var Linkify=function(e,r){"use strict";function t(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var n=t(e);function i(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,r){if(!e)return;if("string"==typeof e)return a(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return a(e,r)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function o(e,t){for(var i=r.tokenize(e),a=[],o=0,l=0;l<i.length;l++){var f=i[l];if("nl"===f.t&&t.nl2br)a.push(n.createElement("br",{key:"linkified-".concat(++o)}));else if(f.isLink&&t.check(f)){var c=t.resolve(f),u=c.formatted,s=c.formattedHref,p=c.tagName,y=c.className,m=c.target,d=c.rel,h=c.attributes,g={key:"linkified-".concat(++o),href:s};if(y&&(g.className=y),m&&(g.target=m),d&&(g.rel=d),h)for(var v in h)g[v]=h[v];a.push(n.createElement(p,g,u))}else a.push(f.toString())}return a}function l(e,r){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;if(0===n.Children.count(e.props.children))return e;var a=[];n.Children.forEach(e.props.children,(function(e){"string"==typeof e?(t+=1,a.push.apply(a,i(o(e,r)))):n.isValidElement(e)?"string"==typeof e.type&&r.ignoreTags.indexOf(e.type.toUpperCase())>=0?a.push(e):a.push(l(e,r,++t)):a.push(e)}));var f={key:"linkified-element-".concat(t)};for(var c in e.props)f[c]=e.props[c];return n.cloneElement(e,f,a)}return function(e){var t={key:"linkified-element-wrapper"};for(var i in e)"options"!==i&&"tagName"!==i&&"children"!==i&&(t[i]=e[i]);var a=new r.Options(e.options),o=e.tagName||n.Fragment||"span",f=e.children;return l(n.createElement(o,t,f),a,0)}}(React,linkify); | ||
var Linkify=function(e,n){"use strict";function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})}})),n.default=e,Object.freeze(n)}var r=t(e);function i(e,t,i){for(var a=n.tokenize(e),c=[],o=0,l=0;l<a.length;l++){var s=a[l];"nl"===s.t&&t.get("nl2br")?c.push(r.createElement("br",{key:"__linkify-el-".concat(i,"-nl-").concat(o++)})):s.isLink&&t.check(s)?c.push(t.render(s)):c.push(s.toString())}return c}function a(e,t){var c=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;if(0===r.Children.count(e.props.children))return e;var o=[];r.Children.forEach(e.props.children,(function(e){"string"==typeof e?o.push.apply(o,i(e,t,c)):r.isValidElement(e)?"string"==typeof e.type&&t.ignoreTags.indexOf(e.type.toUpperCase())>=0?o.push(e):o.push(a(e,t,c+1)):o.push(e)}));var l=n.options.assign({key:"__linkify-el-".concat(c)},e.props);return r.cloneElement(e,l,o)}return function(e){var t=0,i={key:"__linkify-wrapper"};for(var c in e)"options"!==c&&"tagName"!==c&&"children"!==c&&(i[c]=e[c]);var o=new n.Options(e.options,(function(e){var n=e.tagName,i=e.attributes,a=e.content;return i.key="__linkify-lnk-".concat(++t),i.class&&(i.className=i.class,delete i.class),r.createElement(n,i,a)})),l=e.tagName||r.Fragment||"span",s=e.children;return a(r.createElement(l,i,s),o,0)}}(React,linkify); |
import * as React from 'react'; | ||
import { Options, tokenize } from 'linkifyjs'; | ||
import { Options, options, tokenize } from 'linkifyjs'; | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
/** | ||
@@ -41,10 +8,10 @@ * Given a string, converts to an array of valid React components | ||
* @param {string} str | ||
* @param {any} opts | ||
* @param {Options} opts | ||
* @returns {React.ReactNodeArray} | ||
*/ | ||
function stringToElements(str, opts) { | ||
function stringToElements(str, opts, parentElementId) { | ||
var tokens = tokenize(str); | ||
var elements = []; | ||
var linkId = 0; | ||
var nlId = 0; | ||
@@ -54,48 +21,12 @@ for (var i = 0; i < tokens.length; i++) { | ||
if (token.t === 'nl' && opts.nl2br) { | ||
if (token.t === 'nl' && opts.get('nl2br')) { | ||
elements.push(React.createElement('br', { | ||
key: "linkified-".concat(++linkId) | ||
key: "__linkify-el-".concat(parentElementId, "-nl-").concat(nlId++) | ||
})); | ||
continue; | ||
} else if (!token.isLink || !opts.check(token)) { | ||
// Regular text | ||
elements.push(token.toString()); | ||
continue; | ||
} else { | ||
elements.push(opts.render(token)); | ||
} | ||
var _opts$resolve = opts.resolve(token), | ||
formatted = _opts$resolve.formatted, | ||
formattedHref = _opts$resolve.formattedHref, | ||
tagName = _opts$resolve.tagName, | ||
className = _opts$resolve.className, | ||
target = _opts$resolve.target, | ||
rel = _opts$resolve.rel, | ||
attributes = _opts$resolve.attributes; | ||
var props = { | ||
key: "linkified-".concat(++linkId), | ||
href: formattedHref | ||
}; | ||
if (className) { | ||
props.className = className; | ||
} | ||
if (target) { | ||
props.target = target; | ||
} | ||
if (rel) { | ||
props.rel = rel; | ||
} // Build up additional attributes | ||
// Support for events via attributes hash | ||
if (attributes) { | ||
for (var attr in attributes) { | ||
props[attr] = attributes[attr]; | ||
} | ||
} | ||
elements.push(React.createElement(tagName, props, formatted)); | ||
} | ||
@@ -110,3 +41,3 @@ | ||
* @param {React.ReactElement<P, T>} element | ||
* @param {Object} opts | ||
* @param {Options} opts | ||
* @param {number} elementId | ||
@@ -129,4 +60,3 @@ * @returns {React.ReactElement<P, T>} | ||
// ensure that we always generate unique element IDs for keys | ||
elementId = elementId + 1; | ||
children.push.apply(children, _toConsumableArray(stringToElements(child, opts))); | ||
children.push.apply(children, stringToElements(child, opts, elementId)); | ||
} else if (React.isValidElement(child)) { | ||
@@ -137,3 +67,3 @@ if (typeof child.type === 'string' && opts.ignoreTags.indexOf(child.type.toUpperCase()) >= 0) { | ||
} else { | ||
children.push(linkifyReactElement(child, opts, ++elementId)); | ||
children.push(linkifyReactElement(child, opts, elementId + 1)); | ||
} | ||
@@ -146,10 +76,5 @@ } else { | ||
var newProps = { | ||
key: "linkified-element-".concat(elementId) | ||
}; | ||
for (var prop in element.props) { | ||
newProps[prop] = element.props[prop]; | ||
} | ||
var newProps = options.assign({ | ||
key: "__linkify-el-".concat(elementId) | ||
}, element.props); | ||
return React.cloneElement(element, newProps, children); | ||
@@ -167,4 +92,20 @@ } | ||
// Copy over all non-linkify-specific props | ||
var linkId = 0; | ||
var defaultLinkRender = function defaultLinkRender(_ref) { | ||
var tagName = _ref.tagName, | ||
attributes = _ref.attributes, | ||
content = _ref.content; | ||
attributes.key = "__linkify-lnk-".concat(++linkId); | ||
if (attributes.class) { | ||
attributes.className = attributes.class; | ||
delete attributes.class; | ||
} | ||
return React.createElement(tagName, attributes, content); | ||
}; | ||
var newProps = { | ||
key: 'linkified-element-wrapper' | ||
key: '__linkify-wrapper' | ||
}; | ||
@@ -178,3 +119,3 @@ | ||
var opts = new Options(props.options); | ||
var opts = new Options(props.options, defaultLinkRender); | ||
var tagName = props.tagName || React.Fragment || 'span'; | ||
@@ -181,0 +122,0 @@ var children = props.children; |
151
index.js
@@ -7,17 +7,17 @@ 'use strict'; | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
} | ||
@@ -27,35 +27,2 @@ | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
/** | ||
@@ -65,10 +32,10 @@ * Given a string, converts to an array of valid React components | ||
* @param {string} str | ||
* @param {any} opts | ||
* @param {Options} opts | ||
* @returns {React.ReactNodeArray} | ||
*/ | ||
function stringToElements(str, opts) { | ||
function stringToElements(str, opts, parentElementId) { | ||
var tokens = linkifyjs.tokenize(str); | ||
var elements = []; | ||
var linkId = 0; | ||
var nlId = 0; | ||
@@ -78,48 +45,12 @@ for (var i = 0; i < tokens.length; i++) { | ||
if (token.t === 'nl' && opts.nl2br) { | ||
if (token.t === 'nl' && opts.get('nl2br')) { | ||
elements.push(React__namespace.createElement('br', { | ||
key: "linkified-".concat(++linkId) | ||
key: "__linkify-el-".concat(parentElementId, "-nl-").concat(nlId++) | ||
})); | ||
continue; | ||
} else if (!token.isLink || !opts.check(token)) { | ||
// Regular text | ||
elements.push(token.toString()); | ||
continue; | ||
} else { | ||
elements.push(opts.render(token)); | ||
} | ||
var _opts$resolve = opts.resolve(token), | ||
formatted = _opts$resolve.formatted, | ||
formattedHref = _opts$resolve.formattedHref, | ||
tagName = _opts$resolve.tagName, | ||
className = _opts$resolve.className, | ||
target = _opts$resolve.target, | ||
rel = _opts$resolve.rel, | ||
attributes = _opts$resolve.attributes; | ||
var props = { | ||
key: "linkified-".concat(++linkId), | ||
href: formattedHref | ||
}; | ||
if (className) { | ||
props.className = className; | ||
} | ||
if (target) { | ||
props.target = target; | ||
} | ||
if (rel) { | ||
props.rel = rel; | ||
} // Build up additional attributes | ||
// Support for events via attributes hash | ||
if (attributes) { | ||
for (var attr in attributes) { | ||
props[attr] = attributes[attr]; | ||
} | ||
} | ||
elements.push(React__namespace.createElement(tagName, props, formatted)); | ||
} | ||
@@ -134,3 +65,3 @@ | ||
* @param {React.ReactElement<P, T>} element | ||
* @param {Object} opts | ||
* @param {Options} opts | ||
* @param {number} elementId | ||
@@ -153,4 +84,3 @@ * @returns {React.ReactElement<P, T>} | ||
// ensure that we always generate unique element IDs for keys | ||
elementId = elementId + 1; | ||
children.push.apply(children, _toConsumableArray(stringToElements(child, opts))); | ||
children.push.apply(children, stringToElements(child, opts, elementId)); | ||
} else if (React__namespace.isValidElement(child)) { | ||
@@ -161,3 +91,3 @@ if (typeof child.type === 'string' && opts.ignoreTags.indexOf(child.type.toUpperCase()) >= 0) { | ||
} else { | ||
children.push(linkifyReactElement(child, opts, ++elementId)); | ||
children.push(linkifyReactElement(child, opts, elementId + 1)); | ||
} | ||
@@ -170,10 +100,5 @@ } else { | ||
var newProps = { | ||
key: "linkified-element-".concat(elementId) | ||
}; | ||
for (var prop in element.props) { | ||
newProps[prop] = element.props[prop]; | ||
} | ||
var newProps = linkifyjs.options.assign({ | ||
key: "__linkify-el-".concat(elementId) | ||
}, element.props); | ||
return React__namespace.cloneElement(element, newProps, children); | ||
@@ -191,4 +116,20 @@ } | ||
// Copy over all non-linkify-specific props | ||
var linkId = 0; | ||
var defaultLinkRender = function defaultLinkRender(_ref) { | ||
var tagName = _ref.tagName, | ||
attributes = _ref.attributes, | ||
content = _ref.content; | ||
attributes.key = "__linkify-lnk-".concat(++linkId); | ||
if (attributes.class) { | ||
attributes.className = attributes.class; | ||
delete attributes.class; | ||
} | ||
return React__namespace.createElement(tagName, attributes, content); | ||
}; | ||
var newProps = { | ||
key: 'linkified-element-wrapper' | ||
key: '__linkify-wrapper' | ||
}; | ||
@@ -202,3 +143,3 @@ | ||
var opts = new linkifyjs.Options(props.options); | ||
var opts = new linkifyjs.Options(props.options, defaultLinkRender); | ||
var tagName = props.tagName || React__namespace.Fragment || 'span'; | ||
@@ -205,0 +146,0 @@ var children = props.children; |
{ | ||
"name": "linkify-react", | ||
"version": "3.0.4", | ||
"version": "4.0.0-beta.1", | ||
"description": "React element interface for linkifyjs", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"build": "rollup -c rollup.config.js", | ||
"clean": "rm -rf lib dist *.tgz *.d.ts", | ||
"clean": "rm -rf lib dist index.js *.tgz *.d.ts", | ||
"prepack": "run-s clean build tsc", | ||
@@ -32,5 +32,5 @@ "tsc": "tsc", | ||
}, | ||
"homepage": "https://github.com/Hypercontext/linkifyjs#readme", | ||
"homepage": "https://linkify.js.org", | ||
"peerDependencies": { | ||
"linkifyjs": "^3.0.0", | ||
"linkifyjs": "*", | ||
"react": ">= 15.0.0" | ||
@@ -37,0 +37,0 @@ }, |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9
12.5%16819
-25.22%368
-25.2%2
100%