Documentation ¶
Overview ¶
Package anchor is an extension for the Goldmark Markdown parser adding support for anchors for all headers.
Example ¶
package main import ( "log" "os" "github.com/yuin/goldmark" "github.com/yuin/goldmark/parser" "go.abhg.dev/goldmark/anchor" ) func main() { md := goldmark.New( // We need to enable automatic generation of heading IDs. // Otherwise, none of the headings will have IDs // which will leave goldmark-anchor // nothing to generate anchors for. goldmark.WithParserOptions( parser.WithAutoHeadingID(), ), goldmark.WithExtensions( &anchor.Extender{}, ), ) src := []byte("# Foo") if err := md.Convert(src, os.Stdout); err != nil { log.Fatal(err) } }
Output: <h1 id="foo">Foo <a class="anchor" href="#foo">¶</a></h1>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Kind = ast.NewNodeKind("Anchor")
Kind is the NodeKind used by anchor nodes.
Functions ¶
This section is empty.
Types ¶
type Attributer ¶
type Attributer interface { // AnchorAttributes returns the attributes // that should be attached to the anchor node // for the given header. // // If AnchorAttributes returns an empty map or nil, // no attributes will be added. AnchorAttributes(*HeaderInfo) map[string]string }
Attributer determines attributes that will be attached to an anchor node.
By default, we will add 'class="anchor"' to all nodes.
type Attributes ¶
Attributes is an Attributer that uses a constant set of attributes for all anchor nodes.
Pass this into Extender or Transformer to specify custom attributes.
anchor.Extender{ Attributer: Attributes{"class": "permalink"}, }
func (Attributes) AnchorAttributes ¶
func (as Attributes) AnchorAttributes(*HeaderInfo) map[string]string
AnchorAttributes reports the attributes associated with this object for all headers.
type Extender ¶
type Extender struct { // Texter determines the anchor text. // // Defaults to '¶' if unspecified. Texter Texter // Position specifies where the anchor will be placed in a header. // // Defaults to After. Position Position // Attributer determines the attributes // that will be associated with the anchor link. // // Defaults to adding a 'class="anchor"' attribute. Attributer Attributer }
Extender adds support for anchors to a Goldmark Markdown parser.
Use it by installing it into the goldmark.Markdown object upon creation. For example:
goldmark.New( // ... goldmark.WithExtensions( // ... &anchor.Extender{}, ), )
type HeaderInfo ¶
type HeaderInfo struct { // Level of the header. Level int // Identifier for the header on the page. // This will typically become part of the URL fragment. ID []byte }
HeaderInfo holds information about a header for which an anchor is being considered.
type Node ¶
type Node struct { ast.BaseInline // ID of the header this anchor is for. ID []byte // Level of the header that this anchor is for. Level int // Value is the text inside the anchor. // Typically this is a fixed string // like '¶' or '#'. Value []byte }
Node is an anchor node in the Markdown AST.
type Position ¶
type Position int
Position specifies where inside a heading we should place an anchor Node.
type Renderer ¶
type Renderer struct { // Position specifies where in the header text // the anchor is being added. Position Position }
Renderer renders anchor [Node]s.
func (*Renderer) RegisterFuncs ¶
func (r *Renderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)
RegisterFuncs registers functions against the provided goldmark Registerer.
type Texter ¶
type Texter interface { // AnchorText returns the anchor text // that should be used for the provided header info. // // If AnchorText returns an empty slice or nil, // an anchor will not be generated for this header. AnchorText(*HeaderInfo) []byte }
Texter determines the anchor text.
This is the clickable text displayed next to the header which tells readers that they can use it as an anchor to the header.
By default, we will use the string '¶'.
type Transformer ¶
type Transformer struct { // Texter determines the anchor text. // // Defaults to '¶' for all headers if unset. Texter Texter // Position specifies where the anchor will be placed in a header. // // Defaults to After. Position Position // Attributer determines the attributes // that will be associated with the anchor link. // // Defaults to adding a 'class="anchor"' attribute // for all headers if unset. Attributer Attributer }
Transformer transforms a Goldmark Markdown AST, adding anchor Node objects for headers across the document.