140 lines
4.0 KiB
TypeScript
140 lines
4.0 KiB
TypeScript
|
/**
|
||
|
* @author Martin Karkowski
|
||
|
* @email m.karkowski@zema.de
|
||
|
* @create date 2018-05-22 12:39:32
|
||
|
* @modify date 2020-09-08 16:50:49
|
||
|
* @desc [description]
|
||
|
*/
|
||
|
|
||
|
import { injectable } from 'inversify';
|
||
|
import { Converter } from 'showdown';
|
||
|
import { flattenObjectType, isObject, rgetattr, rsetattr } from '../../../lib/helpers/objectMethods';
|
||
|
import { AvailableFromConvertes, AvailableToConvertes, IConverter, IHtmlConfiguration } from '../type/interfaces';
|
||
|
|
||
|
|
||
|
@injectable()
|
||
|
export class ToHTMLConverter implements IConverter<any, string, IHtmlConfiguration> {
|
||
|
|
||
|
public maxJSONLength = 1024 * 100;
|
||
|
|
||
|
public readonly name: AvailableFromConvertes | AvailableToConvertes;
|
||
|
/**
|
||
|
* Container for the Registered Topics
|
||
|
*
|
||
|
* @private
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
private _convertOptions = new Map<string, IHtmlConfiguration>();
|
||
|
|
||
|
/**
|
||
|
* A Mark-Down - Converter
|
||
|
*
|
||
|
* @private
|
||
|
* @type {Converter}
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
private _markDownConverter: Converter;
|
||
|
|
||
|
/**
|
||
|
* Register only Endpoints.
|
||
|
*
|
||
|
* @param {string} identifier The Identifier for the Convertert
|
||
|
* @param {IHtmlConfiguration} config The Config to use.
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
register(identifier: string, config: IHtmlConfiguration): void {
|
||
|
this._convertOptions.set(identifier, config);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*
|
||
|
* @param {*} data The Data to use.
|
||
|
* @param {string} identifier the Identifier to convert the Data
|
||
|
* @param {number} _timeStamp the given TimeStamp.
|
||
|
* @returns {string} the Converted Result
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
convert(data: any, identifier: string | null = null, timeStamp?: number): string {
|
||
|
if (identifier !== null) {
|
||
|
const _config = this._convertOptions.get(identifier) || 'default';
|
||
|
return this[_config](data);
|
||
|
}
|
||
|
throw TypeError('Identifier must be provided for the To-HTML-Converter')
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function for a Picture
|
||
|
*
|
||
|
* @private
|
||
|
* @param {*} data Converts Data to A Picture
|
||
|
* @returns {string} the Picture as HTML Element
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
private picture(data: any): string {
|
||
|
|
||
|
const _pic = '<img max-width="1024" max-height="1024" alt="Picture" src="data:image/png'
|
||
|
/*+ data.type*/
|
||
|
+ ';base64,'
|
||
|
+ data.rawDataBase64
|
||
|
+ '">';
|
||
|
|
||
|
return _pic;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function as A default converter
|
||
|
*
|
||
|
* @private
|
||
|
* @param {*} _data converts Data to JSON
|
||
|
* @returns {string} the Data as JSON
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
private default(_data: any): string {
|
||
|
/** Determine the Type */
|
||
|
|
||
|
let _content: string = null;
|
||
|
|
||
|
if (isObject(_data) || Array.isArray(_data)) {
|
||
|
|
||
|
/** Based on that Copy extract the Types */
|
||
|
const _types = flattenObjectType(_data);
|
||
|
|
||
|
for (const [_key, _type] of _types.entries()) {
|
||
|
const _content = (rgetattr(_data, _key) as string);
|
||
|
if (_type === 'string' && typeof _content === 'string') {
|
||
|
rsetattr(_data, _key, _content.slice(0, 100) + ((_content.length > 100) ? ' ...' : ''));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_content = JSON.stringify(_data, undefined, 4);
|
||
|
|
||
|
} else if (typeof _data === 'string') {
|
||
|
_content = _data;
|
||
|
} else {
|
||
|
_content = _data.toString();
|
||
|
}
|
||
|
|
||
|
if (_content.length > this.maxJSONLength) {
|
||
|
_content = _content.slice(0, this.maxJSONLength) + '\n...'
|
||
|
}
|
||
|
|
||
|
return this._markDownConverter.makeHtml(
|
||
|
'```json\n' +
|
||
|
/** Return the Raw-Data */
|
||
|
_content +
|
||
|
'\n```'
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Creates an instance of ToHTMLConverter.
|
||
|
* @memberof ToHTMLConverter
|
||
|
*/
|
||
|
constructor() {
|
||
|
/** Set the Name of the Converter */
|
||
|
this.name = 'to-html';
|
||
|
this._markDownConverter = new Converter();
|
||
|
}
|
||
|
}
|