Git Product home page Git Product logo

mark's Introduction

Archived. use https://github.com/russross/blackfriday instead

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

mark's People

Contributors

a8m avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mark's Issues

CommonMark support

Hey, how CommonMark is this?

Markdown parsers all seem to build their own unique flavor of Markdown each and every time, usually because they copy and paste around test suites from random other projects then miss updates and changes without noticing.

Sorry for the self link, but I wrote about the mess a bit here.

CommonMark would be awesome, especially if you work from their spec.

Package contains data races

Upon running the tests with a --race flag, you'll find the package is riddled with many race conditions. Due to this, the package cannot safely be utilized in any concurrent fashion.

I will only paste a snippet example here as the actual result errors totalled in over 3000 lines. A simple invocation of the tests with go test --race should be sufficient enough to investigate.

WARNING: DATA RACE
Read at 0x00c42031d818 by goroutine 161:
  github.com/a8m/mark.(*lexer).nextItem()
      /Users/henryhenderson/go/src/github.com/a8m/mark/lexer.go:279 +0xb5
  github.com/a8m/mark.(*parse).peek()
      /Users/henryhenderson/go/src/github.com/a8m/mark/parser.go:126 +0x154
  github.com/a8m/mark.(*parse).parse()
      /Users/henryhenderson/go/src/github.com/a8m/mark/parser.go:38 +0x50
  github.com/a8m/mark.(*parse).parseBlockQuote()
      /Users/henryhenderson/go/src/github.com/a8m/mark/parser.go:280 +0x194
  github.com/a8m/mark.(*parse).parse()
      /Users/henryhenderson/go/src/github.com/a8m/mark/parser.go:59 +0x422
  github.com/a8m/mark.(*Mark).Render()
      /Users/henryhenderson/go/src/github.com/a8m/mark/mark.go:45 +0x4d
  github.com/a8m/mark.Render()
      /Users/henryhenderson/go/src/github.com/a8m/mark/mark.go:59 +0x5d
  github.com/a8m/mark.TestCommonMark()
      /Users/henryhenderson/go/src/github.com/a8m/mark/mark_test.go:1666 +0x14d
  testing.tRunner()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0x16d

Previous write at 0x00c42031d818 by goroutine 447:
  github.com/a8m/mark.(*lexer).next()
      /Users/henryhenderson/go/src/github.com/a8m/mark/lexer.go:114 +0x138
  github.com/a8m/mark.(*lexer).peek()
      /Users/henryhenderson/go/src/github.com/a8m/mark/lexer.go:262 +0x3c
  github.com/a8m/mark.lexText()
      /Users/henryhenderson/go/src/github.com/a8m/mark/lexer.go:231 +0x78
  github.com/a8m/mark.(*lexer).run()
      /Users/henryhenderson/go/src/github.com/a8m/mark/lexer.go:101 +0xa1

Goroutine 161 (running) created at:
  testing.(*T).Run()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:824 +0x564
  testing.runTests.func1()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:1063 +0xa4
  testing.tRunner()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0x16d
  testing.runTests()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:1061 +0x4e1
  testing.(*M).Run()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:978 +0x2cd
  main.main()
      _testmain.go:56 +0x22a

Goroutine 447 (running) created at:
  github.com/a8m/mark.lex()
      /Users/henryhenderson/go/src/github.com/a8m/mark/lexer.go:84 +0x112
  github.com/a8m/mark.(*parse).parseBlockQuote()
      /Users/henryhenderson/go/src/github.com/a8m/mark/parser.go:279 +0xbe
  github.com/a8m/mark.(*parse).parse()
      /Users/henryhenderson/go/src/github.com/a8m/mark/parser.go:59 +0x422
  github.com/a8m/mark.(*Mark).Render()
      /Users/henryhenderson/go/src/github.com/a8m/mark/mark.go:45 +0x4d
  github.com/a8m/mark.Render()
      /Users/henryhenderson/go/src/github.com/a8m/mark/mark.go:59 +0x5d
  github.com/a8m/mark.TestCommonMark()
      /Users/henryhenderson/go/src/github.com/a8m/mark/mark_test.go:1666 +0x14d
  testing.tRunner()
      /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0x16d
==================

Set an <br> and <hr>

What ist the common way to set a line breake <br> and a <hr>.
I tried

**What's new?** /
---
    
* The release notes now use HTML to look friendlier
* A lot of small improvements on Courier2                                    
       
       
/There should be no visible changes for Courier 1 users.

in this code.

package main

import (
	"fmt"
	"io/ioutil"

	"github.com/a8m/mark"
)

func main() {
	markdown_text, err := ioutil.ReadFile("testdata/test3.md")
	html := mark.Render(string(markdown_text))
	fmt.Print(html, err)
}

But it dont set an <br> after two spaces or an <hr> for ---.
I got this

</p>strong>Test This</strong>
</p> ---
<pre><code>
</code></pre>
<ul>
<li>Bla 1</li>
<li>Bla 2

Did it work?</li>
</ul><nil>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.