markdown

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package markdown renders the given goldmark AST to Markdown.

Index

Constants

This section is empty.

Variables

View Source
var (
	NewLineChar             = []byte{'\n'}
	SpaceChar               = []byte{' '}
	StrikeThroughChars      = []byte("~~")
	ThematicBreakChars      = []byte("---")
	BlockquoteChars         = []byte{'>', ' '}
	CodeBlockChars          = []byte("```")
	TableHeaderColChar      = []byte{'-'}
	TableHeaderAlignColChar = []byte{':'}
	Heading1UnderlineChar   = []byte{'='}
	Heading2UnderlineChar   = []byte{'-'}
	FourSpacesChars         = bytes.Repeat([]byte{' '}, 4)
)
View Source
var GoCodeFormatter = CodeFormatter{
	Name:    "go",
	Aliases: []string{"Go"},
	Format:  formatGo,
}

GoCodeFormatter is a CodeFormatter that reformats Go source code inside fenced code blocks tagged with 'go' or 'Go'.

```go
func main() {
}
```

Supply it to the renderer with WithCodeFormatters.

Functions

This section is empty.

Types

type CodeFormatter

type CodeFormatter struct {
	// Name of the language.
	Name string

	// Aliases for the language, if any.
	Aliases []string

	// Function to format the code snippet.
	// In case of errors, format functions should typically return
	// the original string unchanged.
	Format func([]byte) []byte
}

CodeFormatter reformats code samples found in the document, matching them by name.

type LineIndentWriter

type LineIndentWriter struct {
	io.Writer
	// contains filtered or unexported fields
}

LineIndentWriter wraps io.Writer and adds given indent everytime new line is created .

func (*LineIndentWriter) AddIndentOnFirstWrite

func (l *LineIndentWriter) AddIndentOnFirstWrite(add []byte)

func (*LineIndentWriter) DelIndentOnFirstWrite

func (l *LineIndentWriter) DelIndentOnFirstWrite(del []byte)

func (*LineIndentWriter) PopIndent

func (l *LineIndentWriter) PopIndent()

func (*LineIndentWriter) PushIndent

func (l *LineIndentWriter) PushIndent(indent []byte)

func (*LineIndentWriter) WasIndentOnFirstWriteWritten

func (l *LineIndentWriter) WasIndentOnFirstWriteWritten() bool

func (*LineIndentWriter) Write

func (l *LineIndentWriter) Write(b []byte) (n int, _ error)

type ListIndentStyle

type ListIndentStyle int

ListIndentStyle specifies how items nested inside lists should be indented.

const (
	// ListIndentAligned specifies that items inside a list item
	// should be aligned to the content in the first item.
	//
	//	- First paragraph.
	//
	//	  Second paragraph aligned with the first.
	//
	// This applies to ordered lists too.
	//
	//	1. First paragraph.
	//
	//	   Second paragraph aligned with the first.
	//
	//	...
	//
	//	10. Contents.
	//
	//	    Long lists indent content further.
	//
	// This is the default.
	ListIndentAligned ListIndentStyle = iota

	// ListIndentUniform specifies that items inside a list item
	// should be aligned uniformly with 4 spaces.
	//
	// For example:
	//
	//	- First paragraph.
	//
	//	    Second paragraph indented 4 spaces.
	//
	// For ordered lists:
	//
	//	1. First paragraph.
	//
	//	    Second paragraph indented 4 spaces.
	//
	//	...
	//
	//	10. Contents.
	//
	//	    Always indented 4 spaces.
	ListIndentUniform
)

type NodeRenderer

type NodeRenderer interface {
	Render(r *Render, node ast.Node, entering bool) (ast.WalkStatus, error)
}

func WithAfterRender

func WithAfterRender(renderer NodeRenderer, after NodeRendererHookFunc) NodeRenderer

func WithBeforeRender

func WithBeforeRender(renderer NodeRenderer, before NodeRendererHookFunc) NodeRenderer

type NodeRendererHookFunc

type NodeRendererHookFunc func(r *Render, node ast.Node, entering bool)

type Option

type Option interface {
	renderer.Option
	// contains filtered or unexported methods
}

Option customizes the behavior of the markdown renderer.

func WithCodeFormatters

