Documentation
¶
Overview ¶
Package toc provides support for building a Table of Contents from a goldmark Markdown document.
The package operates in two stages: inspection and rendering. During inspection, the package analyzes an existing Markdown document, and builds a Table of Contents from it.
markdown := goldmark.New(...) parser := markdown.Parser() doc := parser.Parse(text.NewReader(src)) tocTree, err := toc.Inspect(doc, src)
During rendering, it converts the Table of Contents into a list of headings with nested items under each as a goldmark Markdown document. You may manipulate the TOC, removing items from it or simplifying it, before rendering.
tocList := toc.RenderList(tocTree)
You can render that Markdown document using goldmark into whatever form you prefer.
renderer := markdown.Renderer() renderer.Render(out, src, tocList)
The following diagram summarizes the flow of information with goldmark-toc.
src
+--------+ +-------------------+
| | goldmark/Parser.Parse | |
| []byte :---------------------------> goldmark/ast.Node |
| | | |
+---.----+ +-------.-----.-----+
| | |
'----------------. .-----------------' |
\ / |
\ / |
| |
| toc.Inspect |
| |
+----v----+ |
| | |
| toc.TOC | |
| | |
+----.----+ |
| |
| toc/Renderer.Render |
| |
+---------v---------+ |
| | |
| goldmark/ast.Node | |
| | |
+---------.---------+ |
| |
'-------. .--------------'
\ /
|
goldmark/Renderer.Render |
|
v
+------+
| HTML |
+------+
Deprecated: Use "go.abhg.dev/goldmark/toc" instead.
Example ¶
package main
import (
"os"
toc "github.com/abhinav/goldmark-toc"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
func main() {
src := []byte(`
# A section
Hello
# Another section
## A sub-section
### A sub-sub-section
Bye
`)
markdown := goldmark.New()
// Request that IDs are automatically assigned to headers.
markdown.Parser().AddOptions(parser.WithAutoHeadingID())
// Alternatively, we can provide our own implementation of parser.IDs
// and use,
//
// pctx := parser.NewContext(parser.WithIDs(ids))
// doc := parser.Parse(text.NewReader(src), parser.WithContext(pctx))
doc := markdown.Parser().Parse(text.NewReader(src))
// Inspect the parsed Markdown document to find headers and build a
// tree for the table of contents.
tree, err := toc.Inspect(doc, src)
if err != nil {
panic(err)
}
// Render the tree as-is into a Markdown list.
treeList := toc.RenderList(tree)
// Render the Markdown list into HTML.
markdown.Renderer().Render(os.Stdout, src, treeList)
}
Output: <ul> <li> <a href="#a-section">A section</a></li> <li> <a href="#another-section">Another section</a><ul> <li> <a href="#a-sub-section">A sub-section</a><ul> <li> <a href="#a-sub-sub-section">A sub-sub-section</a></li> </ul> </li> </ul> </li> </ul>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderList ¶
RenderList renders a table of contents as a nested list with a sane, default configuration for the ListRenderer.
Types ¶
type Extender ¶ added in v0.2.0
Extender extends a Goldmark Markdown parser and renderer to always include a table of contents in the output.
To use this, install it into your Goldmark Markdown object.
md := goldmark.New(
// ...
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
goldmark.WithExtensions(
// ...
&toc.Extender{
},
),
)
This will install the default Transformer. For more control, install the Transformer directly on the Markdown Parser.
NOTE: Unless you've supplied your own parser.IDs implementation, you'll need to enable the WithAutoHeadingID option on the parser to generate IDs and links for headings.
type InspectOption ¶
type InspectOption interface {
// contains filtered or unexported methods
}
InspectOption customizes the behavior of Inspect.
This type is currently just a placeholder to prevent breaking changes to the API in the future. There are no InspectOptions at this time.
type ListRenderer ¶
type ListRenderer = toc.ListRenderer
ListRenderer builds a nested list from a table of contents.
For example,
# Foo ## Bar ## Baz # Qux
Becomes,
- Foo
- Bar
- Baz
- Qux
type TOC ¶
TOC is the table of contents. It's the top-level object under which the rest of the table of contents resides.
func Inspect ¶
Inspect builds a table of contents by inspecting the provided document.
The table of contents is represents as a tree where each item represents a heading or a heading level with zero or more children.
For example,
# Section 1 ## Subsection 1.1 ## Subsection 1.2 # Section 2 ## Subsection 2.1 # Section 3
Will result in the following items.
TOC{Items: ...}
|
+--- &Item{Title: "Section 1", ID: "section-1", Items: ...}
| |
| +--- &Item{Title: "Subsection 1.1", ID: "subsection-1-1"}
| |
| +--- &Item{Title: "Subsection 1.2", ID: "subsection-1-2"}
|
+--- &Item{Title: "Section 2", ID: "section-2", Items: ...}
| |
| +--- &Item{Title: "Subsection 2.1", ID: "subsection-2-1"}
|
+--- &Item{Title: "Section 3", ID: "section-3"}
You may analyze or manipulate the table of contents before rendering it.
type Transformer ¶ added in v0.2.0
type Transformer = toc.Transformer
Transformer is a Goldmark AST transformer adds a TOC to the top of a Markdown document.
To use this, either install the Extender on the goldmark.Markdown object, or install the AST transformer on the Markdown parser like so.
markdown := goldmark.New(...)
markdown.Parser().AddOptions(
parser.WithAutoHeadingID(),
parser.WithASTTransformers(
util.Prioritized(&toc.Transformer{}, 100),
),
)
NOTE: Unless you've supplied your own parser.IDs implementation, you'll need to enable the WithAutoHeadingID option on the parser to generate IDs and links for headings.