Replace marked with remarkable

This commit is contained in:
Roman Hotsiy 2016-06-22 12:42:03 +03:00
parent 791355c60a
commit b65370c8ac
5 changed files with 50 additions and 25 deletions

View File

@ -1,27 +1,27 @@
'use strict'; 'use strict';
import { Pipe, PipeTransform } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizationService } from '@angular/platform-browser';
import { isString, stringify, isBlank } from '@angular/core/src/facade/lang'; import { isString, stringify, isBlank } from '@angular/core/src/facade/lang';
import { BaseException } from '@angular/core/src/facade/exceptions'; import { BaseException } from '@angular/core/src/facade/exceptions';
import JsonPointer from './JsonPointer'; import JsonPointer from './JsonPointer';
declare var Prism: any; declare var Prism: any;
import marked from 'marked'; import Remarkable from 'remarkable';
// in gfm mode marked doesn't parse #Heading (without space after #) as heading const md = new Remarkable({
// https://github.com/chjj/marked/issues/642 html: true,
marked['Lexer'].rules.gfm.heading = marked['Lexer'].rules.normal.heading; linkify: true,
marked['Lexer'].rules.tables.heading = marked['Lexer'].rules.normal.heading;
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false, breaks: false,
pedantic: false, typographer: false,
smartLists: true, highlight: (str, lang) => {
smartypants: false if (lang === 'json') lang = 'js';
let grammar = Prism.languages[lang];
//fallback to clike
if (!grammar) return str;
return Prism.highlight(str, grammar);
}
}); });
class InvalidPipeArgumentException extends BaseException { class InvalidPipeArgumentException extends BaseException {
@ -65,12 +65,16 @@ export class JsonPointerEscapePipe implements PipeTransform {
@Pipe({ name: 'marked' }) @Pipe({ name: 'marked' })
export class MarkedPipe implements PipeTransform { export class MarkedPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizationService) {}
transform(value) { transform(value) {
if (isBlank(value)) return value; if (isBlank(value)) return value;
if (!isString(value)) { if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value); throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
} }
return `<span class="redoc-markdown-block">${marked(value)}</span>`;
return this.sanitizer.bypassSecurityTrustHtml(
`<span class="redoc-markdown-block">${md.render(value)}</span>`
);
} }
} }
@ -84,6 +88,7 @@ const langMap = {
@Pipe({ name: 'prism' }) @Pipe({ name: 'prism' })
export class PrismPipe implements PipeTransform { export class PrismPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizationService) {}
transform(value, args) { transform(value, args) {
if (isBlank(args) || args.length === 0) { if (isBlank(args) || args.length === 0) {
throw new BaseException('Prism pipe requires one argument'); throw new BaseException('Prism pipe requires one argument');
@ -98,7 +103,7 @@ export class PrismPipe implements PipeTransform {
let grammar = Prism.languages[lang]; let grammar = Prism.languages[lang];
//fallback to clike //fallback to clike
if (!grammar) grammar = Prism.languages.clike; if (!grammar) grammar = Prism.languages.clike;
return Prism.highlight(value, grammar); return this.sanitizer.bypassSecurityTrustHtml(Prism.highlight(value, grammar));
} }
} }

View File

@ -43,9 +43,9 @@
"json": "github:systemjs/plugin-json@^0.1.0", "json": "github:systemjs/plugin-json@^0.1.0",
"json-pointer": "npm:json-pointer@^0.3.0", "json-pointer": "npm:json-pointer@^0.3.0",
"json-schema-ref-parser": "npm:json-schema-ref-parser@^3.1.2", "json-schema-ref-parser": "npm:json-schema-ref-parser@^3.1.2",
"marked": "npm:marked@^0.3.5",
"openapi-sampler": "npm:openapi-sampler@0.2.0", "openapi-sampler": "npm:openapi-sampler@0.2.0",
"prismjs": "npm:prismjs@^1.3.0", "prismjs": "npm:prismjs@^1.3.0",
"remarkable": "npm:remarkable@^1.6.2",
"rxjs": "npm:rxjs@5.0.0-beta.6", "rxjs": "npm:rxjs@5.0.0-beta.6",
"scrollparent": "npm:scrollparent@^0.1.0", "scrollparent": "npm:scrollparent@^0.1.0",
"stream-http": "npm:stream-http@^2.3.0", "stream-http": "npm:stream-http@^2.3.0",
@ -121,13 +121,13 @@
"karma-regex-preprocessor": "github:makern/karma-regex-preprocessor", "karma-regex-preprocessor": "github:makern/karma-regex-preprocessor",
"karma-should": "^1.0.0", "karma-should": "^1.0.0",
"karma-sinon": "^1.0.4", "karma-sinon": "^1.0.4",
"marked": "^0.3.5",
"node-sass": "^3.7.0", "node-sass": "^3.7.0",
"openapi-sampler": "^0.2.0", "openapi-sampler": "^0.2.0",
"phantomjs-prebuilt": "^2.1.7", "phantomjs-prebuilt": "^2.1.7",
"protractor": "^3.0.0", "protractor": "^3.0.0",
"reflect-metadata": "^0.1.2", "reflect-metadata": "^0.1.2",
"remap-istanbul": "^0.6.4", "remap-istanbul": "^0.6.4",
"remarkable": "^1.6.2",
"require-dir": "^0.3.0", "require-dir": "^0.3.0",
"rollup-plugin-commonjs": "^2.2.1", "rollup-plugin-commonjs": "^2.2.1",
"run-sequence": "^1.1.5", "run-sequence": "^1.1.5",

View File

@ -33,9 +33,9 @@ System.config({
"json-formatter-js": "npm:json-formatter-js@0.2.0", "json-formatter-js": "npm:json-formatter-js@0.2.0",
"json-pointer": "npm:json-pointer@0.3.0", "json-pointer": "npm:json-pointer@0.3.0",
"json-schema-ref-parser": "npm:json-schema-ref-parser@3.1.2", "json-schema-ref-parser": "npm:json-schema-ref-parser@3.1.2",
"marked": "npm:marked@0.3.5",
"openapi-sampler": "npm:openapi-sampler@0.2.0", "openapi-sampler": "npm:openapi-sampler@0.2.0",
"prismjs": "npm:prismjs@1.3.0", "prismjs": "npm:prismjs@1.3.0",
"remarkable": "npm:remarkable@1.6.2",
"rxjs": "npm:rxjs@5.0.0-beta.6", "rxjs": "npm:rxjs@5.0.0-beta.6",
"scrollparent": "npm:scrollparent@0.1.0", "scrollparent": "npm:scrollparent@0.1.0",
"stream-http": "npm:stream-http@2.3.0", "stream-http": "npm:stream-http@2.3.0",
@ -148,6 +148,15 @@ System.config({
"path": "github:jspm/nodelibs-path@0.1.0", "path": "github:jspm/nodelibs-path@0.1.0",
"process": "github:jspm/nodelibs-process@0.1.2" "process": "github:jspm/nodelibs-process@0.1.2"
}, },
"npm:argparse@0.1.16": {
"assert": "github:jspm/nodelibs-assert@0.1.0",
"fs": "github:jspm/nodelibs-fs@0.1.2",
"path": "github:jspm/nodelibs-path@0.1.0",
"process": "github:jspm/nodelibs-process@0.1.2",
"underscore": "npm:underscore@1.7.0",
"underscore.string": "npm:underscore.string@2.4.0",
"util": "github:jspm/nodelibs-util@0.1.0"
},
"npm:argparse@1.0.7": { "npm:argparse@1.0.7": {
"fs": "github:jspm/nodelibs-fs@0.1.2", "fs": "github:jspm/nodelibs-fs@0.1.2",
"path": "github:jspm/nodelibs-path@0.1.0", "path": "github:jspm/nodelibs-path@0.1.0",
@ -193,6 +202,10 @@ System.config({
"npm:async@1.5.2": { "npm:async@1.5.2": {
"process": "github:jspm/nodelibs-process@0.1.2" "process": "github:jspm/nodelibs-process@0.1.2"
}, },
"npm:autolinker@0.15.3": {
"child_process": "github:jspm/nodelibs-child_process@0.1.0",
"process": "github:jspm/nodelibs-process@0.1.2"
},
"npm:aws-sign2@0.6.0": { "npm:aws-sign2@0.6.0": {
"crypto": "github:jspm/nodelibs-crypto@0.1.0", "crypto": "github:jspm/nodelibs-crypto@0.1.0",
"url": "github:jspm/nodelibs-url@0.1.0" "url": "github:jspm/nodelibs-url@0.1.0"
@ -743,6 +756,16 @@ System.config({
"string_decoder": "npm:string_decoder@0.10.31", "string_decoder": "npm:string_decoder@0.10.31",
"util-deprecate": "npm:util-deprecate@1.0.2" "util-deprecate": "npm:util-deprecate@1.0.2"
}, },
"npm:remarkable@1.6.2": {
"argparse": "npm:argparse@0.1.16",
"autolinker": "npm:autolinker@0.15.3",
"buffer": "github:jspm/nodelibs-buffer@0.1.0",
"fs": "github:jspm/nodelibs-fs@0.1.2",
"path": "github:jspm/nodelibs-path@0.1.0",
"process": "github:jspm/nodelibs-process@0.1.2",
"systemjs-json": "github:systemjs/plugin-json@0.1.2",
"util": "github:jspm/nodelibs-util@0.1.0"
},
"npm:request@2.72.0": { "npm:request@2.72.0": {
"aws-sign2": "npm:aws-sign2@0.6.0", "aws-sign2": "npm:aws-sign2@0.6.0",
"aws4": "npm:aws4@1.4.1", "aws4": "npm:aws4@1.4.1",

7
typings/marked.d.ts vendored
View File

@ -1,7 +0,0 @@
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/json-pointer/json-pointer.d.ts
declare module "marked" {
var x: any;
export default x;
}

4
typings/remarkable.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
declare module 'remarkable' {
var x: any;
export default x;
}