import { escape , isFunction } from 'lodash' ; import { IFieldFormat , HtmlContextTypeConvert , FieldFormatsContentType } from '../types' ; import { asPrettyString , getHighlightHtml } from '../utils' ; export const HTML_CONTEXT_TYPE = 'html' ; const getConvertFn = ( format , convert ? ) : HtmlContextTypeConvert => { const fallbackHtml = ( value , options = { } ) => { const { field , hit } = options ; const formatted = escape ( format . convert ( value , 'text' ) ) ; return ! field || ! hit || ! hit . highlight || ! hit . highlight [ field . name ] ? formatted : getHighlightHtml ( formatted , hit . highlight [ field . name ] ) ; } ; return ( convert || fallbackHtml ) as HtmlContextTypeConvert ; } ; export const setup = ( format , htmlContextTypeConvert ? ) : HtmlContextTypeConvert => { const convert = getConvertFn ( format , htmlContextTypeConvert ) ; const recurse = ( value , options = { } ) => { if ( value == null ) { return asPrettyString ( value ) ; } if ( ! value || ! isFunction ( value . map ) ) { return convert . call ( format , value , options ) ; } const subValues = value . map ( ( v ) => recurse ( v , options ) ) ; const useMultiLine = subValues . some ( ( sub ) => sub . indexOf ( '\n' ) > - 1 ) ; return subValues . join ( ',' + ( useMultiLine ? '\n' : ' ' ) ) ; } ; const wrap = ( value , options ) => { return `<span ng-non-bindable>${ recurse ( value , options ) }</span>` ; } ; return wrap ; } ;