toc

package module
v0.0.0-...-7bc0e0d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 8 Imported by: 1

README

goldmark-toc

GoDoc

Goldmark extension for generating table of content.

var source = []byte(`
# Title
paragraph text
## Section *1*
paragraph text
### Subsection *1.1*
paragraph text
## Section *2*
paragraph text
## Заголовок на русском
`)
info, err := toc.Convert(source, ioutil.Discard)
if err != nil {
	log.Fatal(err)
}
for _, header := range info.Headers {
	fmt.Printf("%+v\n", header)
}
{Level:1 Text:Title ID:title}
{Level:2 Text:Section 1 ID:section-1}
{Level:3 Text:Subsection 1.1 ID:subsection-1-1}
{Level:2 Text:Section 2 ID:section-2}
{Level:2 Text:Заголовок на русском ID:zagolovok-na-russkom}

Documentation

Overview

Package toc adds the ability to get a table of contents to the goldmark parser.

Example
package main

import (
	"fmt"
	"io/ioutil"
	"log"

	toc "github.com/mdigger/goldmark-toc"
)

func main() {
	var source = []byte(`
# Title
paragraph text
## Section *1*
paragraph text
### Subsection *1.1*
paragraph text
## Section *2*
paragraph text
## Заголовок на русском
`)
	toc, err := toc.Convert(source, ioutil.Discard)
	if err != nil {
		log.Fatal(err)
	}
	// fmt.Printf("%+v\n", info)
	for _, header := range toc {
		fmt.Printf("%+v\n", header)
	}
}
Output:

1
{Level:1 Text:Title ID:title}
{Level:2 Text:Section 1 ID:section-1}
{Level:3 Text:Subsection 1.1 ID:subsection-1-1}
{Level:2 Text:Section 2 ID:section-2}
{Level:2 Text:Заголовок на русском ID:zagolovok-na-russkom}

Index

Examples

Constants

This section is empty.

Variables

View Source
var Lang = ""

Lang define the default language for generating slug id fo headers.

Functions

func NewIDs

func NewIDs(lang string) parser.IDs

NewIDs return initialized ID generator for goldmark parser context.

func WithIDs

func WithIDs() parser.ParseOption

WithIDs return new initializer parser option with ID generator.

Types

type ConverterFunc

type ConverterFunc = func(source []byte, writer io.Writer) ([]Header, error)

ConverterFunc is markdown converting function.

func Markdown

func Markdown(m goldmark.Markdown) ConverterFunc

Markdown extends initialied goldmark.Markdown and return converter function.

func New

func New(options ...goldmark.Option) ConverterFunc

New return markdown converter with table of content support.

type Header struct {
	Level int
	Text  string
	ID    string
}

Header holds the data about a header.

func Convert

func Convert(source []byte, writer io.Writer) ([]Header, error)

Convert from markdown to html with default options and return TOC.

func Headers

func Headers(n ast.Node, source []byte) []Header

Headers return table of content from parsed markdown document.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL