@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
40 lines • 2.06 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
// credits to https://www.regextester.com/96504, modified though
const URL_REGEX =
// eslint-disable-next-line
/(?:https?|s?ftp|bolt):\/\/(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?/gi;
const ClickableUrlsComponent = ({ text }) => {
var _a;
const definedText = text !== null && text !== void 0 ? text : '';
const urls = (_a = definedText.match(URL_REGEX)) !== null && _a !== void 0 ? _a : [];
return (_jsx(_Fragment, { children: definedText.split(URL_REGEX).map((t, index) => {
/* since we never move these components this key should be fine */
return (_jsxs(React.Fragment, { children: [t, urls[index] && (
// Should be safe from XSS.
// Ref: https://mathiasbynens.github.io/rel-noopener/
_jsx("a", { href: urls[index], target: "_blank", rel: "noopener noreferrer", className: "hover:underline", children: urls[index] }))] }, `clickable-url-${index}`));
}) }));
};
export const ClickableUrls = React.memo(ClickableUrlsComponent);
//# sourceMappingURL=clickable-urls.js.map