(w io.Writer, ast *Node)
| 898 | } |
| 899 | |
| 900 | func (r *HTMLRenderer) writeTOC(w io.Writer, ast *Node) { |
| 901 | buf := bytes.Buffer{} |
| 902 | |
| 903 | inHeading := false |
| 904 | tocLevel := 0 |
| 905 | headingCount := 0 |
| 906 | |
| 907 | ast.Walk(func(node *Node, entering bool) WalkStatus { |
| 908 | if node.Type == Heading && !node.HeadingData.IsTitleblock { |
| 909 | inHeading = entering |
| 910 | if entering { |
| 911 | node.HeadingID = fmt.Sprintf("toc_%d", headingCount) |
| 912 | if node.Level == tocLevel { |
| 913 | buf.WriteString("</li>\n\n<li>") |
| 914 | } else if node.Level < tocLevel { |
| 915 | for node.Level < tocLevel { |
| 916 | tocLevel-- |
| 917 | buf.WriteString("</li>\n</ul>") |
| 918 | } |
| 919 | buf.WriteString("</li>\n\n<li>") |
| 920 | } else { |
| 921 | for node.Level > tocLevel { |
| 922 | tocLevel++ |
| 923 | buf.WriteString("\n<ul>\n<li>") |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | fmt.Fprintf(&buf, `<a href="#toc_%d">`, headingCount) |
| 928 | headingCount++ |
| 929 | } else { |
| 930 | buf.WriteString("</a>") |
| 931 | } |
| 932 | return GoToNext |
| 933 | } |
| 934 | |
| 935 | if inHeading { |
| 936 | return r.RenderNode(&buf, node, entering) |
| 937 | } |
| 938 | |
| 939 | return GoToNext |
| 940 | }) |
| 941 | |
| 942 | for ; tocLevel > 0; tocLevel-- { |
| 943 | buf.WriteString("</li>\n</ul>") |
| 944 | } |
| 945 | |
| 946 | if buf.Len() > 0 { |
| 947 | io.WriteString(w, "<nav>\n") |
| 948 | w.Write(buf.Bytes()) |
| 949 | io.WriteString(w, "\n\n</nav>\n") |
| 950 | } |
| 951 | r.lastOutputLen = buf.Len() |
| 952 | } |
no test coverage detected