Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';