Skip to content

Commit

Permalink
Merge remote-tracking branch 'evilstreak/master' into upstream
Browse files Browse the repository at this point in the history
* evilstreak/master:
  Start changes file for next release
  v0.5.0
  Add test for image without title inside link (evilstreak#71)
  Update changelog since 0.4
  Style change - move function just used in table processing into Maruku.block.table
  Fix broken escaping introduected in 404344f
  added reference to new tests
  added some tests for tables
  added tolerance for up to three spaces for headers and first horizontal rule as per markdown extra
  corrected some omitted characters needing escape in _split_on_unescaped
  resolved issue with escaped pipes
  implemented tables
  • Loading branch information
Matt Kunze committed Aug 4, 2013
2 parents 26459ac + a4e17d7 commit 8f4a6c0
Show file tree
Hide file tree
Showing 20 changed files with 445 additions and 6 deletions.
23 changes: 22 additions & 1 deletion Changes.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog for markdown

## vNEXT - ???

## v0.5.0 - 2013-07-26

There might be other bug fixes then the ones listed - I've been a bit lax at
updating the changes file, sorry :(

- Fix 'undefined' appearing in output for some cases with blockquotes
- Fix (multiple) global variable leaks. Ooops
- Fix IE8 issues (#68, #74, #97)
- Fix IE8 issue (#86)
- Handle windows line endings (#58)
- Allow spaces in img/link paths (#48)
- Add explicit text of the license to the readme (#74)
- Style tweaks by Xhmikosr (#83, #81, #82)
- Build now tested by TravisCI thanks to sebs (#85)
- Fix 'cuddled' header parsing (#94)
- Fix images inside links mistakenly requiring a title attribute to parse
correctly (#71)


## v0.4.0 - 2012-06-09

- Fix for anchors enclosed by parenthesis (issue #46)
Expand All @@ -13,4 +34,4 @@
- Fix blockquote merging/implicit conversion between string/String (#44, #24)
- md2html can now process stdin (#43)
- Fix jslint warnings (#42)
- Fix to correctly render self-closing tags (#40, #35, #28)
- Fix to correctly render self-closing tags (#40, #35, #28)
77 changes: 74 additions & 3 deletions lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,12 @@ Markdown.dialects.Gruber.inline = {
"]": function () {},
"}": function () {},

__escape__ : /^\\[\\`\*_{}\[\]()#\+.!\-]/,

"\\": function escaped( text ) {
// [ length of input processed, node/children to add... ]
// Only esacape: \ ` * _ { } [ ] ( ) # * + - . !
if ( text.match( /^\\[\\`\*_{}\[\]()#\+.!\-]/ ) )
if ( this.dialect.inline.__escape__.exec( text ) )
return [ 2, text.charAt( 1 ) ];
else
// Not an esacpe
Expand Down Expand Up @@ -1306,7 +1308,7 @@ Markdown.dialects.Maruku.block.definition_list = function definition_list( block
// one or more terms followed by one or more definitions, in a single block
var tight = /^((?:[^\s:].*\n)+):\s+([\s\S]+)$/,
list = [ "dl" ],
i;
i, m;

// see if we're dealing with a tight or loose block
if ( ( m = block.match( tight ) ) ) {
Expand Down Expand Up @@ -1340,6 +1342,72 @@ Markdown.dialects.Maruku.block.definition_list = function definition_list( block
return [ list ];
};

// splits on unescaped instances of @ch. If @ch is not a character the result
// can be unpredictable

Markdown.dialects.Maruku.block.table = function table (block, next) {

var _split_on_unescaped = function(s, ch) {
ch = ch || '\\s';
if (ch.match(/^[\\|\[\]{}?*.+^$]$/)) { ch = '\\' + ch; }
var res = [ ],
r = new RegExp('^((?:\\\\.|[^\\\\' + ch + '])*)' + ch + '(.*)'),
m;
while(m = s.match(r)) {
res.push(m[1]);
s = m[2];
}
res.push(s);
return res;
}

var leading_pipe = /^ {0,3}\|(.+)\n {0,3}\|\s*([\-:]+[\-| :]*)\n((?:\s*\|.*(?:\n|$))*)(?=\n|$)/,
// find at least an unescaped pipe in each line
no_leading_pipe = /^ {0,3}(\S(?:\\.|[^\\|])*\|.*)\n {0,3}([\-:]+\s*\|[\-| :]*)\n((?:(?:\\.|[^\\|])*\|.*(?:\n|$))*)(?=\n|$)/,
i, m;
if (m = block.match(leading_pipe)) {
// remove leading pipes in contents
// (header and horizontal rule already have the leading pipe left out)
m[3] = m[3].replace(/^\s*\|/gm, '');
} else if (! ( m = block.match(no_leading_pipe))) {
return undefined;
}

var table = [ "table", [ "thead", [ "tr" ] ], [ "tbody" ] ];

// remove trailing pipes, then split on pipes
// (no escaped pipes are allowed in horizontal rule)
m[2] = m[2].replace(/\|\s*$/, '').split('|');

// process alignment
var html_attrs = [ ];
forEach (m[2], function (s) {
if (s.match(/^\s*-+:\s*$/)) html_attrs.push({align: "right"});
else if (s.match(/^\s*:-+\s*$/)) html_attrs.push({align: "left"});
else if (s.match(/^\s*:-+:\s*$/)) html_attrs.push({align: "center"});
else html_attrs.push({});
});

// now for the header, avoid escaped pipes
m[1] = _split_on_unescaped(m[1].replace(/\|\s*$/, ''), '|');
for (i = 0; i < m[1].length; i++) {
table[1][1].push(['th', html_attrs[i] || {}].concat(
this.processInline(m[1][i].trim())));
}

// now for body contents
forEach (m[3].replace(/\|\s*$/mg, '').split('\n'), function (row) {
var html_row = ['tr'];
row = _split_on_unescaped(row, '|');
for (i = 0; i < row.length; i++) {
html_row.push(['td', html_attrs[i] || {}].concat(this.processInline(row[i].trim())));
}
table[2].push(html_row);
}, this);

return [table];
}

Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, out ) {
if ( !out.length ) {
return [ 2, "{:" ];
Expand Down Expand Up @@ -1377,6 +1445,8 @@ Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, o
return [ m[ 0 ].length, "" ];
};

Markdown.dialects.Maruku.inline.__escape__ = /^\\[\\`\*_{}\[\]()#\+.!\-|:]/;

Markdown.buildBlockOrder ( Markdown.dialects.Maruku.block );
Markdown.buildInlinePatterns( Markdown.dialects.Maruku.inline );

Expand Down Expand Up @@ -1732,7 +1802,8 @@ function convert_tree_to_html( tree, references, options ) {
if ( attrs ) {
// if there are keys, skip over it
for ( var key in jsonml[ 1 ] ) {
i = 2;
i = 2;
break;
}
// if there aren't, remove it
if ( i === 1 ) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown",
"version": "0.4.0",
"version": "0.5.0",
"description": "A sensible Markdown parser for javascript",
"keywords": [
"markdown",
Expand Down
2 changes: 1 addition & 1 deletion test/features.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dialects.Gruber = [
];

dialects.Maruku = dialects.Gruber.slice( 0 );
dialects.Maruku.push( "meta", "definition_lists" );
dialects.Maruku.push( "meta", "definition_lists", "tables" );

dialects.Github = [ "emphasis", "github" ]

Expand Down
11 changes: 11 additions & 0 deletions test/features/images/inside_link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
["html",
["p",
["a",
{ "href": "https://link-url" },
["img", {
"alt": "alt",
"src": "https://img-url"
} ]
]
]
]
1 change: 1 addition & 0 deletions test/features/images/inside_link.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[![alt](https://img-url)](https://link-url)
64 changes: 64 additions & 0 deletions test/features/tables/align.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[ "html",
[ "table",
[ "thead",
[ "tr",
[ "th",
{ "align": "left" },
"#"
],
[ "th",
{ "align": "center" },
"Item"
],
[ "th",
{ "align": "right" },
"Value"
]
]
],
[ "tbody",
[ "tr",
[ "td",
{ "align": "left" },
"1"
],
[ "td",
{ "align": "center" },
"Computer"
],
[ "td",
{ "align": "right" },
"$1600"
]
],
[ "tr",
[ "td",
{ "align": "left" },
"2"
],
[ "td",
{ "align": "center" },
"Phone"
],
[ "td",
{ "align": "right" },
"$12"
]
],
[ "tr",
[ "td",
{ "align": "left" },
"3"
],
[ "td",
{ "align": "center" },
"Pipe"
],
[ "td",
{ "align": "right" },
"$1"
]
]
]
]
]
5 changes: 5 additions & 0 deletions test/features/tables/align.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| # | Item | Value |
| :- | :-------: | -----:|
| 1 | Computer | $1600 |
| 2 | Phone | $12 |
| 3 | Pipe | $1 |
64 changes: 64 additions & 0 deletions test/features/tables/compact.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[ "html",
[ "table",
[ "thead",
[ "tr",
[ "th",
{ "align": "left" },
"#"
],
[ "th",
{ "align": "center" },
"Item"
],
[ "th",
{ "align": "right" },
"Value"
]
]
],
[ "tbody",
[ "tr",
[ "td",
{ "align": "left" },
"1"
],
[ "td",
{ "align": "center" },
"Computer"
],
[ "td",
{ "align": "right" },
"$1600"
]
],
[ "tr",
[ "td",
{ "align": "left" },
"2"
],
[ "td",
{ "align": "center" },
"Phone"
],
[ "td",
{ "align": "right" },
"$12"
]
],
[ "tr",
[ "td",
{ "align": "left" },
"3"
],
[ "td",
{ "align": "center" },
"Pipe"
],
[ "td",
{ "align": "right" },
"$1"
]
]
]
]
]
5 changes: 5 additions & 0 deletions test/features/tables/compact.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#|Item|Value
:-|:-:|-:
1|Computer|$1600
2|Phone|$12
3|Pipe|$1
38 changes: 38 additions & 0 deletions test/features/tables/inline_formatting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[ "html",
[ "table",
[ "thead",
[ "tr",
[ "th",
"Function name"
],
[ "th",
"Description"
]
]
],
[ "tbody",
[ "tr",
[ "td",
[ "code",
"help()"
]
],
[ "td",
"Display the help window."
]
],
[ "tr",
[ "td",
[ "code",
"destroy()"
]
],
[ "td",
[ "strong",
"Destroy your computer!"
]
]
]
]
]
]
4 changes: 4 additions & 0 deletions test/features/tables/inline_formatting.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Function name | Description |
| ------------- | ------------------------------ |
| `help()` | Display the help window. |
| `destroy()` | **Destroy your computer!** |
Loading

0 comments on commit 8f4a6c0

Please sign in to comment.