-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] return source field format
- Loading branch information
Showing
5 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/plugins/field_formats/common/converters/source.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { SourceFormat } from './source'; | ||
import { HtmlContextTypeConvert } from '../types'; | ||
import { HTML_CONTEXT_TYPE } from '../content_types'; | ||
|
||
describe('Source Format', () => { | ||
let convertHtml: Function; | ||
|
||
beforeEach(() => { | ||
const source = new SourceFormat({}, jest.fn()); | ||
|
||
convertHtml = source.getConverterFor(HTML_CONTEXT_TYPE) as HtmlContextTypeConvert; | ||
}); | ||
|
||
test('should use the text content type if a field is not passed', () => { | ||
const hit = { | ||
foo: 'bar', | ||
number: 42, | ||
hello: '<h1>World</h1>', | ||
also: 'with "quotes" or \'single quotes\'', | ||
}; | ||
|
||
expect(convertHtml(hit)).toBe( | ||
'{"foo":"bar","number":42,"hello":"<h1>World</h1>","also":"with \\"quotes\\" or 'single quotes'"}' | ||
); | ||
}); | ||
|
||
test('should render a description list if a field is passed', () => { | ||
const hit = { | ||
foo: 'bar', | ||
number: 42, | ||
hello: '<h1>World</h1>', | ||
also: 'with "quotes" or \'single quotes\'', | ||
}; | ||
|
||
expect( | ||
convertHtml(hit, { field: 'field', indexPattern: { formatHit: (h: string) => h }, hit }) | ||
).toMatchInlineSnapshot( | ||
`"<dl class=\\"source truncate-by-height\\"><dt>foo:</dt><dd>bar</dd> <dt>number:</dt><dd>42</dd> <dt>hello:</dt><dd><h1>World</h1></dd> <dt>also:</dt><dd>with \\"quotes\\" or 'single quotes'</dd> </dl>"` | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { KBN_FIELD_TYPES } from '@kbn/field-types'; | ||
import React, { Fragment } from 'react'; | ||
import ReactDOM from 'react-dom/server'; | ||
import { escape, keys } from 'lodash'; | ||
import { shortenDottedString } from '../utils'; | ||
import { FieldFormat } from '../field_format'; | ||
import { TextContextTypeConvert, HtmlContextTypeConvert, FIELD_FORMAT_IDS } from '../types'; | ||
import { FORMATS_UI_SETTINGS } from '../constants/ui_settings'; | ||
|
||
interface Props { | ||
defPairs: Array<[string, string]>; | ||
} | ||
const TemplateComponent = ({ defPairs }: Props) => { | ||
return ( | ||
<dl className={'source'}> | ||
{defPairs.map((pair, idx) => ( | ||
<Fragment key={idx}> | ||
<dt | ||
dangerouslySetInnerHTML={{ __html: `${escape(pair[0])}:` }} // eslint-disable-line react/no-danger | ||
/> | ||
<dd | ||
dangerouslySetInnerHTML={{ __html: `${pair[1]}` }} // eslint-disable-line react/no-danger | ||
/>{' '} | ||
</Fragment> | ||
))} | ||
</dl> | ||
); | ||
}; | ||
|
||
/** @public */ | ||
export class SourceFormat extends FieldFormat { | ||
static id = FIELD_FORMAT_IDS._SOURCE; | ||
static title = '_source'; | ||
static fieldType = KBN_FIELD_TYPES._SOURCE; | ||
|
||
textConvert: TextContextTypeConvert = (value: string) => JSON.stringify(value); | ||
|
||
htmlConvert: HtmlContextTypeConvert = (value: string, options = {}) => { | ||
const { field, hit, indexPattern } = options; | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('_source converter'); | ||
if (!field) { | ||
const converter = this.getConverterFor('text') as Function; | ||
|
||
return escape(converter(value)); | ||
} | ||
|
||
const highlights: Record<string, string[]> = (hit && hit.highlight) || {}; | ||
// TODO: remove index pattern dependency | ||
const formatted = hit ? indexPattern!.formatHit(hit) : {}; | ||
const highlightPairs: Array<[string, string]> = []; | ||
const sourcePairs: Array<[string, string]> = []; | ||
const isShortDots = this.getConfig!(FORMATS_UI_SETTINGS.SHORT_DOTS_ENABLE); | ||
|
||
keys(formatted).forEach((key) => { | ||
const pairs = highlights[key] ? highlightPairs : sourcePairs; | ||
const newField = isShortDots ? shortenDottedString(key) : key; | ||
const val = formatted![key]; | ||
pairs.push([newField as string, val]); | ||
}, []); | ||
|
||
return ReactDOM.renderToStaticMarkup( | ||
<TemplateComponent defPairs={highlightPairs.concat(sourcePairs)} /> | ||
); | ||
}; | ||
} |