diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/.npmignore | 3 | ||||
-rw-r--r-- | js/README.md | 24 | ||||
-rw-r--r-- | js/lib/html-renderer.js | 2 | ||||
-rw-r--r-- | js/lib/inlines.js | 40 | ||||
-rw-r--r-- | js/package.json | 27 |
5 files changed, 73 insertions, 23 deletions
diff --git a/js/.npmignore b/js/.npmignore new file mode 100644 index 0000000..d73e230 --- /dev/null +++ b/js/.npmignore @@ -0,0 +1,3 @@ +commonmark.js +*.tgz +index.html diff --git a/js/README.md b/js/README.md new file mode 100644 index 0000000..9f1e043 --- /dev/null +++ b/js/README.md @@ -0,0 +1,24 @@ +CommonMark +========== + +CommonMark is a rationalized version of Markdown syntax, +with a [spec][the spec] and BSD3-licensed reference +implementations in C and JavaScript. + +For more information, see <http://commonmark.org>. + +To play with this library without installing it, see +the live dingus at <http://spec.commonmark.org/dingus.html>. + +This package includes the commonmark library and a +command-line executable, `commonmark`. + +Basic usage example: + + var reader = new commonmark.DocParser(); + var writer = new commonmark.HtmlRenderer(); + var parsed = reader.parse("Hello *world*"); + var result = writer.render(parsed); + + [the spec]: http://spec.commonmark.org + diff --git a/js/lib/html-renderer.js b/js/lib/html-renderer.js index e1a6063..a676e3a 100644 --- a/js/lib/html-renderer.js +++ b/js/lib/html-renderer.js @@ -43,7 +43,7 @@ var renderInline = function(inline) { return inTags('a', attrs, this.renderInlines(inline.label)); case 'Image': attrs = [['src', this.escape(inline.destination, true)], - ['alt', this.escape(this.renderInlines(inline.label))]]; + ['alt', this.renderInlines(inline.label).replace(/\<[^>]*\>/g,'')]]; if (inline.title) { attrs.push(['title', this.escape(inline.title, true)]); } diff --git a/js/lib/inlines.js b/js/lib/inlines.js index 5fde099..4f1f16a 100644 --- a/js/lib/inlines.js +++ b/js/lib/inlines.js @@ -235,8 +235,8 @@ var scanDelims = function(cc) { char_after = fromCodePoint(cc_after); } - var can_open = numdelims > 0 && numdelims <= 3 && !(/\s/.test(char_after)); - var can_close = numdelims > 0 && numdelims <= 3 && !(/\s/.test(char_before)); + var can_open = numdelims > 0 && !(/\s/.test(char_after)); + var can_close = numdelims > 0 && !(/\s/.test(char_before)); if (cc === C_UNDERSCORE) { can_open = can_open && !((/[a-z0-9]/i).test(char_before)); can_close = can_close && !((/[a-z0-9]/i).test(char_after)); @@ -265,6 +265,7 @@ var parseEmphasis = function(cc,inlines) { var res = this.scanDelims(cc); var numdelims = res.numdelims; + var usedelims; if (numdelims === 0) { this.pos = startpos; @@ -279,41 +280,36 @@ var parseEmphasis = function(cc,inlines) { if (opener.cc === cc) { // we have a match! - if (opener.numdelims <= numdelims) { // all openers used - - this.pos += opener.numdelims; - var X; - switch (opener.numdelims) { - case 3: - X = function(x) { return Strong([Emph(x)]); }; - break; - case 2: - X = Strong; - break; - case 1: - default: - X = Emph; - break; - } + if (numdelims < 3 || opener.numdelims < 3) { + usedelims = numdelims <= opener.numdelims ? numdelims : opener.numdelims; + } else { // numdelims >= 3 && opener.numdelims >= 3 + usedelims = numdelims % 2 === 0 ? 2 : 1; + } + var X = usedelims === 1 ? Emph : Strong; + + if (opener.numdelims == usedelims) { // all openers used + + this.pos += usedelims; inlines[opener.pos] = X(inlines.slice(opener.pos + 1)); inlines.splice(opener.pos + 1, inlines.length - (opener.pos + 1)); // Remove entries after this, to prevent overlapping nesting: this.emphasis_openers = opener.previous; return true; - } else if (opener.numdelims > numdelims) { // only some openers used + } else if (opener.numdelims > usedelims) { // only some openers used - this.pos += numdelims; - opener.numdelims -= numdelims; + this.pos += usedelims; + opener.numdelims -= usedelims; inlines[opener.pos].c = inlines[opener.pos].c.slice(0, opener.numdelims); - var X = numdelims === 2 ? Strong : Emph; inlines[opener.pos + 1] = X(inlines.slice(opener.pos + 1)); inlines.splice(opener.pos + 2, inlines.length - (opener.pos + 2)); // Remove entries after this, to prevent overlapping nesting: this.emphasis_openers = opener; return true; + } else { // usedelims > opener.numdelims, should never happen + throw new Error("Logic error: usedelims > opener.numdelims"); } } diff --git a/js/package.json b/js/package.json new file mode 100644 index 0000000..4ae8bca --- /dev/null +++ b/js/package.json @@ -0,0 +1,27 @@ +{ "name": "commonmark", + "description": "a strongly specified, highly compatible variant of Markdown", + "dist-tags": { "latest": "0.9.0" }, + "versions": "0.9.0", + "homepage": "http://commonmark.org", + "keywords": + [ "markdown", + "commonmark", + "md", + "stmd" ], + "repository": + { "type": "git", + "url": "https://github.com/jgm/CommonMark.git" }, + "author": "John MacFarlane", + "bugs": { "url": "https://github.com/jgm/CommonMark/issues" }, + "license": "BSD-3-Clause", + "version": "0.9.0", + "main": "./lib/index.js", + "bin": { "commonmark": "./bin/commonmark" }, + "scripts": { "test": "node ./test.js" }, + "directories": { + "lib": "./lib" + }, + "engines": { + "node": "*" + } +} |