Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

previously, when a fenced code block was in the second paragraph of a li... #443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var block = {
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
table: noop,
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
Expand Down Expand Up @@ -146,6 +146,31 @@ Lexer.prototype.lex = function(src) {
* Lexing
*/

function outdentListItem(item, space, options) {
var inCodeFence = false;
var itemLines = item.split('\n');

var changeCodeFenceState = function(line) {
if(!inCodeFence && (new RegExp(' *```.*')).test(line)) {
inCodeFence = true;
} else if(inCodeFence && (new RegExp(' *``` *')).test(line)) {
inCodeFence = false;
}
}

//outdent each line UNLESS we're in a fenced code block
for (var x = 0; x < itemLines.length; x++) {
var line = itemLines[x];
if(!inCodeFence) {
itemLines[x] = !options.pedantic
? line.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
: line.replace(/^ {1,4}/gm, '');
}
changeCodeFenceState(line);
}
return itemLines.join('\n');
}

Lexer.prototype.token = function(src, top, bq) {
var src = src.replace(/^ +$/gm, '')
, next
Expand Down Expand Up @@ -296,20 +321,24 @@ Lexer.prototype.token = function(src, top, bq) {
i = 0;

for (; i < l; i++) {
item = cap[i];
item = cap[i]; // item is a list item

// Remove the list item's bullet
// so it is seen as the next token.
space = item.length;
item = item.replace(/^ *([*+-]|\d+\.) +/, '');

// Outdent whatever the
// list item contains. Hacky.
if (~item.indexOf('\n ')) {
space -= item.length;
item = !this.options.pedantic
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
: item.replace(/^ {1,4}/gm, '');
if (~item.indexOf('\n ')) { //this evaluates to true iff '\n ' is in the string
space -= item.length; //space is now how far the bullet indents the contents
if(this.options.gfm) {
item = outdentListItem(item, space, this.options);
} else {
item = !this.options.pedantic
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
: item.replace(/^ {1,4}/gm, '');
}
}

// Determine whether the next list item belongs here.
Expand Down Expand Up @@ -1154,13 +1183,8 @@ function marked(src, opt, callback) {

pending = tokens.length;

var done = function(err) {
if (err) {
opt.highlight = highlight;
return callback(err);
}

var out;
var done = function() {
var out, err;

try {
out = Parser.parse(tokens, opt);
Expand Down Expand Up @@ -1189,7 +1213,6 @@ function marked(src, opt, callback) {
return --pending || done();
}
return highlight(token.text, token.lang, function(err, code) {
if (err) return done(err);
if (code == null || code === token.text) {
return --pending || done();
}
Expand Down Expand Up @@ -1259,7 +1282,7 @@ marked.inlineLexer = InlineLexer.output;

marked.parse = marked;

if (typeof module !== 'undefined' && typeof exports === 'object') {
if (typeof exports === 'object') {
module.exports = marked;
} else if (typeof define === 'function' && define.amd) {
define(function() { return marked; });
Expand Down
9 changes: 7 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ main:
}

try {
text = engine(file.text).replace(/\s/g, '');
html = file.html.replace(/\s/g, '');
if(filename !== 'fenced_code_list_item.gfm.text') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just saw this. Most of the tests get rid of whitespace, but whitespace is causing the problem that my pull request is fixing, so I didn't replace whitespace for my test's file.

text = engine(file.text).replace(/\s/g, '');
html = file.html.replace(/\s/g, '');
} else {
text = engine(file.text);
html = file.html;
}
} catch(e) {
console.log('%s failed.', filename);
throw e;
Expand Down
6 changes: 6 additions & 0 deletions test/new/fenced_code_list_item.gfm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ol>
<li><p>This is a list.</p>
<pre><code class="lang-python"> This line should stay indented.
</code></pre>
</li>
</ol>
5 changes: 5 additions & 0 deletions test/new/fenced_code_list_item.gfm.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. This is a list.

```python
This line should stay indented.
```
6 changes: 6 additions & 0 deletions test/tests/fenced_code_list_item.gfm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ol>
<li><p>This is a list.</p>
<pre><code class="lang-python"> This line should stay indented.
</code></pre>
</li>
</ol>
5 changes: 5 additions & 0 deletions test/tests/fenced_code_list_item.gfm.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. This is a list.

```python
This line should stay indented.
```