Fix consecutive lists of different kinds (#443)

* fixed consecutive lists
* used helper method and addressed flag setting
This commit is contained in:
Mitchell Cohen 2018-03-20 15:50:35 -04:00 committed by Vytautas Šaltenis
parent cfdcce5196
commit c455fd41c6
2 changed files with 36 additions and 6 deletions

View File

@ -1148,6 +1148,18 @@ func (p *Markdown) list(data []byte, flags ListType) int {
return i
}
// Returns true if the list item is not the same type as its parent list
func (p *Markdown) listTypeChanged(data []byte, flags *ListType) bool {
if p.dliPrefix(data) > 0 && *flags&ListTypeDefinition == 0 {
return true
} else if p.oliPrefix(data) > 0 && *flags&ListTypeOrdered == 0 {
return true
} else if p.uliPrefix(data) > 0 && (*flags&ListTypeOrdered != 0 || *flags&ListTypeDefinition != 0) {
return true
}
return false
}
// Returns true if block ends with a blank line, descending if needed
// into lists and sublists.
func endsWithBlankLine(block *Node) bool {
@ -1286,14 +1298,21 @@ gatherlines:
p.oliPrefix(chunk) > 0 ||
p.dliPrefix(chunk) > 0:
if containsBlankLine {
*flags |= ListItemContainsBlock
// to be a nested list, it must be indented more
// if not, it is either a different kind of list
// or the next item in the same list
if indent <= itemIndent {
if p.listTypeChanged(chunk, flags) {
*flags |= ListItemEndOfList
} else if containsBlankLine {
*flags |= ListItemContainsBlock
}
break gatherlines
}
// to be a nested list, it must be indented more
// if not, it is the next item in the same list
if indent <= itemIndent {
break gatherlines
if containsBlankLine {
*flags |= ListItemContainsBlock
}
// is this the first item in the nested list?

View File

@ -853,6 +853,17 @@ func TestDefinitionList(t *testing.T) {
doTestsBlock(t, tests, DefinitionLists)
}
func TestConsecutiveLists(t *testing.T) {
var tests = []string{
"1. Hello\n\n* Hello\n\nTerm 1\n: Definition a\n",
"<ol>\n<li>Hello</li>\n</ol>\n\n<ul>\n<li>Hello</li>\n</ul>\n\n<dl>\n<dt>Term 1</dt>\n<dd>Definition a</dd>\n</dl>\n",
"1. Not nested\n2. ordered list\n\n\t1. nested\n\t2. ordered list\n\n\t* nested\n\t* unordered list\n* Not nested\n* unordered list",
"<ol>\n<li><p>Not nested</p></li>\n\n<li><p>ordered list</p>\n\n<ol>\n<li>nested</li>\n<li>ordered list</li>\n</ol>\n\n<ul>\n<li>nested</li>\n<li>unordered list</li>\n</ul></li>\n</ol>\n\n<ul>\n<li>Not nested</li>\n<li>unordered list</li>\n</ul>\n",
}
doTestsBlock(t, tests, DefinitionLists)
}
func TestPreformattedHtml(t *testing.T) {
var tests = []string{
"<div></div>\n",