markdown/README.md

214 lines
7.4 KiB
Markdown
Raw Normal View History

2018-01-26 22:59:37 +00:00
Markdown Parser and HTML Renderer for Go [![GoDoc](https://godoc.org/github.com/gomarkdown/markdown?status.svg)](https://godoc.org/github.com/gomarkdown/markdown) [![Build Status](https://travis-ci.org/gomarkdown/markdown.svg?branch=master)](https://travis-ci.org/gomarkdown/markdown)
2011-06-28 02:11:32 +00:00
===========
2011-05-29 03:33:16 +00:00
2018-01-28 03:45:55 +00:00
Package `github.com/gomarkdown/markdown` is a Go library for parsing [Markdown](https://daringfireball.net/projects/markdown/) documents and rendering them to HTML.
2018-01-26 22:57:25 +00:00
2018-01-26 09:12:35 +00:00
It's fast and supports common extensions.
2011-05-29 03:33:16 +00:00
2018-01-28 03:45:55 +00:00
## Installation
2018-01-26 12:08:27 +00:00
2018-01-26 22:55:05 +00:00
go get -u github.com/gomarkdown/markdown
2017-01-26 11:30:06 +00:00
2018-01-26 22:55:05 +00:00
API Docs: https://godoc.org/github.com/gomarkdown/markdown
2018-01-26 12:08:27 +00:00
2018-01-28 03:45:55 +00:00
## Usage
2014-05-08 22:42:05 +00:00
2018-01-26 22:57:25 +00:00
To convert markdown text to HTML using reasonable defaults:
2017-07-30 16:50:50 +00:00
```go
2018-01-26 22:55:05 +00:00
md := []byte("## markdown document")
output := markdown.ToHTML(md, nil, nil)
2017-07-29 08:59:03 +00:00
```
2018-01-26 22:57:25 +00:00
To customize both parser and HTML renderer:
2017-07-30 16:50:50 +00:00
```go
2018-01-28 03:45:55 +00:00
import (
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
extensions := html.CommonExtensions | html.AutoHeadingIDs
parser := parser.NewWithExensions(extensions)
2018-01-28 04:44:39 +00:00
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
md := []byte("markdown text")
2018-01-28 03:45:55 +00:00
html := markdown.ToHTML(md, parser, renderer)
2017-07-29 08:59:03 +00:00
```
2018-01-28 03:45:55 +00:00
## Sanitize untrusted content
2018-01-26 09:12:35 +00:00
We don't protect against malicious content. When dealing with user-provided
2018-01-26 22:55:05 +00:00
markdown, run renderer HTML through HTML sanitizer such as [Bluemonday](https://github.com/microcosm-cc/bluemonday).
2018-01-26 22:55:05 +00:00
Here's an example of simple usage with Bluemonday:
2017-07-30 16:50:50 +00:00
```go
import (
"github.com/microcosm-cc/bluemonday"
2018-01-26 09:12:35 +00:00
"github.com/gomarkdown/markdown"
)
// ...
2018-01-28 03:45:55 +00:00
maybeUnsafeHTML := markdown.ToHTML(md, nil, nil)
2018-01-26 09:12:35 +00:00
html := bluemonday.UGCPolicy().SanitizeBytes(maybeUnsafeHTML)
```
2018-01-28 03:45:55 +00:00
## Customizing parser and renderer
2018-01-26 09:12:35 +00:00
Ways to customize parser:
2018-01-28 03:45:55 +00:00
* use custom extensions by creating parser with `parser.NewWithExtensions(extensions)`
* over-ride `parser.Parser.ReferenceOverride` function
2011-05-29 03:33:16 +00:00
2018-01-26 22:55:05 +00:00
You can also check out [cmd/mdhtml](https://github.com/gomarkdown/markdown/tree/master/cmd/mdtohtml) for more complete example of how to use it.
You can install it with:
2012-03-08 05:12:46 +00:00
2018-01-26 22:55:05 +00:00
go get -u github.com/gomarkdown/markdown/cmd/mdtohtml
2011-05-29 03:33:16 +00:00
2018-01-26 09:12:35 +00:00
This is a simple command-line tool to convert markdown files to HTML.
2011-05-29 03:33:16 +00:00
2018-01-28 03:45:55 +00:00
## Features
2011-05-29 03:43:17 +00:00
2011-07-06 16:01:13 +00:00
* **Compatibility**. The Markdown v1.0.3 test suite passes with
the `--tidy` option. Without `--tidy`, the differences are
2018-01-26 22:55:05 +00:00
mostly in whitespace and entity escaping, where this package is
2011-07-06 16:01:13 +00:00
more consistent and cleaner.
2011-05-29 03:43:17 +00:00
2011-07-06 16:01:13 +00:00
* **Common extensions**, including table support, fenced code
blocks, autolinks, strikethroughs, non-strict emphasis, etc.
2011-05-29 03:43:17 +00:00
2018-01-26 09:12:35 +00:00
* **Safety**. Markdown is paranoid when parsing, making it safe
2011-07-06 16:01:13 +00:00
to feed untrusted user input without fear of bad things
happening. The test suite stress tests this and there are no
known inputs that make it crash. If you find one, please let me
know and send me the input that does it.
2011-05-29 03:43:17 +00:00
NOTE: "safety" in this context means *runtime safety only*. In order to
protect yourself against JavaScript injection in untrusted content, see
2018-01-26 09:12:35 +00:00
[this example](https://github.com/gomarkdown/markdown#sanitize-untrusted-content).
2018-01-26 09:12:35 +00:00
* **Fast**. It is fast enough to render on-demand in
2011-07-06 16:01:13 +00:00
most web applications without having to cache the output.
2011-05-29 03:43:17 +00:00
2013-09-01 17:59:06 +00:00
* **Thread safety**. You can run multiple parsers in different
2011-06-29 17:21:46 +00:00
goroutines without ill effect. There is no dependence on global
shared state.
2018-01-26 09:12:35 +00:00
* **Minimal dependencies**. Only depends on standard library packages in Go.
2011-05-29 03:43:17 +00:00
2011-07-06 16:01:13 +00:00
* **Standards compliant**. Output successfully validates using the
W3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.
2011-05-29 03:43:17 +00:00
2018-01-28 03:45:55 +00:00
## Extensions
2011-05-29 03:33:16 +00:00
2011-07-06 16:01:13 +00:00
In addition to the standard markdown syntax, this package
implements the following extensions:
* **Intra-word emphasis supression**. The `_` character is
commonly used inside words when discussing code, so having
markdown interpret it as an emphasis command is usually the
2018-01-26 22:55:05 +00:00
wrong thing. We let you treat all emphasis markers as
2011-07-06 16:01:13 +00:00
normal characters when they occur inside a word.
2011-07-06 16:04:30 +00:00
* **Tables**. Tables can be created by drawing them in the input
using a simple syntax:
2011-07-06 16:01:13 +00:00
2013-05-05 03:35:09 +00:00
```
Name | Age
--------|------
Bob | 27
Alice | 23
```
2011-07-06 16:01:13 +00:00
* **Fenced code blocks**. In addition to the normal 4-space
indentation to mark code blocks, you can explicitly mark them
and supply a language (to make syntax highlighting simple). Just
mark it like this:
2017-07-30 16:50:50 +00:00
```go
2011-07-06 16:01:13 +00:00
func getTrue() bool {
return true
}
```
You can use 3 or more backticks to mark the beginning of the
block, and the same number to mark the end of the block.
* **Definition lists**. A simple definition list is made of a single-line
term followed by a colon and the definition for that term.
Cat
: Fluffy animal everyone likes
2018-01-25 21:06:17 +00:00
Internet
: Vector of transmission for pictures of cats
Terms must be separated from the previous definition by a blank line.
* **Footnotes**. A marker in the text that will become a superscript number;
a footnote definition that will be placed in a list of footnotes at the
end of the document. A footnote looks like this:
This is a footnote.[^1]
2018-01-25 21:06:17 +00:00
[^1]: the footnote text.
2018-01-26 22:55:05 +00:00
* **Autolinking**. We can find URLs that have not been
2011-07-06 16:01:13 +00:00
explicitly marked as links and turn them into links.
* **Strikethrough**. Use two tildes (`~~`) to mark text that
should be crossed out.
* **Hard line breaks**. With this extension enabled newlines in the input
translate into line breaks in the output. This extension is off by default.
2011-07-06 16:01:13 +00:00
* **Smart quotes**. Smartypants-style punctuation substitution is
supported, turning normal double- and single-quote marks into
curly quotes, etc.
* **LaTeX-style dash parsing** is an additional option, where `--`
is translated into `–`, and `---` is translated into
`—`. This differs from most smartypants processors, which
turn a single hyphen into an ndash and a double hyphen into an
mdash.
* **Smart fractions**, where anything that looks like a fraction
is translated into suitable HTML (instead of just a few special
cases like most smartypant processors). For example, `4/5`
becomes `<sup>4</sup>&frasl;<sub>5</sub>`, which renders as
2011-06-24 22:42:17 +00:00
<sup>4</sup>&frasl;<sub>5</sub>.
2011-05-29 03:33:16 +00:00
2018-01-28 03:45:55 +00:00
## Todo
2011-05-29 03:33:16 +00:00
2018-01-26 09:12:35 +00:00
* port https://github.com/russross/blackfriday/issues/348
* port [LaTeX output](https://github.com/Ambrevar/Blackfriday-LaTeX):
renders output as LaTeX.
* port https://github.com/shurcooL/github_flavored_markdown to markdown
* port [markdownfmt](https://github.com/shurcooL/markdownfmt): like gofmt,
but for markdown.
* More unit testing
* Improve unicode support. It does not understand all unicode
rules (about what constitutes a letter, a punctuation symbol,
etc.), so it may fail to detect word boundaries correctly in
some instances. It is safe on all utf-8 input.
2011-05-29 03:33:16 +00:00
2018-01-28 03:45:55 +00:00
## History
2018-01-26 09:12:35 +00:00
2018-01-28 03:45:55 +00:00
markdown is a fork of v2 of https://github.com/russross/blackfriday that is:
2018-01-26 22:55:05 +00:00
* actively maintained (sadly in Feb 2018 blackfriday was inactive for 5 months with many bugs and pull requests accumulated)
2018-01-28 03:45:55 +00:00
* refactored API (split into ast/parser/html sub-packages)
2011-05-29 03:33:16 +00:00
2018-01-26 22:55:05 +00:00
Blackfriday itself was based on C implementation [sundown](https://github.com/vmg/sundown) which in turn was based on [libsoldout](http://fossil.instinctive.eu/libsoldout/home).
2018-01-28 03:45:55 +00:00
## License
2011-06-28 02:11:32 +00:00
2018-01-28 03:45:55 +00:00
[Simplified BSD License](LICENSE.txt)