tocenize

package module
v0.0.0-...-e1e48c4 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: MIT Imports: 6 Imported by: 0

README

markdown-toc

Insert and maintain a table of contents in Markdown files.

Features

markdown-toc generates a TOC (table of content) from Markdown files and inserts or updates it in the given file.

  • Cross-platform command line utility
    • Windows, Linux, Mac and *bsd (anything the Go compiler will handle)
  • Anchor links are compatible to GFM (Github flavoured Markdown) and GitLab. It is configured using -style command line option.
  • Automatic "intelligent" insertion of new TOC
  • Update existing TOCs without moving it
  • Configurable max. and min. header depth
  • Line endings are kept intact (LF or CRLF is detected and then used for new lines)

Installation

GO111MODULE=on go get github.com/gtrafimenkov/markdown-toc/cmd/markdown-toc@0.0.2

Usage

The output of markdown-toc -h should be self explanatory:

markdown-toc [options] FILE...

  -d	print full diff to stdout
  -e	update only existing TOC (no insert)
  -indent string
    	string used for nesting (default "\t")
  -max int
    	maximum depth (default 99)
  -min int
    	minimum depth (default 1)
  -p	print full result to stdout
  -style string
    	style of the TOC: github or gitlab (default "github")
  -v	print version

Alternatives

Changes

All notable changes to this project will be documented in the [changelog].

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

License

This project is released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Indent = "\t"

Indent string used for nesting. See `-indent`.

Functions

This section is empty.

Types

type Document

type Document struct {
	Path  string
	Lines []string
	// contains filtered or unexported fields
}

func NewDocFromPath

func NewDocFromPath(path string) (Document, error)

func NewDocFromString

func NewDocFromString(s string, path string) Document

func (Document) FindTOC

func (d Document) FindTOC() (start, end int)

func (Document) String

func (d Document) String() string

func (Document) SuggestTOC

func (d Document) SuggestTOC(toc TOC) (start, end int)

SuggestTOC looks for the first heading below a root heading. A root heading has minimum depth and the depth must only be used once. e.g.

# Name
--here--
## A
## B

func (Document) Update

func (d Document) Update(toc TOC, existingOnly bool, style Style) (Document, error)

type Heading

type Heading struct {
	Title         string
	Depth         int
	Index         int
	UniqueCounter int
}

func NewHeadingATX

func NewHeadingATX(line string, index int) *Heading

func NewHeadingSE

func NewHeadingSE(title string, sep string, index int) *Heading

func (Heading) Anchor

func (h Heading) Anchor(style Style) string

func (Heading) GetMarkdown

func (h Heading) GetMarkdown(minDepth int, style Style) string

GetMarkdown returns the heading line of the TOC formatted to markdown

func (Heading) LinkTitle

func (h Heading) LinkTitle() string

LinkTitle returns a cleaned up title for use in links. It removes Markdown images and links, keeping only the link text.

type Job

type Job struct {
	MinDepth     int
	MaxDepth     int
	ExistingOnly bool
	Style        Style
}

type Style

type Style int

Style describes the style of the TOC we want to produce

const (
	// GitHub style of the TOC
	GitHub Style = iota

	// GitLab style of the TOC
	GitLab
)

type TOC

type TOC struct {
	Headings []Heading
	// contains filtered or unexported fields
}

func NewTOC

func NewTOC(doc Document, pj Job) TOC

func (TOC) MinDepth

func (t TOC) MinDepth() int

func (TOC) String

func (t TOC) String(style Style) string

Jump to

Keyboard shortcuts

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