redoc/lib/utils/pipes.ts

143 lines
4.0 KiB
TypeScript
Raw Normal View History

2015-10-08 20:16:45 +03:00
'use strict';
2016-06-13 20:54:24 +03:00
import { Pipe, PipeTransform } from '@angular/core';
2017-02-27 17:27:57 +03:00
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { isString, stringify, isBlank } from './helpers';
import JsonPointer from './JsonPointer';
2016-10-31 11:15:04 +03:00
import { MdRenderer } from './';
2016-09-02 23:18:31 +03:00
import { JsonFormatter } from './JsonFormatterPipe';
import { OptionsService } from '../services/options.service';
declare var Prism: any;
2016-09-02 23:18:31 +03:00
class BaseException {
message: string;
constructor(message) {
this.message = message;
}
}
2015-12-14 19:31:17 +03:00
class InvalidPipeArgumentException extends BaseException {
constructor(type, value) {
super(`Invalid argument '${value}' for pipe '${stringify(type)}'`);
}
}
2015-10-08 20:16:45 +03:00
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'keys' })
2016-06-13 20:54:24 +03:00
export class KeysPipe implements PipeTransform {
2015-12-14 19:31:17 +03:00
transform(value) {
if (isBlank(value)) return value;
if (typeof value !== 'object') {
2016-10-31 22:09:55 +03:00
throw new InvalidPipeArgumentException(KeysPipe, value);
2015-12-14 19:14:14 +03:00
}
2015-12-14 19:31:17 +03:00
return Object.keys(value);
2015-10-08 20:16:45 +03:00
}
}
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'jsonPointerEscape' })
2016-06-13 20:54:24 +03:00
export class JsonPointerEscapePipe implements PipeTransform {
2016-08-31 20:20:11 +03:00
transform(value:string) {
2015-12-14 19:31:17 +03:00
if (isBlank(value)) return value;
2015-12-14 19:14:14 +03:00
if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
}
return JsonPointer.escape(value);
2015-10-08 20:16:45 +03:00
}
}
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'marked' })
2016-06-13 20:54:24 +03:00
export class MarkedPipe implements PipeTransform {
2016-10-30 18:56:24 +03:00
renderer: MdRenderer;
unstrustedSpec: boolean;
constructor(private sanitizer: DomSanitizer, optionsService: OptionsService) {
2016-10-30 18:56:24 +03:00
this.renderer = new MdRenderer(true);
this.unstrustedSpec = !!optionsService.options.untrustedSpec;
2016-10-30 18:56:24 +03:00
}
2016-08-31 20:20:11 +03:00
transform(value:string) {
2015-12-14 19:31:17 +03:00
if (isBlank(value)) return value;
2015-12-14 19:14:14 +03:00
if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
}
let res = `<span class="redoc-markdown-block">${this.renderer.renderMd(value)}</span>`;
return this.unstrustedSpec ? res : this.sanitizer.bypassSecurityTrustHtml(res);
}
}
2016-01-31 20:37:35 +03:00
@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
2016-09-02 23:18:31 +03:00
constructor(private sanitizer: DomSanitizer) {}
2017-02-27 17:27:57 +03:00
transform(value:string|SafeHtml):SafeHtml {
if (isBlank(value)) return value;
if (!isString(value)) {
2017-02-27 17:27:57 +03:00
return value;
}
2017-02-27 17:27:57 +03:00
return this.sanitizer.bypassSecurityTrustHtml(value as string);
}
}
2016-01-31 20:37:35 +03:00
const langMap = {
'c++': 'cpp',
'c#': 'csharp',
'objective-c': 'objectivec',
'shell': 'bash',
'viml': 'vim'
};
@Pipe({ name: 'prism' })
2016-06-13 20:54:24 +03:00
export class PrismPipe implements PipeTransform {
2016-09-02 23:18:31 +03:00
constructor(private sanitizer: DomSanitizer) {}
2016-01-31 20:37:35 +03:00
transform(value, args) {
if (isBlank(args) || args.length === 0) {
throw new BaseException('Prism pipe requires one argument');
}
if (isBlank(value)) return value;
if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
}
let lang = args[0].toString().trim().toLowerCase();
if (langMap[lang]) lang = langMap[lang];
let grammar = Prism.languages[lang];
//fallback to clike
if (!grammar) grammar = Prism.languages.clike;
2016-06-22 12:42:03 +03:00
return this.sanitizer.bypassSecurityTrustHtml(Prism.highlight(value, grammar));
2016-01-31 20:37:35 +03:00
}
}
2016-03-09 21:44:20 +03:00
@Pipe({ name: 'encodeURIComponent' })
2016-06-13 20:54:24 +03:00
export class EncodeURIComponentPipe implements PipeTransform {
2016-08-31 20:20:11 +03:00
transform(value:string) {
2016-03-09 21:44:20 +03:00
if (isBlank(value)) return value;
if (!isString(value)) {
throw new InvalidPipeArgumentException(EncodeURIComponentPipe, value);
}
return encodeURIComponent(value);
}
}
2016-08-22 12:12:13 +03:00
2016-11-28 17:15:09 +03:00
const COLLECTION_FORMATS = {
csv: 'Comma Separated',
ssv: 'Space Separated',
tsv: 'Tab Separated',
pipes: 'Pipe Separated'
};
@Pipe({ name: 'collectionFormat' })
export class CollectionFormatPipe implements PipeTransform {
transform(param:any) {
let format = param.collectionFormat;
if (!format) format = 'csv';
if (format === 'multi') {
return 'Multiple ' + param.in + ' params of';
}
return COLLECTION_FORMATS[format];
}
}
2016-08-22 12:12:13 +03:00
export const REDOC_PIPES = [
2016-11-28 17:15:09 +03:00
JsonPointerEscapePipe, MarkedPipe, SafePipe, PrismPipe, EncodeURIComponentPipe, JsonFormatter, KeysPipe, CollectionFormatPipe
2016-08-22 12:12:13 +03:00
];