From a0177115c59a157c13ff8654ac68c1f54fb434e6 Mon Sep 17 00:00:00 2001 From: Olivier Date: Sat, 4 Jan 2020 19:04:55 +0100 Subject: [PATCH] Add anchor link for metadata values that match valid URLs --- client/src/components/listViewHelpers.js | 23 ++++++++++++++++++----- client/src/utils/string.js | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 client/src/utils/string.js diff --git a/client/src/components/listViewHelpers.js b/client/src/components/listViewHelpers.js index ccd096a..8fe3e74 100644 --- a/client/src/components/listViewHelpers.js +++ b/client/src/components/listViewHelpers.js @@ -1,16 +1,29 @@ import _ from 'lodash'; import React from 'react'; import fromNow from '../utils/dates'; +import {isAValidURL} from '../utils/string'; import Loading from './loading'; import Sorter, {sortByDate} from './sorter'; import ResourceSvg from '../art/resourceSvg'; export function objectMap(items = {}) { - return Object.entries(items).map(([key, value]) => ( -
- {key}{value.length <= 50 ? value : `${value.substr(0, 50)}...`} -
- )); + return Object.entries(items).map(([key, value]) => { + const substrValue = value.length <= 50 ? value : `${value.substr(0, 50)}...`; + + if (isAValidURL(value)) { + // Check valid URL + return
+ {key}{substrValue} +
+ } + // Note : alternative parsing could be implemented in other if clauses + else { + // By default, just display the raw value in a span + return
+ {key} {substrValue} +
+ } + }); } export function TableBody({items, filter, colSpan, sort, row}) { diff --git a/client/src/utils/string.js b/client/src/utils/string.js new file mode 100644 index 0000000..c656967 --- /dev/null +++ b/client/src/utils/string.js @@ -0,0 +1,23 @@ +/** + * Regular expression for URL validation created by @dperini. + * https://gist.github.com/dperini/729294 + * (copy pasted from the official Kubernetes dashboard) + */ +const URL_REGEXP = new RegExp( + '^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!' + + '(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1' + + ',3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])' + + '){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*' + + '[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]' + + '+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$', + 'i', +); + +/** +* Check whether a string is a valid URL +*/ +export function isAValidURL(str) { + return URL_REGEXP.test(str.trim()); +} + +/** ... Add other String utilities functions here */ \ No newline at end of file