mark

package
v0.0.0-...-8f62ba8 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: MIT, MIT Imports: 7 Imported by: 0

README

Mark Test coverage Build status Go doc license

A markdown processor written in Go. built for fun.

Mark is a markdown processor that supports all the features of GFM, smartypants and smart-fractions rendering.
It was built with a nice-ish concurrency model that fully inspired from Rob Pike - Lexical Scanning talk and marked project.
Please note that any contribution is welcomed and appreciated, so feel free to take some task here.

Table of contents:

Get Started
Installation
$ go get github.com/a8m/mark
Examples

Add to your project:

import (
	"fmt"
	"github.com/a8m/mark"
)

func main() {
	html := mark.Render("I am using __markdown__.")
	fmt.Println(html)
	// <p>I am using <strong>markdown</strong>.</p>
}

or using as a command line tool:

1. install:

$ go get github.com/a8m/mark/cmd/mark

2. usage:

$ echo 'hello __world__...' | mark -smartypants

or:

$ mark -i hello.text -o hello.html
Documentation
Render

Staic rendering function.

html := mark.Render("I am using __markdown__.")
fmt.Println(html)
// <p>I am using <strong>markdown</strong>.</p>
Mark
New

New get string as an input, and mark.Options as configuration and return a new Mark.

m := mark.New("hello world...", &mark.Options{
    Smartypants: true,
})
fmt.Println(m.Render())
// <p>hello world…</p>
// Note: you can instantiate it like so: mark.New("...", nil) to get the default options.
Mark.AddRenderFn

AddRenderFn let you pass NodeType, and RenderFn function and override the default Node rendering.
To get all Nodes type and their fields/methods, see the full documentation: go-doc

Example 1:

m := mark.New("hello", nil)
m.AddRenderFn(mark.NodeParagraph, func(node mark.Node) (s string) {
    p, _ := node.(*mark.ParagraphNode)
    s += "<p class=\"mv-msg\">"
    for _, n := range p.Nodes {
        s += n.Render()
    }
    s += "</p>"
    return
})
fmt.Println(m.Render())
// <p class="mv-msg">hello</p>

Example 2:

m := mark.New("# Hello world", &mark.Options{
	Smartypants: true,
	Fractions:   true,
})
m.AddRenderFn(mark.NodeHeading, func(node mark.Node) string {
	h, _ := node.(*mark.HeadingNode)
	return fmt.Sprintf("<angular-heading-directive level=\"%d\" text=\"%s\"/>", h.Level, h.Text)
})
fmt.Println(m.Render())
// <angular-heading-directive level="1" text="Hello world"/>
Mark.Render

Parse and render input.

m := mark.New("hello", nil)
fmt.Println(m.Render())
// <p>hello</p>
Smartypants and Smartfractions

Mark also support smartypants and smartfractions rendering

func main() {
	opts := mark.DefaultOptions()
	opts.Smartypants = true
	opts.Fractions = true
	m := mark.New("'hello', 1/2 beer please...", opts)
	fmt.Println(m.Render())
	// ‘hello’, ½ beer please…
}
Todo
  • Commonmark support v0.2
  • Expand documentation
  • Configuration options
    • gfm, table
    • heading(auto hashing)
License

MIT

Documentation

Index

Constants

View Source
const (
	Header = iota
	Data
)

Cell types

Variables

This section is empty.

Functions

func DumpFile

func DumpFile(fn string)

func DumpNode

func DumpNode(node Node, level int) string

func NewParse

func NewParse(input string, opts *Options) *parse

Return new parser

func ParseFile

func ParseFile(fn string) *parse

func Render

func Render(input string) string

Staic render function

func RenderFile

func RenderFile(fn string) string

Types

type AlignType

type AlignType int

AlignType identifies the aligment-type of specfic cell.

const (
	None AlignType = iota
	Right
	Left
	Center
)

Alignment

func (AlignType) Align

func (t AlignType) Align() AlignType

Align returns itself and provides an easy default implementation for embedding in a Node.

type BlockQuoteNode

type BlockQuoteNode struct {
	NodeType
	Pos
	Nodes []Node
}

BlockQuote represents block-quote tag.

func (*BlockQuoteNode) NodeName

func (n *BlockQuoteNode) NodeName() (s string)

func (*BlockQuoteNode) Render

func (n *BlockQuoteNode) Render() string

Render returns the html representation of BlockQuote

type BrNode

type BrNode struct {
	NodeType
	Pos
}

BrNode represents a link-break element.

func (*BrNode) NodeName

func (n *BrNode) NodeName() (s string)

func (*BrNode) Render

func (n *BrNode) Render() string

Render returns the html representation of line-break.

type CellNode

type CellNode struct {
	NodeType
	Pos
	AlignType
	Kind  int
	Nodes []Node
}

