Categorygithub.com/a8m/mark
modulepackage
0.1.0
Repository: https://github.com/a8m/mark.git
Documentation: pkg.go.dev

# README

Mark Test coverage Build status

A markdown processor written in Go. built for fun.

This project inspired from Rob Pike - Lexical Scanning talk and marked project.
Please note that this is a WIP project and 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 mark-cli

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

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

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>
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

  • Backslash escaping
    • should ignore inside code spans
  • Expand documentation
  • Configuration options
    • gfm, table
    • heading(auto hashing)

License

MIT

# Functions

DefaultOptions return an options struct with default configuration it's means that only Gfm, and Tables set to true.
New return a new Mark.
Staic render function.

# Constants

Alignment.
Cell types.
Cell types.
Alignment.
A blockquote.
A link break.
A table-cell(td).
A code block(wrapped with pre).
A link definition.
An emphasis(strong, em, ...).
A heading (h1, h2, ...).
A horizontal rule.
An inline HTML.
An image.
A link(href).
A list of ListItems.
A list item node.
A Paragraph.
A image reference.
A link reference.
A row of NodeCells.
A table of NodeRows.
A plain text.
Alignment.
Alignment.

# Structs

BlockQuote represent.
BrNode represent a link-break element.
CellNode represent table-data/cell that holds simple text(may be emphasis) Note: the text in <th> elements are bold and centered by default.
Code holds CodeBlock node with specific lang field.
DefLinkNode refresent single reference to link-definition.
EmphasisNode holds plain-text wrapped with style.
Heading holds heaing element with specific level(1-6).
HrNode represent horizontal rule.
HTMLNode holds the raw html source.
ImageNode represent an image element with optional alt and title attributes.
Link holds a tag with optional title.
ListItem represent single item in ListNode that may contains nested nodes.
ListNode holds list items nodes in ordered or unordered states.
Mark.
Mark options.
ParagraphNode hold simple paragraph node contains text that may be emphasis.
RefLink holds link with refrence to link definition.
RowNode represnt tr that holds list of cell-nodes.
TableNode represent table element contains head and body.
TextNode holds plain text.

# Interfaces

Lexer interface, used to composed it inside the parser.
A Node is an element in the parse tree.

# Type aliases

AlignType identifies the aligment-type of specfic cell.
NodeType identifies the type of a parse tree node.
type position.
Render function, used for overriding default rendering.