diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/index.html | 118 | ||||
-rw-r--r-- | js/lib/blocks.js | 18 |
2 files changed, 20 insertions, 116 deletions
diff --git a/js/index.html b/js/index.html index 6f462a9..3f6c904 100644 --- a/js/index.html +++ b/js/index.html @@ -1,108 +1,12 @@ -<!doctype html> -<html lang="en"> -<head> - <meta charset="utf-8"> - <title>commonmark.js demo</title> - <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> - <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> - <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"> - <script src="commonmark.js"></script> - <script type="text/javascript"> - -var writer = new commonmark.HtmlRenderer(); -var reader = new commonmark.DocParser(); - -$(document).ready(function() { - var timer; - var x; - var parsed; - var render = function() { - if (parsed === undefined) { - return; - } - var startTime = new Date().getTime(); - var result = writer.renderBlock(parsed); - var endTime = new Date().getTime(); - var renderTime = endTime - startTime; - // $("#html").text(result); - $("#preview").html(result); - $("#html").text(result); - $("#ast").text(commonmark.ASTRenderer(parsed)); - $("#rendertime").text(renderTime); - }; - var parseAndRender = function () { - if (x) { x.abort() } // If there is an existing XHR, abort it. - clearTimeout(timer); // Clear the timer so we don't end up with dupes. - timer = setTimeout(function() { // assign timer a new timeout - var startTime = new Date().getTime(); - parsed = reader.parse($("#text").val()); - var endTime = new Date().getTime(); - var parseTime = endTime - startTime; - $("#parsetime").text(parseTime); - $(".timing").css('visibility','visible'); - /* - var warnings = parsed.warnings; - $("#warnings").html(''); - for (i=0; i < warnings.length; i++) { - var w = warnings[i]; - var warning = $("#warnings").append('<li></li>'); - $("#warnings li").last().text('Line ' + w.line + ' column ' + w.column + ': ' + w.message); - } - */ - render(); - }, 0); // ms delay - }; - $("#text").bind('keyup paste cut mouseup', parseAndRender); - $(".option").change(render); -}); - </script> - <style type="text/css"> - h1.title { font-family: monospace; font-size: 120%; font-weight: bold; - margin-top: 0.5em; margin-bottom: 0; } - textarea#text { height: 400px; width: 95%; font-family: monospace; font-size: 92%; } - pre code#html { font-size: 92%; font-family: monospace; } - pre#htmlpre { height: 400px; width: 95%; overflow: scroll; } - div#preview { height: 400px; overflow: scroll; } - div.row { margin-top: 1em; } - blockquote { font-size: 100%; } - footer { color: #555; text-align: center; margin: 1em; } - pre { display: block; padding: 0.5em; color: #333; background: #f8f8ff } - #warnings li { color: red; font-weight: bold; } - label { padding-left: 1em; padding-top: 0; padding-bottom: 0; } - div.timing { color: red; visibility: hidden; height: 3em; } - span.timing { font-weight: bold; } - span.timing { font-weight: bold; } - </style> -</head> -<body> -<div class="container"> - <div class="row"> - <h1 class="title">commonmark.js dingus</h1> - </div> - <div class="row"> - <div class="col-md-6"> - <div class="timing">Parsed in <span class="timing" id="parsetime"></span> - ms. Rendered in <span class="timing" id="rendertime"></span> ms.</div> - <textarea id="text"></textarea> - <ul id="warnings"></ul> - </div> - <div class="col-md-6"> - <ul class="nav nav-tabs" role="tablist"> - <li class="active"><a href="#preview" role="tab" data-toggle="tab">Preview</a></li> - <li><a href="#result" role="tab" data-toggle="tab">HTML</a></li> - <li><a href="#result-ast" role="tab" data-toggle="tab">AST</a></li> - </ul> - <div class="tab-content"> - <div id="preview" class="tab-pane active"> - </div> - <div id="result" class="tab-pane"> - <pre id="htmlpre"><code id="html"></code></pre> - </div> - <div id="result-ast" class="tab-pane"> - <pre id="astpre"><code id="ast"></code></pre> - </div> - </div> - </div> -</div> -</body> +<!DOCTYPE html> +<html> + <head> + <title>CommonMark dingus</title> + <meta http-equiv="refresh" content="0;URL='/dingus.html" > + </head> + <body> + <p>The most recent version of the CommonMark dingus can be found +at <a + href="http://try.commonmark.org/dingus.html/">/dingus.html/</a>.</p> + </body> </html> diff --git a/js/lib/blocks.js b/js/lib/blocks.js index 109661f..791b74f 100644 --- a/js/lib/blocks.js +++ b/js/lib/blocks.js @@ -25,13 +25,13 @@ var detabLine = function(text) { }; // Attempt to match a regex in string s at offset offset. -// Return index of match or null. +// Return index of match or -1. var matchAt = function(re, s, offset) { var res = s.slice(offset).match(re); if (res) { return offset + res.index; } else { - return null; + return -1; } }; @@ -218,7 +218,7 @@ var incorporateLine = function(ln, line_number) { container = last_child; match = matchAt(/[^ ]/, ln, offset); - if (match === null) { + if (match === -1) { first_nonspace = ln.length; blank = true; } else { @@ -326,10 +326,10 @@ var incorporateLine = function(ln, line_number) { container.t != 'IndentedCode' && container.t != 'HtmlBlock' && // this is a little performance optimization: - matchAt(/^[ #`~*+_=<>0-9-]/,ln,offset) !== null) { + matchAt(/^[ #`~*+_=<>0-9-]/,ln,offset) !== -1) { match = matchAt(/[^ ]/, ln, offset); - if (match === null) { + if (match === -1) { first_nonspace = ln.length; blank = true; } else { @@ -366,7 +366,7 @@ var incorporateLine = function(ln, line_number) { container.level = match[0].trim().length; // number of #s // remove trailing ###s: container.strings = - [ln.slice(offset).replace(/(?:(\\#) *#*| *#+) *$/,'$1')]; + [ln.slice(offset).replace(/^ *#+ *$/, '').replace(/ +#+ *$/,'')]; break; } else if ((match = ln.slice(first_nonspace).match(/^`{3,}(?!.*`)|^~{3,}(?!.*~)/))) { @@ -380,7 +380,7 @@ var incorporateLine = function(ln, line_number) { offset = first_nonspace + fence_length; break; - } else if (matchAt(reHtmlBlockOpen, ln, first_nonspace) !== null) { + } else if (matchAt(reHtmlBlockOpen, ln, first_nonspace) !== -1) { // html block closeUnmatchedBlocks(this); container = this.addChild('HtmlBlock', line_number, first_nonspace); @@ -396,7 +396,7 @@ var incorporateLine = function(ln, line_number) { container.level = match[0][0] === '=' ? 1 : 2; offset = ln.length; - } else if (matchAt(reHrule, ln, first_nonspace) !== null) { + } else if (matchAt(reHrule, ln, first_nonspace) !== -1) { // hrule closeUnmatchedBlocks(this); container = this.addChild('HorizontalRule', line_number, first_nonspace); @@ -435,7 +435,7 @@ var incorporateLine = function(ln, line_number) { // appropriate container. match = matchAt(/[^ ]/, ln, offset); - if (match === null) { + if (match === -1) { first_nonspace = ln.length; blank = true; } else { |