Skip to content

Commit

Permalink
Signed-off-by: Matus Majchrak <matus.majchrak@gmail.com>
Browse files Browse the repository at this point in the history
Updates regex and merge json/markup functions
  • Loading branch information
Matus Majchrak committed Aug 9, 2019
1 parent 907aefe commit 57abda4
Showing 1 changed file with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,18 @@ import { KeyValuePair, Link } from '../../../../types/trace';

import './KeyValuesTable.css';

const jsonObjectOrArrayStartRegex = new RegExp('^[{[]+');
const jsonObjectOrArrayStartRegex = '/^({|[)/';

function parseIfJson(value: any) {
try {
return JSON.parse(value);
// eslint-disable-next-line no-empty
} catch (_) {}
return value;
}

function createMarkup(value: any) {
function parseIfComplexJson(value: any) {
// if the value is a string representing actual json object or array, then use json-markup
if (typeof value === 'string' && jsonObjectOrArrayStartRegex.test(value)) {
return jsonMarkup(parseIfJson(value));
if (typeof value === 'string' && jsonObjectOrArrayStartRegex.match(value)) {
// otherwise just return as is
try {
return JSON.parse(value);
// eslint-disable-next-line no-empty
} catch (_) {}
}
// otherwise just return as is
return jsonMarkup(value);
return value;
}

export const LinkValue = (props: { href: string; title?: string; children: React.ReactNode }) => (
Expand Down Expand Up @@ -77,7 +72,7 @@ export default function KeyValuesTable(props: KeyValuesTableProps) {
<tbody className="KeyValueTable--body">
{data.map((row, i) => {
const markup = {
__html: createMarkup(row.value),
__html: jsonMarkup(parseIfComplexJson(row.value)),
};
// eslint-disable-next-line react/no-danger
const jsonTable = <div className="ub-inline-block" dangerouslySetInnerHTML={markup} />;
Expand Down

0 comments on commit 57abda4

Please sign in to comment.