|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +define([ |
| 5 | + 'jquery', |
| 6 | + 'base/js/utils', |
| 7 | + 'base/js/mathjaxutils', |
| 8 | + 'base/js/security', |
| 9 | + 'components/marked/lib/marked', |
| 10 | + 'codemirror/lib/codemirror', |
| 11 | +], function($, utils, mathjaxutils, security, marked, CodeMirror){ |
| 12 | + "use strict"; |
| 13 | + |
| 14 | + marked.setOptions({ |
| 15 | + gfm : true, |
| 16 | + tables: true, |
| 17 | + langPrefix: "cm-s-ipython language-", |
| 18 | + highlight: function(code, lang, callback) { |
| 19 | + if (!lang) { |
| 20 | + // no language, no highlight |
| 21 | + if (callback) { |
| 22 | + callback(null, code); |
| 23 | + return; |
| 24 | + } else { |
| 25 | + return code; |
| 26 | + } |
| 27 | + } |
| 28 | + utils.requireCodeMirrorMode(lang, function (spec) { |
| 29 | + var el = document.createElement("div"); |
| 30 | + var mode = CodeMirror.getMode({}, spec); |
| 31 | + if (!mode) { |
| 32 | + console.log("No CodeMirror mode: " + lang); |
| 33 | + callback(null, code); |
| 34 | + return; |
| 35 | + } |
| 36 | + try { |
| 37 | + CodeMirror.runMode(code, spec, el); |
| 38 | + callback(null, el.innerHTML); |
| 39 | + } catch (err) { |
| 40 | + console.log("Failed to highlight " + lang + " code", err); |
| 41 | + callback(err, code); |
| 42 | + } |
| 43 | + }, function (err) { |
| 44 | + console.log("No CodeMirror mode: " + lang); |
| 45 | + console.log("Require CodeMirror mode error: " + err); |
| 46 | + callback(null, code); |
| 47 | + }); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + var mathjax_init_done = false; |
| 52 | + function ensure_mathjax_init() { |
| 53 | + if(!mathjax_init_done) { |
| 54 | + mathjax_init_done = true; |
| 55 | + mathjaxutils.init(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + function render(markdown, options, callback) { |
| 60 | + /** |
| 61 | + * Find a readme in the current directory. Look for files with |
| 62 | + * a name like 'readme.md' (case insensitive) or similar and |
| 63 | + * mimetype 'text/markdown'. |
| 64 | + * |
| 65 | + * @param markdown: the markdown text to parse |
| 66 | + * @param options |
| 67 | + * Object with parameters: |
| 68 | + * with_math: the markdown can contain mathematics |
| 69 | + * clean_tables: prevent default inline styles for table cells |
| 70 | + * sanitize: remove dangerous html (like <script>) |
| 71 | + * @param callback |
| 72 | + * A function with two arguments (err, html) |
| 73 | + * err: null or the error if there was one |
| 74 | + * html: the rendered html string, or if {sanitize: true} was used |
| 75 | + * a sanitized jQuery object |
| 76 | + */ |
| 77 | + options = $.extend({ |
| 78 | + // Apply mathjax transformation |
| 79 | + with_math: false, |
| 80 | + // Prevent marked from returning inline styles for table cells |
| 81 | + clean_tables: false, |
| 82 | + // Apply sanitation, this will return a jQuery object. |
| 83 | + sanitize: false, |
| 84 | + }, options); |
| 85 | + var renderer = new marked.Renderer(); |
| 86 | + if(options.clean_tables) { |
| 87 | + renderer.tablecell = function (content, flags) { |
| 88 | + var type = flags.header ? 'th' : 'td'; |
| 89 | + var style = flags.align == null ? '': ' style="text-align: ' + flags.align + '"'; |
| 90 | + var start_tag = '<' + type + style + '>'; |
| 91 | + var end_tag = '</' + type + '>\n'; |
| 92 | + return start_tag + content + end_tag; |
| 93 | + }; |
| 94 | + } |
| 95 | + var text = markdown; |
| 96 | + var math = null; |
| 97 | + if(options.with_math) { |
| 98 | + ensure_mathjax_init(); |
| 99 | + var text_and_math = mathjaxutils.remove_math(markdown); |
| 100 | + text = text_and_math[0]; |
| 101 | + math = text_and_math[1]; |
| 102 | + } |
| 103 | + marked(text, { renderer: renderer }, function (err, html) { |
| 104 | + if(!err) { |
| 105 | + if(options.with_math) { |
| 106 | + html = mathjaxutils.replace_math(html, math); |
| 107 | + } |
| 108 | + if(options.sanitize) { |
| 109 | + html = $(security.sanitize_html_and_parse(html)); |
| 110 | + } |
| 111 | + } |
| 112 | + callback(err, html); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + return {'render': render}; |
| 117 | +}); |
0 commit comments