2015-10-08 20:16:45 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-13 20:54:24 +03:00
|
|
|
import { Pipe, PipeTransform } from '@angular/core';
|
2016-06-22 12:42:03 +03:00
|
|
|
import { DomSanitizationService } from '@angular/platform-browser';
|
2016-06-13 20:54:24 +03:00
|
|
|
import { isString, stringify, isBlank } from '@angular/core/src/facade/lang';
|
|
|
|
import { BaseException } from '@angular/core/src/facade/exceptions';
|
2016-05-25 18:34:31 +03:00
|
|
|
import JsonPointer from './JsonPointer';
|
2016-07-21 13:35:27 +03:00
|
|
|
import { renderMd } from './helpers';
|
2016-02-10 14:19:50 +03:00
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
declare var Prism: any;
|
2016-03-28 00:54:43 +03:00
|
|
|
|
2016-02-11 14:38:44 +03:00
|
|
|
|
2015-10-23 12:03:47 +03:00
|
|
|
|
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') {
|
|
|
|
throw new InvalidPipeArgumentException(ValuesPipe, 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: 'values' })
|
2016-06-13 20:54:24 +03:00
|
|
|
export class ValuesPipe implements PipeTransform {
|
2015-12-14 19:14:14 +03:00
|
|
|
transform(value) {
|
2015-12-14 19:31:17 +03:00
|
|
|
if (isBlank(value)) return value;
|
2015-12-14 19:14:14 +03:00
|
|
|
if (typeof value !== 'object') {
|
|
|
|
throw new InvalidPipeArgumentException(ValuesPipe, value);
|
|
|
|
}
|
|
|
|
return Object.keys(value).map(key => value[key]);
|
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 {
|
2015-12-14 19:14:14 +03:00
|
|
|
transform(value) {
|
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-10-23 12:03:47 +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-06-22 12:42:03 +03:00
|
|
|
constructor(private sanitizer: DomSanitizationService) {}
|
2015-12-14 19:14:14 +03:00
|
|
|
transform(value) {
|
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);
|
|
|
|
}
|
2016-06-22 12:42:03 +03:00
|
|
|
|
|
|
|
return this.sanitizer.bypassSecurityTrustHtml(
|
2016-07-21 13:35:27 +03:00
|
|
|
`<span class="redoc-markdown-block">${renderMd(value)}</span>`
|
2016-06-22 12:42:03 +03:00
|
|
|
);
|
2015-10-23 12:03:47 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 20:37:35 +03:00
|
|
|
|
2016-07-21 13:35:27 +03:00
|
|
|
@Pipe({ name: 'safe' })
|
|
|
|
export class SafePipe implements PipeTransform {
|
|
|
|
constructor(private sanitizer: DomSanitizationService) {}
|
|
|
|
transform(value) {
|
|
|
|
if (isBlank(value)) return value;
|
|
|
|
if (!isString(value)) {
|
|
|
|
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.sanitizer.bypassSecurityTrustHtml(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-22 12:42:03 +03:00
|
|
|
constructor(private sanitizer: DomSanitizationService) {}
|
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-03-09 21:44:20 +03:00
|
|
|
transform(value) {
|
|
|
|
if (isBlank(value)) return value;
|
|
|
|
if (!isString(value)) {
|
|
|
|
throw new InvalidPipeArgumentException(EncodeURIComponentPipe, value);
|
|
|
|
}
|
|
|
|
return encodeURIComponent(value);
|
|
|
|
}
|
|
|
|
}
|