You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
9.6 KiB

  1. Blackfriday [![Build Status](https://travis-ci.org/russross/blackfriday.svg?branch=master)](https://travis-ci.org/russross/blackfriday) [![GoDoc](https://godoc.org/github.com/russross/blackfriday?status.svg)](https://godoc.org/github.com/russross/blackfriday)
  2. ===========
  3. Blackfriday is a [Markdown][1] processor implemented in [Go][2]. It
  4. is paranoid about its input (so you can safely feed it user-supplied
  5. data), it is fast, it supports common extensions (tables, smart
  6. punctuation substitutions, etc.), and it is safe for all utf-8
  7. (unicode) input.
  8. HTML output is currently supported, along with Smartypants
  9. extensions. An experimental LaTeX output engine is also included.
  10. It started as a translation from C of [Sundown][3].
  11. Installation
  12. ------------
  13. Blackfriday is compatible with Go 1. If you are using an older
  14. release of Go, consider using v1.1 of blackfriday, which was based
  15. on the last stable release of Go prior to Go 1. You can find it as a
  16. tagged commit on github.
  17. With Go 1 and git installed:
  18. go get github.com/russross/blackfriday
  19. will download, compile, and install the package into your `$GOPATH`
  20. directory hierarchy. Alternatively, you can achieve the same if you
  21. import it into a project:
  22. import "github.com/russross/blackfriday"
  23. and `go get` without parameters.
  24. Usage
  25. -----
  26. For basic usage, it is as simple as getting your input into a byte
  27. slice and calling:
  28. output := blackfriday.MarkdownBasic(input)
  29. This renders it with no extensions enabled. To get a more useful
  30. feature set, use this instead:
  31. output := blackfriday.MarkdownCommon(input)
  32. ### Sanitize untrusted content
  33. Blackfriday itself does nothing to protect against malicious content. If you are
  34. dealing with user-supplied markdown, we recommend running blackfriday's output
  35. through HTML sanitizer such as
  36. [Bluemonday](https://github.com/microcosm-cc/bluemonday).
  37. Here's an example of simple usage of blackfriday together with bluemonday:
  38. ``` go
  39. import (
  40. "github.com/microcosm-cc/bluemonday"
  41. "github.com/russross/blackfriday"
  42. )
  43. // ...
  44. unsafe := blackfriday.MarkdownCommon(input)
  45. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  46. ```
  47. ### Custom options
  48. If you want to customize the set of options, first get a renderer
  49. (currently either the HTML or LaTeX output engines), then use it to
  50. call the more general `Markdown` function. For examples, see the
  51. implementations of `MarkdownBasic` and `MarkdownCommon` in
  52. `markdown.go`.
  53. You can also check out `blackfriday-tool` for a more complete example
  54. of how to use it. Download and install it using:
  55. go get github.com/russross/blackfriday-tool
  56. This is a simple command-line tool that allows you to process a
  57. markdown file using a standalone program. You can also browse the
  58. source directly on github if you are just looking for some example
  59. code:
  60. * <http://github.com/russross/blackfriday-tool>
  61. Note that if you have not already done so, installing
  62. `blackfriday-tool` will be sufficient to download and install
  63. blackfriday in addition to the tool itself. The tool binary will be
  64. installed in `$GOPATH/bin`. This is a statically-linked binary that
  65. can be copied to wherever you need it without worrying about
  66. dependencies and library versions.
  67. Features
  68. --------
  69. All features of Sundown are supported, including:
  70. * **Compatibility**. The Markdown v1.0.3 test suite passes with
  71. the `--tidy` option. Without `--tidy`, the differences are
  72. mostly in whitespace and entity escaping, where blackfriday is
  73. more consistent and cleaner.
  74. * **Common extensions**, including table support, fenced code
  75. blocks, autolinks, strikethroughs, non-strict emphasis, etc.
  76. * **Safety**. Blackfriday is paranoid when parsing, making it safe
  77. to feed untrusted user input without fear of bad things
  78. happening. The test suite stress tests this and there are no
  79. known inputs that make it crash. If you find one, please let me
  80. know and send me the input that does it.
  81. NOTE: "safety" in this context means *runtime safety only*. In order to
  82. protect yourself against JavaScript injection in untrusted content, see
  83. [this example](https://github.com/russross/blackfriday#sanitize-untrusted-content).
  84. * **Fast processing**. It is fast enough to render on-demand in
  85. most web applications without having to cache the output.
  86. * **Thread safety**. You can run multiple parsers in different
  87. goroutines without ill effect. There is no dependence on global
  88. shared state.
  89. * **Minimal dependencies**. Blackfriday only depends on standard
  90. library packages in Go. The source code is pretty
  91. self-contained, so it is easy to add to any project, including
  92. Google App Engine projects.
  93. * **Standards compliant**. Output successfully validates using the
  94. W3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.
  95. Extensions
  96. ----------
  97. In addition to the standard markdown syntax, this package
  98. implements the following extensions:
  99. * **Intra-word emphasis supression**. The `_` character is
  100. commonly used inside words when discussing code, so having
  101. markdown interpret it as an emphasis command is usually the
  102. wrong thing. Blackfriday lets you treat all emphasis markers as
  103. normal characters when they occur inside a word.
  104. * **Tables**. Tables can be created by drawing them in the input
  105. using a simple syntax:
  106. ```
  107. Name | Age
  108. --------|------
  109. Bob | 27
  110. Alice | 23
  111. ```
  112. * **Fenced code blocks**. In addition to the normal 4-space
  113. indentation to mark code blocks, you can explicitly mark them
  114. and supply a language (to make syntax highlighting simple). Just
  115. mark it like this:
  116. ``` go
  117. func getTrue() bool {
  118. return true
  119. }
  120. ```
  121. You can use 3 or more backticks to mark the beginning of the
  122. block, and the same number to mark the end of the block.
  123. To preserve classes of fenced code blocks while using the bluemonday
  124. HTML sanitizer, use the following policy:
  125. ``` go
  126. p := bluemonday.UGCPolicy()
  127. p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code")
  128. html := p.SanitizeBytes(unsafe)
  129. ```
  130. * **Definition lists**. A simple definition list is made of a single-line
  131. term followed by a colon and the definition for that term.
  132. Cat
  133. : Fluffy animal everyone likes
  134. Internet
  135. : Vector of transmission for pictures of cats
  136. Terms must be separated from the previous definition by a blank line.
  137. * **Footnotes**. A marker in the text that will become a superscript number;
  138. a footnote definition that will be placed in a list of footnotes at the
  139. end of the document. A footnote looks like this:
  140. This is a footnote.[^1]
  141. [^1]: the footnote text.
  142. * **Autolinking**. Blackfriday can find URLs that have not been
  143. explicitly marked as links and turn them into links.
  144. * **Strikethrough**. Use two tildes (`~~`) to mark text that
  145. should be crossed out.
  146. * **Hard line breaks**. With this extension enabled (it is off by
  147. default in the `MarkdownBasic` and `MarkdownCommon` convenience
  148. functions), newlines in the input translate into line breaks in
  149. the output.
  150. * **Smart quotes**. Smartypants-style punctuation substitution is
  151. supported, turning normal double- and single-quote marks into
  152. curly quotes, etc.
  153. * **LaTeX-style dash parsing** is an additional option, where `--`
  154. is translated into `&ndash;`, and `---` is translated into
  155. `&mdash;`. This differs from most smartypants processors, which
  156. turn a single hyphen into an ndash and a double hyphen into an
  157. mdash.
  158. * **Smart fractions**, where anything that looks like a fraction
  159. is translated into suitable HTML (instead of just a few special
  160. cases like most smartypant processors). For example, `4/5`
  161. becomes `<sup>4</sup>&frasl;<sub>5</sub>`, which renders as
  162. <sup>4</sup>&frasl;<sub>5</sub>.
  163. Other renderers
  164. ---------------
  165. Blackfriday is structured to allow alternative rendering engines. Here
  166. are a few of note:
  167. * [github_flavored_markdown](https://godoc.org/github.com/shurcooL/github_flavored_markdown):
  168. provides a GitHub Flavored Markdown renderer with fenced code block
  169. highlighting, clickable header anchor links.
  170. It's not customizable, and its goal is to produce HTML output
  171. equivalent to the [GitHub Markdown API endpoint](https://developer.github.com/v3/markdown/#render-a-markdown-document-in-raw-mode),
  172. except the rendering is performed locally.
  173. * [markdownfmt](https://github.com/shurcooL/markdownfmt): like gofmt,
  174. but for markdown.
  175. * LaTeX output: renders output as LaTeX. This is currently part of the
  176. main Blackfriday repository, but may be split into its own project
  177. in the future. If you are interested in owning and maintaining the
  178. LaTeX output component, please be in touch.
  179. It renders some basic documents, but is only experimental at this
  180. point. In particular, it does not do any inline escaping, so input
  181. that happens to look like LaTeX code will be passed through without
  182. modification.
  183. * [Md2Vim](https://github.com/FooSoft/md2vim): transforms markdown files into vimdoc format.
  184. Todo
  185. ----
  186. * More unit testing
  187. * Improve unicode support. It does not understand all unicode
  188. rules (about what constitutes a letter, a punctuation symbol,
  189. etc.), so it may fail to detect word boundaries correctly in
  190. some instances. It is safe on all utf-8 input.
  191. License
  192. -------
  193. [Blackfriday is distributed under the Simplified BSD License](LICENSE.txt)
  194. [1]: http://daringfireball.net/projects/markdown/ "Markdown"
  195. [2]: http://golang.org/ "Go Language"
  196. [3]: https://github.com/vmg/sundown "Sundown"