mdfmt

package
v0.0.0-...-857411a Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package mdfmt contains basic markdown reformatting functionality.

Use this package to write CLI help text as markdown and get nicely-rendered terminal output.

The sample CLI in cmd/sample uses this package to format cmd/sample/docs/testfile.md.

Run go run ./cmd/sample testfile to view the rendered document.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StaticColumns

func StaticColumns(cols int) func() int

StaticColumns is a static columns setting.

func WriteTable

func WriteTable(header []string, rows [][]string) string

WriteTable writes a markdown table with equal spaced columns. Text within the columns will be left-aligned.

func WriteTableWithAlignment

func WriteTableWithAlignment(header []string, rows [][]string, alignment []TextAlignment) string

WriteTableWithAlignment writes a markdown table with equal spaced columns. Text within the columns will be aligned according to the alignments given.

Types

type Formatter

type Formatter struct {
	Header    func(text string, loc Location) string      // reformats headers
	Link      func(text, url string, loc Location) string // reformats links
	Code      func(code string, loc Location) string      // reformats inline code blocks
	CodeBlock func(code string, loc Location) string      // reformats multiline code blocks
	Bold      func(text string, loc Location) string      // reformats bolded text
	Italics   func(text string, loc Location) string      // reformats italicized text
	Indent    func(loc Location) string                   // produces indent for a line's location

	// produce column width for wrapping
	// (nil function or 0 return value disables wrapping)
	Columns func() int

	// CodeBlockWrapMode signifies a code block wrapping style.
	CodeBlockWrapMode WrapMode
}

Formatter formats Markdown for terminal output.

func (*Formatter) Format

func (format *Formatter) Format(markdownText string) string

Format formats markdown text according the Formatter's rules.

type Location

type Location struct {
	Level          int    // Header level of the current section
	Header         bool   // Line is a header line
	CodeBlock      bool   // Line is within a multiline code block
	CodeBlockLang  string // Language identifier for the code block
	CodeBlockLevel int    // Number of "`" characters used to start the multiline code block
	Table          bool   // In a table
	Comment        bool   // Line is in an HTML comment
}

Location describes the current location of text in a markdown document.

type TextAlignment

type TextAlignment uint8

TextAlignment defines text alignment.

const (
	TextAlignmentDefault TextAlignment = iota
	TextAlignmentLeft
	TextAlignmentCenter
	TextAlignmentRight
)

Defined text alignments.

type WrapMode

type WrapMode uint8

WrapMode signifies a code block wrapping style.

const (
	Default                   WrapMode = iota
	WrapToCurrentIndentation           // Indent wrapped lines to the current line's indentation
	WrapToStartingIndentation          // Indent wrapped lines to the starting indentation of the sectin
)

Defined code block wrapping styles.

Jump to

Keyboard shortcuts

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