withtoc

package module
v0.0.0-...-87c0614 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2019 License: MIT Imports: 8 Imported by: 0

README

goldmark-withtoc

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
`)
toc, err := withtoc.Convert(source, ioutil.Discard)
if err != nil {
	log.Fatal(err)
}
for _, header := range toc {
	fmt.Printf("%+v\n", header)
}
{ID:toc:01 Level:1 Text:Title}
{ID:toc:02 Level:2 Text:Section 1}
{ID:toc:03 Level:3 Text:Subsection 1.1}
{ID:toc:04 Level:2 Text:Section 2}

Documentation

Overview

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

Example
package main

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

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

func main() {
	var source = []byte(`
# Title
paragraph text
## Section *1*
paragraph text
### Subsection *1.1*
paragraph text
## Section *2*
paragraph text
`)
	toc, err := withtoc.Convert(source, ioutil.Discard)
	if err != nil {
		log.Fatal(err)
	}
	for _, header := range toc {
		fmt.Printf("%+v\n", header)
	}
}
Output:

{ID:toc:01 Level:1 Text:Title}
{ID:toc:02 Level:2 Text:Section 1}
{ID:toc:03 Level:3 Text:Subsection 1.1}
{ID:toc:04 Level:2 Text:Section 2}

Index

Examples

Constants

This section is empty.

Variables

View Source
var IDFormat = "toc:%02d"

IDFormat define the format of generated header ID.

Functions

func MarkdownHeaders

func MarkdownHeaders(w io.Writer, headers []Header, filename string)

MarkdownHeaders writes table of content in markdown format.

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 {
	ID    string
	Level int
	Text  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.

Jump to

Keyboard shortcuts

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