CellNode represents table-data/cell that holds simple text(may be emphasis) Note: the text in <th> elements are bold and centered by default.

func (*CellNode) NodeName

func (n *CellNode) NodeName() (s string)

func (*CellNode) Render

func (c *CellNode) Render() string

Render returns the html reprenestation of table-cell

func (*CellNode) Style

func (c *CellNode) Style() string

Style return the cell-style based on alignment field

type CheckboxNode

type CheckboxNode struct {
	NodeType
	Pos
	Checked bool
}

CheckboxNode represents checked and unchecked checkbox tag. Used in task lists.

func (*CheckboxNode) NodeName

func (n *CheckboxNode) NodeName() (s string)

func (*CheckboxNode) Render

func (n *CheckboxNode) Render() string

Render returns the html representation of checked and unchecked CheckBox.

type CodeNode

type CodeNode struct {
	NodeType
	Pos
	Lang, Text string
}

Code holds CodeBlock node with specific lang field.

func (*CodeNode) NodeName

func (n *CodeNode) NodeName() (s string)

func (*CodeNode) Render

func (n *CodeNode) Render() string

Return the html representation of codeBlock

type DefLinkNode

type DefLinkNode struct {
	NodeType
	Pos
	Name, Href, Title string
}

DefLinkNode refresent single reference to link-definition

func (*DefLinkNode) NodeName

func (n *DefLinkNode) NodeName() (s string)

func (*DefLinkNode) Render

func (n *DefLinkNode) Render() string

Deflink have no representation(Transparent node)

type EmphasisNode

type EmphasisNode struct {
	NodeType
	Pos
	Style itemType
	Nodes []Node
}

EmphasisNode holds plain-text wrapped with style. (strong, em, del, code)

func (*EmphasisNode) NodeName

func (n *EmphasisNode) NodeName() (s string)

func (*EmphasisNode) Render

func (n *EmphasisNode) Render() string

Return the html representation of emphasis text.

func (*EmphasisNode) Tag

func (n *EmphasisNode) Tag() (s string)

Tag return the tagName based on the Style field.

type HTMLNode

type HTMLNode struct {
	NodeType
	Pos
	Src string
}

HTMLNode holds the raw html source.

func (*HTMLNode) NodeName

func (n *HTMLNode) NodeName() (s string)

func (*HTMLNode) Render

func (n *HTMLNode) Render() string

Render returns the src of the HTMLNode

type HeadingNode

type HeadingNode struct {
	NodeType
	Pos
	Level int
	Text  string
	Nodes []Node
}

HeadingNode holds heaing element with specific level(1-6).

func (*HeadingNode) NodeName

func (n *HeadingNode) NodeName() (s string)

func (*HeadingNode) Render

func (n *HeadingNode) Render() (s string)

Render returns the html representation based on heading level.

type HrNode

type HrNode struct {
	NodeType
	Pos
}

HrNode represents horizontal rule

func (*HrNode) NodeName

func (n *HrNode) NodeName() (s string)

func (*HrNode) Render

func (n *HrNode) Render() string

Render returns the html representation of hr.

type ImageNode

type ImageNode struct {
	NodeType
	Pos
	Title, Src, Alt string
}

ImageNode represents an image element with optional alt and title attributes.

func (*ImageNode) NodeName

func (n *ImageNode) NodeName() (s string)

func (*ImageNode) Render

func (n *ImageNode) Render() string

Render returns the html representation on image node

type Lexer

type Lexer interface {
	// contains filtered or unexported methods
}

Lexer interface, used to composed it inside the parser

type LinkNode

type LinkNode struct {
	NodeType
	Pos
	Title, Href string
	Nodes       []Node
}

Link holds a tag with optional title

func (*LinkNode) NodeName

func (n *LinkNode) NodeName() (s string)

func (*LinkNode) Render

func (n *LinkNode) Render() (s string)

Return the html representation of link node

type ListItemNode

type ListItemNode struct {
	NodeType
	Pos
	Nodes []Node
}

ListItem represents single item in ListNode that may contains nested nodes.

func (*ListItemNode) Children

func (n *ListItemNode) Children() []Node

func (*ListItemNode) NodeName

func (n *ListItemNode) NodeName() string

func (*ListItemNode) Render

func (l *ListItemNode) Render() (s string)

Render returns the html representation of list-item

type ListNode

type ListNode struct {
	NodeType
	Pos
	Ordered bool
	Items   []*ListItemNode
}

ListNode holds list items nodes in ordered or unordered states.

func (*ListNode) Children

func (n *ListNode) Children() []Node

func (*ListNode) NodeName

func (n *ListNode) NodeName() string

func (*ListNode) Render

