Skip to content

Commit

Permalink
[Discover] Improve HTML formatting of fields with a list of values (#…
Browse files Browse the repository at this point in the history
…136684)

* [Discover] Improve HTML formatting of fields with a list of values

* [Discover] Add support for highlighting array brackets and commas

* [Discover] Update array field format to use a CSS class instead of inline styles

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
davismcphee and kibanamachine authored Jul 25, 2022
1 parent bd5abb1 commit a37aae2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const setup = (
htmlContextTypeConvert?: HtmlContextTypeConvert
): HtmlContextTypeConvert => {
const convert = getConvertFn(format, htmlContextTypeConvert);
const highlight = (text: string) => `<span class="ffArray__highlight">${text}</span>`;

const recurse: HtmlContextTypeConvert = (value, options = {}) => {
if (value == null) {
Expand All @@ -46,11 +47,23 @@ export const setup = (
const subValues = value.map((v: unknown) => recurse(v, options));
const useMultiLine = subValues.some((sub: string) => sub.indexOf('\n') > -1);

return subValues.join(',' + (useMultiLine ? '\n' : ' '));
return subValues.join(highlight(',') + (useMultiLine ? '\n' : ' '));
};

const wrap: HtmlContextTypeConvert = (value, options) => {
return recurse(value, options);
const convertedValue = recurse(value, options);

if (!Array.isArray(value) || value.length < 2) {
return convertedValue;
}

if (convertedValue.includes('\n')) {
const indentedValue = convertedValue.replaceAll(/(\n+)/g, '$1 ');

return highlight('[') + `\n ${indentedValue}\n` + highlight(']');
}

return highlight('[') + convertedValue + highlight(']');
};

return wrap;
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/field_formats/common/field_format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@ describe('FieldFormat class', () => {

expect(f.convert(['one', 'two', 'three'])).toBe('["one","two","three"]');
});

test('formats a list of values as html', () => {
const f = getTestFormat();

expect(f.convert([123, 456, 789], 'html')).toMatchInlineSnapshot(
`"<span class=\\"ffArray__highlight\\">[</span>123<span class=\\"ffArray__highlight\\">,</span> 456<span class=\\"ffArray__highlight\\">,</span> 789<span class=\\"ffArray__highlight\\">]</span>"`
);
});

test('formats a list of values containing newlines as html', () => {
const f = getTestFormat();
const newlineList = [
'{\n "foo": "bar",\n "fizz": "buzz"\n}',
'{\n "bar": "foo",\n "buzz": "fizz"\n}',
];

expect(f.convert(newlineList, 'html')).toMatchInlineSnapshot(`
"<span class=\\"ffArray__highlight\\">[</span>
{
&quot;foo&quot;: &quot;bar&quot;,
&quot;fizz&quot;: &quot;buzz&quot;
}<span class=\\"ffArray__highlight\\">,</span>
{
&quot;bar&quot;: &quot;foo&quot;,
&quot;buzz&quot;: &quot;fizz&quot;
}
<span class=\\"ffArray__highlight\\">]</span>"
`);
});
});
});
});
1 change: 1 addition & 0 deletions src/plugins/field_formats/public/index.scss
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@import './lib/converters/index';
@import './lib/content_types/index';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.ffArray__highlight {
color: $euiColorMediumShade;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './html_content_type';

0 comments on commit a37aae2

Please sign in to comment.