func WithCodeFormatters(fs ...CodeFormatter) Option

WithCodeFormatters changes the functions used to reformat code blocks found in the original file.

formatters := []markdown.CodeFormatter{
	markdown.GoCodeFormatter,
	// ...
}
r := NewRenderer()
r.AddMarkdownOptions(WithCodeFormatters(formatters...))

Defaults to empty.

func WithEmphasisToken

func WithEmphasisToken(c rune) Option

WithEmphasisToken specifies the character used to wrap emphasised text. Per the CommonMark spec, valid values are '*' and '_'.

Defaults to '*'.

func WithListIndentStyle

func WithListIndentStyle(style ListIndentStyle) Option

WithListIndentStyle specifies how contents nested under a list item should be indented.

Defaults to ListIndentAligned.

func WithNodeRenderer

func WithNodeRenderer(kind ast.NodeKind, renderer NodeRenderer) Option

WithNodeRenderer configures the renderer to use a custom renderer for the nodes of the given kind.

func WithNodeRenderers

func WithNodeRenderers(renderers map[ast.NodeKind]NodeRenderer) Option

WithNodeRenderer configures the renderer to use the given renderers for the nodes of the mapped kind.

func WithSoftWraps

func WithSoftWraps() Option

WithSoftWraps allows you to wrap lines even on soft line breaks.

func WithStrongToken

func WithStrongToken(s string) Option

WithStrongToken specifies the string used to wrap bold text. Per the CommonMark spec, valid values are '**' and '__'.

Defaults to repeating the emphasis token twice. See WithEmphasisToken for how to change that.

func WithUnderlineHeadings

func WithUnderlineHeadings() Option

WithUnderlineHeadings configures the renderer to use Setext-style headers (=== and ---).

type Render

type Render struct {
	// contains filtered or unexported fields
}

Render represents a single markdown rendering operation.

func (*Render) EmphToken

func (r *Render) EmphToken() []byte

func (*Render) RenderNode

func (r *Render) RenderNode(node ast.Node, entering bool) (ast.WalkStatus, error)

func (*Render) Renderer

func (r *Render) Renderer() *Renderer

func (*Render) Source

func (r *Render) Source() []byte

func (*Render) StrongToken

func (r *Render) StrongToken() []byte

func (*Render) WrapNonEmptyContentWith

func (r *Render) WrapNonEmptyContentWith(b []byte, entering bool) ast.WalkStatus

func (*Render) Writer

func (r *Render) Writer() *LineIndentWriter

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer allows to render markdown AST into markdown bytes in consistent format. Render is reusable across Renders, it holds configuration only.

func NewRenderer

func NewRenderer() *Renderer

NewRenderer builds a new Markdown renderer with default settings. To use this with goldmark.Markdown, use the goldmark.WithRenderer option.

r := markdown.NewRenderer()
md := goldmark.New(goldmark.WithRenderer(r))
md.Convert(src, w)

Alternatively, you can call Renderer.Render directly.

r := markdown.NewRenderer()
r.Render(w, src, node)

Use Renderer.AddMarkdownOptions to customize the output of the renderer.

func (*Renderer) AddMarkdownOptions

func (mr *Renderer) AddMarkdownOptions(opts ...Option)

AddMarkdownOptions modifies the Renderer with the given options.

func (*Renderer) AddOptions

func (mr *Renderer) AddOptions(opts ...renderer.Option)

AddOptions pulls Markdown renderer specific options from the given list, and applies them to the renderer.

func (*Renderer) Formatters

func (r *Renderer) Formatters() map[string]func([]byte) []byte

func (*Renderer) ListIndentStyle

func (r *Renderer) ListIndentStyle() ListIndentStyle

func (*Renderer) NewRender

func (mr *Renderer) NewRender(w io.Writer, source []byte) *Render

func (*Renderer) Render

func (mr *Renderer) Render(w io.Writer, source []byte, node ast.Node) error

Render renders the given AST node to the given writer, given the original source from which the node was parsed.

NOTE: This is the entry point used by Goldmark.

func (*Renderer) SoftWraps

func (r *Renderer) SoftWraps() bool

func (*Renderer) UnderlineHeadings

func (r *Renderer) UnderlineHeadings() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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