From b65370c8ac48f8994f40ce2fcd65905ae593944d Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 22 Jun 2016 12:42:03 +0300 Subject: [PATCH] Replace marked with remarkable --- lib/utils/pipes.ts | 35 ++++++++++++++++++++--------------- package.json | 4 ++-- system.config.js | 25 ++++++++++++++++++++++++- typings/marked.d.ts | 7 ------- typings/remarkable.d.ts | 4 ++++ 5 files changed, 50 insertions(+), 25 deletions(-) delete mode 100644 typings/marked.d.ts create mode 100644 typings/remarkable.d.ts diff --git a/lib/utils/pipes.ts b/lib/utils/pipes.ts index ab09fbb8..425ce7ce 100644 --- a/lib/utils/pipes.ts +++ b/lib/utils/pipes.ts @@ -1,27 +1,27 @@ 'use strict'; import { Pipe, PipeTransform } from '@angular/core'; +import { DomSanitizationService } from '@angular/platform-browser'; import { isString, stringify, isBlank } from '@angular/core/src/facade/lang'; import { BaseException } from '@angular/core/src/facade/exceptions'; import JsonPointer from './JsonPointer'; 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 -// https://github.com/chjj/marked/issues/642 -marked['Lexer'].rules.gfm.heading = marked['Lexer'].rules.normal.heading; -marked['Lexer'].rules.tables.heading = marked['Lexer'].rules.normal.heading; - -marked.setOptions({ - renderer: new marked.Renderer(), - gfm: true, - tables: true, +const md = new Remarkable({ + html: true, + linkify: true, breaks: false, - pedantic: false, - smartLists: true, - smartypants: false + typographer: false, + highlight: (str, lang) => { + 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 { @@ -65,12 +65,16 @@ export class JsonPointerEscapePipe implements PipeTransform { @Pipe({ name: 'marked' }) export class MarkedPipe implements PipeTransform { + constructor(private sanitizer: DomSanitizationService) {} transform(value) { if (isBlank(value)) return value; if (!isString(value)) { throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value); } - return `${marked(value)}`; + + return this.sanitizer.bypassSecurityTrustHtml( + `${md.render(value)}` + ); } } @@ -84,6 +88,7 @@ const langMap = { @Pipe({ name: 'prism' }) export class PrismPipe implements PipeTransform { + constructor(private sanitizer: DomSanitizationService) {} transform(value, args) { if (isBlank(args) || args.length === 0) { throw new BaseException('Prism pipe requires one argument'); @@ -98,7 +103,7 @@ export class PrismPipe implements PipeTransform { let grammar = Prism.languages[lang]; //fallback to clike if (!grammar) grammar = Prism.languages.clike; - return Prism.highlight(value, grammar); + return this.sanitizer.bypassSecurityTrustHtml(Prism.highlight(value, grammar)); } } diff --git a/package.json b/package.json index 8a6c87f4..34f355c7 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,9 @@ "json": "github:systemjs/plugin-json@^0.1.0", "json-pointer": "npm:json-pointer@^0.3.0", "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", "prismjs": "npm:prismjs@^1.3.0", + "remarkable": "npm:remarkable@^1.6.2", "rxjs": "npm:rxjs@5.0.0-beta.6", "scrollparent": "npm:scrollparent@^0.1.0", "stream-http": "npm:stream-http@^2.3.0", @@ -121,13 +121,13 @@ "karma-regex-preprocessor": "github:makern/karma-regex-preprocessor", "karma-should": "^1.0.0", "karma-sinon": "^1.0.4", - "marked": "^0.3.5", "node-sass": "^3.7.0", "openapi-sampler": "^0.2.0", "phantomjs-prebuilt": "^2.1.7", "protractor": "^3.0.0", "reflect-metadata": "^0.1.2", "remap-istanbul": "^0.6.4", + "remarkable": "^1.6.2", "require-dir": "^0.3.0", "rollup-plugin-commonjs": "^2.2.1", "run-sequence": "^1.1.5", diff --git a/system.config.js b/system.config.js index 7b7378b2..0a670a43 100644 --- a/system.config.js +++ b/system.config.js @@ -33,9 +33,9 @@ System.config({ "json-formatter-js": "npm:json-formatter-js@0.2.0", "json-pointer": "npm:json-pointer@0.3.0", "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", "prismjs": "npm:prismjs@1.3.0", + "remarkable": "npm:remarkable@1.6.2", "rxjs": "npm:rxjs@5.0.0-beta.6", "scrollparent": "npm:scrollparent@0.1.0", "stream-http": "npm:stream-http@2.3.0", @@ -148,6 +148,15 @@ System.config({ "path": "github:jspm/nodelibs-path@0.1.0", "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": { "fs": "github:jspm/nodelibs-fs@0.1.2", "path": "github:jspm/nodelibs-path@0.1.0", @@ -193,6 +202,10 @@ System.config({ "npm:async@1.5.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": { "crypto": "github:jspm/nodelibs-crypto@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", "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": { "aws-sign2": "npm:aws-sign2@0.6.0", "aws4": "npm:aws4@1.4.1", diff --git a/typings/marked.d.ts b/typings/marked.d.ts deleted file mode 100644 index be77f66a..00000000 --- a/typings/marked.d.ts +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/typings/remarkable.d.ts b/typings/remarkable.d.ts new file mode 100644 index 00000000..ee3c03b4 --- /dev/null +++ b/typings/remarkable.d.ts @@ -0,0 +1,4 @@ +declare module 'remarkable' { + var x: any; + export default x; +}