53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
|
|
package mdprint
|
||
|
|
|
||
|
|
import "fmt"
|
||
|
|
|
||
|
|
// Node is the interface for all AST nodes.
|
||
|
|
type Node interface{ isNode() }
|
||
|
|
|
||
|
|
// ---- block-level ----
|
||
|
|
|
||
|
|
type Heading struct{ Level int; Content []Node }
|
||
|
|
type Paragraph struct{ Content []Node }
|
||
|
|
type CodeBlock struct{ Lang, Body string }
|
||
|
|
type Blockquote struct{ Children []Node }
|
||
|
|
type List struct{ Ordered bool; Items []ListItem }
|
||
|
|
type ListItem struct{ Checked *bool; Content []Node }
|
||
|
|
type Table struct{ Headers []string; Rows [][]string }
|
||
|
|
type ThematicBreak struct{}
|
||
|
|
|
||
|
|
// ---- inline-level ----
|
||
|
|
|
||
|
|
type Text struct{ Text string }
|
||
|
|
type Bold struct{ Content []Node }
|
||
|
|
type Italic struct{ Content []Node }
|
||
|
|
type Code struct{ Text string }
|
||
|
|
type Link struct{ Content []Node; URL string }
|
||
|
|
|
||
|
|
func (Heading) isNode() {}
|
||
|
|
func (Paragraph) isNode() {}
|
||
|
|
func (CodeBlock) isNode() {}
|
||
|
|
func (Blockquote) isNode() {}
|
||
|
|
func (List) isNode() {}
|
||
|
|
func (ListItem) isNode() {}
|
||
|
|
func (Table) isNode() {}
|
||
|
|
func (ThematicBreak) isNode() {}
|
||
|
|
|
||
|
|
func (Text) isNode() {}
|
||
|
|
func (Bold) isNode() {}
|
||
|
|
func (Italic) isNode() {}
|
||
|
|
func (Code) isNode() {}
|
||
|
|
func (Link) isNode() {}
|
||
|
|
|
||
|
|
// Print parses Markdown content and renders it to the terminal.
|
||
|
|
func Print(content string) {
|
||
|
|
blocks := parseBlocks(content)
|
||
|
|
for i, b := range blocks {
|
||
|
|
if i > 0 {
|
||
|
|
fmt.Println()
|
||
|
|
}
|
||
|
|
fmt.Print(renderNode(b))
|
||
|
|
}
|
||
|
|
fmt.Println()
|
||
|
|
}
|