all repos — grayfriday @ b5ff8e0286f7dd5ba40cf0b26001b1dcdfec3993

blackfriday fork with a few changes

Break lines properly in TOC generation and omit content if required
Pierre Neidhardt ambrevar@gmail.com
Tue, 09 Aug 2016 14:59:22 +0530
commit

b5ff8e0286f7dd5ba40cf0b26001b1dcdfec3993

parent

4d756003cd4d43b837fffc8916825ddabe9b674e

1 files changed, 19 insertions(+), 11 deletions(-)

jump to
M html.gohtml.go

@@ -746,7 +746,8 @@ w.WriteString("<body>\n\n")

} func (r *HTMLRenderer) writeTOC(w *bytes.Buffer, ast *Node) { - w.WriteString("<nav>\n") + buf := bytes.Buffer{} + inHeader := false tocLevel := 0 headerCount := 0

@@ -755,42 +756,46 @@ ast.Walk(func(node *Node, entering bool) WalkStatus {

if node.Type == Header && !node.HeaderData.IsTitleblock { inHeader = entering if entering { - headerCount++ node.HeaderID = fmt.Sprintf("toc_%d", headerCount) if node.Level == tocLevel { - w.WriteString("</li>\n<li>") + buf.WriteString("</li>\n\n<li>") } else if node.Level < tocLevel { for node.Level < tocLevel { tocLevel-- - w.WriteString("</li>\n</ul>\n") + buf.WriteString("</li>\n</ul>") } - w.WriteString("<li>") + buf.WriteString("</li>\n\n<li>") } else { for node.Level > tocLevel { tocLevel++ - w.WriteString("\n<ul>\n<li>") + buf.WriteString("\n<ul>\n<li>") } } - fmt.Fprintf(w, `<a href="#toc_%d">`, headerCount) + fmt.Fprintf(&buf, `<a href="#toc_%d">`, headerCount) + headerCount++ } else { - w.WriteString("</a>") + buf.WriteString("</a>") } return GoToNext } if inHeader { - return r.RenderNode(w, node, entering) + return r.RenderNode(&buf, node, entering) } return GoToNext }) for ; tocLevel > 0; tocLevel-- { - w.WriteString("</li>\n</ul>\n") + buf.WriteString("</li>\n</ul>") } - w.WriteString("</nav>\n") + if buf.Len() > 0 { + w.WriteString("<nav>\n") + w.Write(buf.Bytes()) + w.WriteString("\n\n</nav>\n") + } } func (r *HTMLRenderer) writeDocumentFooter(w *bytes.Buffer) {

@@ -820,6 +825,9 @@ var buff bytes.Buffer

r.writeDocumentHeader(&buff, sr) if r.Extensions&TOC != 0 || r.Extensions&OmitContents != 0 { r.writeTOC(&buff, ast) + if r.Extensions&OmitContents != 0 { + return buff.Bytes() + } } ast.Walk(func(node *Node, entering bool) WalkStatus { return r.RenderNode(&buff, node, entering)