Skip to content

Commit

Permalink
fix issue with loose_item_end detection breaking
Browse files Browse the repository at this point in the history
and add previously failing text case for it
  • Loading branch information
notslang committed Mar 27, 2015
1 parent 3d528c3 commit df89eae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
27 changes: 6 additions & 21 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,19 @@ preprocessAST = (ast) ->
# this is actually all we need the list start token for
orderedList = currentToken.ordered
else if currentToken.type in nestingStartTokens
nestingStartToken = currentToken.type
tokenIndex = nestingStartTokens.indexOf(nestingStartToken)
nestingEndToken = nestingEndTokens[tokenIndex]
tokenIndex = nestingStartTokens.indexOf(currentToken.type)
currentToken.type = nestContainingTokens[tokenIndex]
i++

# if other nestingStartTokens of the same type open while looking for the
# end of this subAST, make sure not to steal their nestingEndToken
# there is no `loose_item_end` token, so we don't actually check the type
# of the token that's closing a given nesting level... we just assume it's
# correct.
nestingLevel = 1
subAST = []
loop
if ast[i].type is nestingEndToken
if ast[i].type in nestingEndTokens
nestingLevel--
if ast[i].type is nestingStartToken
else if ast[i].type in nestingStartTokens
nestingLevel++

if nestingLevel is 0
Expand Down Expand Up @@ -285,20 +284,6 @@ module.exports = (dirtyMarkdown, options = {}) ->

# remove all the `space` and `list_end` tokens - they're useless
ast = ast.filter (token) -> token.type not in ['space', 'list_end']

# there is no `loose_item_end` token, so we need to make it. thankfully, it's
# pretty easy to determine where they go, by finding `list_item_end` wherever
# there's an unclosed `loose_item`
openLooseItem = false
ast = ast.map (token) ->
if token.type is 'loose_item_start'
openLooseItem = true
else if token.type is 'list_item_end' and openLooseItem
openLooseItem = false
# replace the list_item_end with loose_item_end
token = type: 'loose_item_end'
return token

ast = preprocessAST(ast)
ast = fixHeaders(ast, options.ensureFirstHeaderIsH1)

Expand Down
22 changes: 22 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@ describe 'lists', ->
- last item
''')

it 'should normalize unordered nested lists (2)', ->
tidyMdSnippet('''
+ adas
- asdas
- sdas
+ sdfsdas
- sddfasdfdfs
- sdfafasdfsa
+ asdfads
''').should.equal('''
- adas
- asdas
- sdas
- sdfsdas
- sddfasdfdfs
- sdfafasdfsa
- asdfads
''')

it 'should normalize ordered lists', ->
tidyMdSnippet('''
# H1 header
Expand Down

0 comments on commit df89eae

Please sign in to comment.