For simple list items, don't wrap in <p> tags

This also removes extra newlines for simple list items, see changes to
tests for details.

Closes #57
This commit is contained in:
Ori 2021-06-22 16:57:09 -04:00 committed by Niklas Fasching
parent 429602aba7
commit 4d3a099922
9 changed files with 146 additions and 319 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"html"
"log"
"reflect"
"regexp"
"strconv"
"strings"
@ -415,8 +416,8 @@ func (w *HTMLWriter) WriteListItem(li ListItem) {
if li.Status != "" {
attributes += fmt.Sprintf(` class="%s"`, listItemStatuses[li.Status])
}
w.WriteString(fmt.Sprintf("<li%s>\n", attributes))
WriteNodes(w, li.Children...)
w.WriteString(fmt.Sprintf("<li%s>", attributes))
w.writeListItemContent(li.Children)
w.WriteString("</li>\n")
}
@ -433,11 +434,26 @@ func (w *HTMLWriter) WriteDescriptiveListItem(di DescriptiveListItem) {
w.WriteString("?")
}
w.WriteString("\n</dt>\n")
w.WriteString("<dd>\n")
WriteNodes(w, di.Details...)
w.WriteString("<dd>")
w.writeListItemContent(di.Details)
w.WriteString("</dd>\n")
}
func (w *HTMLWriter) writeListItemContent(children []Node) {
if isParagraphNodeSlice(children) {
for i, c := range children {
out := w.WriteNodesAsString(c.(Paragraph).Children...)
if i != 0 && out != "" {
w.WriteString("\n")
}
w.WriteString(out)
}
} else {
w.WriteString("\n")
WriteNodes(w, children...)
}
}
func (w *HTMLWriter) WriteParagraph(p Paragraph) {
if len(p.Children) == 0 {
return
@ -585,6 +601,15 @@ func setHTMLAttribute(attributes []h.Attribute, k, v string) []h.Attribute {
return append(attributes, h.Attribute{Namespace: "", Key: k, Val: v})
}
func isParagraphNodeSlice(ns []Node) bool {
for _, n := range ns {
if reflect.TypeOf(n).Name() != "Paragraph" {
return false
}
}
return true
}
func (fs *footnotes) add(f FootnoteLink) int {
if i, ok := fs.mapping[f.Name]; ok && f.Name != "" {
return i