fix(markdown): close a definition list before the paragraph that follows it
This commit is contained in:
@@ -614,6 +614,9 @@ func (p *parser) finalise(n *Node) {
|
||||
n.content = trimTrailingBlankLines(n.content)
|
||||
}
|
||||
case kindList, kindDefList:
|
||||
if n.kind == kindDefList {
|
||||
p.liftLeakedTerms(n)
|
||||
}
|
||||
n.tight = !blocksAreLoose(n.children)
|
||||
case kindDocument:
|
||||
p.gatherFootnotes(n)
|
||||
@@ -623,6 +626,60 @@ func (p *parser) finalise(n *Node) {
|
||||
}
|
||||
}
|
||||
|
||||
// liftLeakedTerms moves the paragraphs that never became terms out of a
|
||||
// definition list. A paragraph may sit directly inside the list as a
|
||||
// term waiting for its definition line; when that line never arrives the
|
||||
// paragraph is prose after the list, and the list ends before it, since
|
||||
// a dl element carries nothing but terms and definitions. Terms and
|
||||
// definitions after the leak form a list of their own.
|
||||
func (p *parser) liftLeakedTerms(dl *Node) {
|
||||
leak := -1
|
||||
for i, c := range dl.children {
|
||||
if c.kind == kindParagraph {
|
||||
leak = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if leak < 0 {
|
||||
return
|
||||
}
|
||||
parent := dl.parent
|
||||
base := 0
|
||||
for i, c := range parent.children {
|
||||
if c == dl {
|
||||
base = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
tail := dl.children[leak:]
|
||||
dl.children = dl.children[:leak]
|
||||
insert := func(n *Node) {
|
||||
parent.children = append(parent.children, nil)
|
||||
copy(parent.children[base+1:], parent.children[base:])
|
||||
parent.children[base] = n
|
||||
base++
|
||||
n.parent = parent
|
||||
}
|
||||
var regroup *Node
|
||||
for _, c := range tail {
|
||||
switch c.kind {
|
||||
case kindParagraph:
|
||||
insert(c)
|
||||
regroup = nil
|
||||
case kindDefTerm, kindDefItem:
|
||||
if regroup == nil {
|
||||
regroup = &Node{kind: kindDefList, parent: parent}
|
||||
insert(regroup)
|
||||
}
|
||||
c.parent = regroup
|
||||
regroup.children = append(regroup.children, c)
|
||||
}
|
||||
}
|
||||
if regroup != nil {
|
||||
regroup.tight = !blocksAreLoose(regroup.children)
|
||||
}
|
||||
}
|
||||
|
||||
// trimTrailingBlankLines removes the trailing blank lines of an indented
|
||||
// code block. Every content line carries its newline, so the result of a
|
||||
// non-empty block ends with exactly one.
|
||||
|
||||
Reference in New Issue
Block a user