func (n *ListNode) Render() (s string)

Render returns the html representation of orderd(ol) or unordered(ul) list.

type Mark

type Mark struct {
	Input string
	// contains filtered or unexported fields
}

Mark

func New

func New(input string, opts *Options) *Mark

New return a new Mark

func (*Mark) AddRenderFn

func (m *Mark) AddRenderFn(typ NodeType, fn RenderFn)

AddRenderFn let you pass NodeType, and RenderFn function and override the default Node rendering

func (Mark) Dump

func (p Mark) Dump() string

func (Mark) Parse

func (p Mark) Parse()

parse convert the raw text to Nodeparse.

func (*Mark) Render

func (m *Mark) Render() string

parse and render input

type Node

type Node interface {
	Type() NodeType
	Render() string
	NodeName() string
	Dump(int) string
	Children() []Node
}

A Node is an element in the parse tree.

func CollectNode

func CollectNode(node Node, doCollect func(node Node) bool) (nodes []Node)

type NodeType

type NodeType int

NodeType identifies the type of a parse tree node.

const (
	NodeText       NodeType = iota // A plain text
	NodeParagraph                  // A Paragraph
	NodeEmphasis                   // An emphasis(strong, em, ...)
	NodeHeading                    // A heading (h1, h2, ...)
	NodeBr                         // A link break
	NodeHr                         // A horizontal rule
	NodeImage                      // An image
	NodeRefImage                   // A image reference
	NodeList                       // A list of ListItems
	NodeListItem                   // A list item node
	NodeLink                       // A link(href)
	NodeRefLink                    // A link reference
	NodeDefLink                    // A link definition
	NodeTable                      // A table of NodeRows
	NodeRow                        // A row of NodeCells
	NodeCell                       // A table-cell(td)
	NodeCode                       // A code block(wrapped with pre)
	NodeBlockQuote                 // A blockquote
	NodeHTML                       // An inline HTML
	NodeCheckbox                   // A checkbox
)

func (NodeType) Children

func (t NodeType) Children() []Node
func (t NodeType) CollectLinks() []LinkNode

func (NodeType) Dump

func (t NodeType) Dump(level int) string

func (*NodeType) NodeName

func (t *NodeType) NodeName() string

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes.

type Options

type Options struct {
	Gfm         bool
	Tables      bool
	Smartypants bool
	Fractions   bool
}

Mark options used to configure your Mark object set `Smartypants` and `Fractions` to true to enable smartypants and smartfractions rendering.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions return an options struct with default configuration it's means that only Gfm, and Tables set to true.

type ParagraphNode

type ParagraphNode struct {
	NodeType
	Pos
	Nodes []Node
	Name  string "ParagraphNode"
}

ParagraphNode hold simple paragraph node contains text that may be emphasis.

func (*ParagraphNode) Children

func (n *ParagraphNode) Children() []Node

func (*ParagraphNode) NodeName

func (n *ParagraphNode) NodeName() (s string)

func (*ParagraphNode) Render

func (n *ParagraphNode) Render() (s string)

Render returns the html representation of ParagraphNode

type Pos

type Pos int

type position

type RefNode

type RefNode struct {
	NodeType
	Pos

	Text, Ref, Raw string
	Nodes          []Node
	// contains filtered or unexported fields
}

RefLink holds link with refrence to link definition

func (*RefNode) NodeName

func (n *RefNode) NodeName() (s string)

func (*RefNode) Render

func (n *RefNode) Render() string

rendering based type

type RenderFn

type RenderFn func(Node) string

Render function, used for overriding default rendering.

type RowNode

type RowNode struct {
	NodeType
	Pos
	Cells []*CellNode
}

RowNode represnt tr that holds list of cell-nodes

func (*RowNode) Children

func (n *RowNode) Children() []Node

func (*RowNode) NodeName

func (n *RowNode) NodeName() (s string)

func (*RowNode) Render

func (r *RowNode) Render() string

Render returns the html representation of table-row

type TableNode

type TableNode struct {
	NodeType
	Pos
	Rows []*RowNode
}

TableNode represents table element contains head and body

func (*TableNode) Children

func (n *TableNode) Children() []Node

func (*TableNode) NodeName

func (n *TableNode) NodeName() (s string)

func (*TableNode) Render

func (n *TableNode) Render() string

Render returns the html representation of a table

type TextNode

type TextNode struct {
	NodeType
	Pos
	Text string
}

TextNode holds plain text.

func (*TextNode) NodeName

func (n *TextNode) NodeName() (s string)

func (*TextNode) Render

func (n *TextNode) Render() string

Render returns the string representation of TexNode

Directories

Path Synopsis
cmd
mark
mark command line tool.
mark command line tool.

Jump to

Keyboard shortcuts

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