فهرست منبع

Add a markdown renderer

Steven Silvester 9 سال پیش
والد
کامیت
2876f3ba0d
4فایلهای تغییر یافته به همراه155 افزوده شده و 1 حذف شده
  1. 1 0
      package.json
  2. 25 1
      src/renderers/index.ts
  3. 1 0
      src/typings.d.ts
  4. 128 0
      typings/marked/marked.d.ts

+ 1 - 0
package.json

@@ -11,6 +11,7 @@
     "jupyter-js-services": "^0.8.0",
     "jupyter-js-utils": "^0.4.0",
     "jupyter-js-widgets": "0.0.17",
+    "marked": "^0.3.5",
     "moment": "^2.11.2",
     "phosphor-arrays": "^1.0.6",
     "phosphor-domutil": "^1.2.0",

+ 25 - 1
src/renderers/index.ts

@@ -1,6 +1,9 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import * as marked
+  from 'marked';
+
 import {
   IRenderer
 } from '../rendermime';
@@ -17,7 +20,7 @@ import {
 } from 'phosphor-messaging';
 
 import {
-  typeset
+  typeset, removeMath, replaceMath
 } from './latex';
 
 
@@ -69,6 +72,7 @@ class LatexWidget extends Widget {
 export
 class HTMLRenderer implements IRenderer<Widget> {
   mimetypes = ['text/html'];
+
   render(mimetype: string, data: string): Widget {
     return new HTMLWidget(data);
   }
@@ -81,6 +85,7 @@ class HTMLRenderer implements IRenderer<Widget> {
 export
 class ImageRenderer implements IRenderer<Widget> {
   mimetypes = ['image/png', 'image/jpeg', 'image/gif'];
+
   render(mimetype: string, data: string): Widget {
     let w = new Widget();
     let img = document.createElement('img');
@@ -97,6 +102,7 @@ class ImageRenderer implements IRenderer<Widget> {
 export
 class TextRenderer implements IRenderer<Widget> {
   mimetypes = ['text/plain'];
+
   render(mimetype: string, data: string): Widget {
     let w = new Widget();
     w.node.textContent = data;
@@ -135,6 +141,7 @@ class ConsoleTextRenderer implements IRenderer<Widget> {
 export
 class JavascriptRenderer implements IRenderer<Widget> {
   mimetypes = ['text/javascript', 'application/javascript'];
+
   render(mimetype: string, data: string): Widget {
     let w = new Widget();
     let s = document.createElement('script');
@@ -152,6 +159,7 @@ class JavascriptRenderer implements IRenderer<Widget> {
 export
 class SVGRenderer implements IRenderer<Widget> {
   mimetypes = ['image/svg+xml'];
+
   render(mimetype: string, data: string): Widget {
     let w = new Widget();
     w.node.innerHTML = data;
@@ -170,7 +178,23 @@ class SVGRenderer implements IRenderer<Widget> {
 export
 class LatexRenderer implements IRenderer<Widget> {
   mimetypes = ['text/latex'];
+
   render(mimetype: string, data: string): Widget {
     return new LatexWidget(data);
   }
 }
+
+
+/**
+ * A renderer for Jupyter Markdown data.
+ */
+export
+class MarkdownRenderer implements IRenderer<Widget> {
+  mimetypes = ['application/vnd.jupyter.markdown'];
+
+  render(mimetype: string, text: string): Widget {
+    let data = removeMath(text);
+    let html = marked(data['text']);
+    return new HTMLWidget(replaceMath(html, data['math']));
+  }
+}

+ 1 - 0
src/typings.d.ts

@@ -7,3 +7,4 @@
 /// <reference path="../typings/require/require.d.ts"/>
 /// <reference path="../typings/jupyter-js-widgets/jupyter-js-widgets.d.ts"/>
 /// <reference path="../typings/term.js/term.js.d.ts"/>
+/// <reference path="../typings/marked/marked.d.ts"/>

+ 128 - 0
typings/marked/marked.d.ts

@@ -0,0 +1,128 @@
+// Type definitions for Marked
+// Project: https://github.com/chjj/marked
+// Definitions by: William Orr <https://github.com/worr>
+// Definitions: https://github.com/borisyankov/DefinitelyTyped
+
+
+interface MarkedStatic {
+    /**
+     * Compiles markdown to HTML.
+     *
+     * @param src String of markdown source to be compiled
+     * @param callback Function called when the markdownString has been fully parsed when using async highlighting
+     * @return String of compiled HTML
+     */
+    (src: string, callback: Function): string;
+
+    /**
+     * Compiles markdown to HTML.
+     *
+     * @param src String of markdown source to be compiled
+     * @param options Hash of options
+     * @param callback Function called when the markdownString has been fully parsed when using async highlighting
+     * @return String of compiled HTML
+     */
+    (src: string, options?: MarkedOptions, callback?: Function): string;
+
+    /**
+     * @param src String of markdown source to be compiled
+     * @param options Hash of options
+     */
+    lexer(src: string, options?: MarkedOptions): any[];
+
+    /**
+     * Compiles markdown to HTML.
+     *
+     * @param src String of markdown source to be compiled
+     * @param callback Function called when the markdownString has been fully parsed when using async highlighting
+     * @return String of compiled HTML
+     */
+    parse(src: string, callback: Function): string;
+
+    /**
+     * Compiles markdown to HTML.
+     *
+     * @param src String of markdown source to be compiled
+     * @param options Hash of options
+     * @param callback Function called when the markdownString has been fully parsed when using async highlighting
+     * @return String of compiled HTML
+     */
+    parse(src: string, options?: MarkedOptions, callback?: Function): string;
+
+    /**
+     * @param options Hash of options
+     */
+    parser(src: any[], options?: MarkedOptions): string;
+
+    /**
+     * Sets the default options.
+     *
+     * @param options Hash of options
+     */
+    setOptions(options: MarkedOptions): void;
+}
+
+interface MarkedOptions {
+    /**
+     * Type: object Default: new Renderer()
+     *
+     * An object containing functions to render tokens to HTML.
+     */
+    renderer?: Object; 
+
+    /**
+     * Enable GitHub flavored markdown.
+     */
+    gfm?: boolean;
+
+    /**
+     * Enable GFM tables. This option requires the gfm option to be true.
+     */
+    tables?: boolean;
+
+    /**
+     * Enable GFM line breaks. This option requires the gfm option to be true.
+     */
+    breaks?: boolean;
+
+    /**
+     * Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
+     */
+    pedantic?: boolean;
+
+    /**
+     * Sanitize the output. Ignore any HTML that has been input.
+     */
+    sanitize?: boolean;
+
+    /**
+     * Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
+     */
+    smartLists?: boolean;
+
+    /**
+     * Shows an HTML error message when rendering fails.
+     */
+    silent?: boolean;
+
+    /**
+     * A function to highlight code blocks. The function takes three arguments: code, lang, and callback.
+     */
+    highlight? (code: string, lang: string, callback?: Function): string;
+
+    /**
+     * Set the prefix for code block classes.
+     */
+    langPrefix?: string;
+
+    /**
+     * Use "smart" typograhic punctuation for things like quotes and dashes.
+     */
+    smartypants?: boolean;
+}
+
+declare module "marked" {
+    export = marked;
+}
+
+declare var marked: MarkedStatic;