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.

363 lines
13 KiB

  1. Blackfriday
  2. [![Build Status][BuildSVG]][BuildURL]
  3. [![Godoc][GodocV2SVG]][GodocV2URL]
  4. ===========
  5. Blackfriday is a [Markdown][1] processor implemented in [Go][2]. It
  6. is paranoid about its input (so you can safely feed it user-supplied
  7. data), it is fast, it supports common extensions (tables, smart
  8. punctuation substitutions, etc.), and it is safe for all utf-8
  9. (unicode) input.
  10. HTML output is currently supported, along with Smartypants
  11. extensions.
  12. It started as a translation from C of [Sundown][3].
  13. Installation
  14. ------------
  15. Blackfriday is compatible with any modern Go release. With Go and git installed:
  16. go get -u gopkg.in/russross/blackfriday.v2
  17. will download, compile, and install the package into your `$GOPATH` directory
  18. hierarchy.
  19. Versions
  20. --------
  21. Currently maintained and recommended version of Blackfriday is `v2`. It's being
  22. developed on its own branch: https://github.com/russross/blackfriday/tree/v2 and the
  23. documentation is available at
  24. https://godoc.org/gopkg.in/russross/blackfriday.v2.
  25. It is `go get`-able via [gopkg.in][6] at `gopkg.in/russross/blackfriday.v2`,
  26. but we highly recommend using package management tool like [dep][7] or
  27. [Glide][8] and make use of semantic versioning. With package management you
  28. should import `github.com/russross/blackfriday` and specify that you're using
  29. version 2.0.0.
  30. Version 2 offers a number of improvements over v1:
  31. * Cleaned up API
  32. * A separate call to [`Parse`][4], which produces an abstract syntax tree for
  33. the document
  34. * Latest bug fixes
  35. * Flexibility to easily add your own rendering extensions
  36. Potential drawbacks:
  37. * Our benchmarks show v2 to be slightly slower than v1. Currently in the
  38. ballpark of around 15%.
  39. * API breakage. If you can't afford modifying your code to adhere to the new API
  40. and don't care too much about the new features, v2 is probably not for you.
  41. * Several bug fixes are trailing behind and still need to be forward-ported to
  42. v2. See issue [#348](https://github.com/russross/blackfriday/issues/348) for
  43. tracking.
  44. If you are still interested in the legacy `v1`, you can import it from
  45. `github.com/russross/blackfriday`. Documentation for the legacy v1 can be found
  46. here: https://godoc.org/github.com/russross/blackfriday
  47. ### Known issue with `dep`
  48. There is a known problem with using Blackfriday v1 _transitively_ and `dep`.
  49. Currently `dep` prioritizes semver versions over anything else, and picks the
  50. latest one, plus it does not apply a `[[constraint]]` specifier to transitively
  51. pulled in packages. So if you're using something that uses Blackfriday v1, but
  52. that something does not use `dep` yet, you will get Blackfriday v2 pulled in and
  53. your first dependency will fail to build.
  54. There are couple of fixes for it, documented here:
  55. https://github.com/golang/dep/blob/master/docs/FAQ.md#how-do-i-constrain-a-transitive-dependencys-version
  56. Meanwhile, `dep` team is working on a more general solution to the constraints
  57. on transitive dependencies problem: https://github.com/golang/dep/issues/1124.
  58. Usage
  59. -----
  60. ### v1
  61. For basic usage, it is as simple as getting your input into a byte
  62. slice and calling:
  63. output := blackfriday.MarkdownBasic(input)
  64. This renders it with no extensions enabled. To get a more useful
  65. feature set, use this instead:
  66. output := blackfriday.MarkdownCommon(input)
  67. ### v2
  68. For the most sensible markdown processing, it is as simple as getting your input
  69. into a byte slice and calling:
  70. ```go
  71. output := blackfriday.Run(input)
  72. ```
  73. Your input will be parsed and the output rendered with a set of most popular
  74. extensions enabled. If you want the most basic feature set, corresponding with
  75. the bare Markdown specification, use:
  76. ```go
  77. output := blackfriday.Run(input, blackfriday.WithNoExtensions())
  78. ```
  79. ### Sanitize untrusted content
  80. Blackfriday itself does nothing to protect against malicious content. If you are
  81. dealing with user-supplied markdown, we recommend running Blackfriday's output
  82. through HTML sanitizer such as [Bluemonday][5].
  83. Here's an example of simple usage of Blackfriday together with Bluemonday:
  84. ```go
  85. import (
  86. "github.com/microcosm-cc/bluemonday"
  87. "gopkg.in/russross/blackfriday.v2"
  88. )
  89. // ...
  90. unsafe := blackfriday.Run(input)
  91. html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
  92. ```
  93. ### Custom options, v1
  94. If you want to customize the set of options, first get a renderer
  95. (currently only the HTML output engine), then use it to
  96. call the more general `Markdown` function. For examples, see the
  97. implementations of `MarkdownBasic` and `MarkdownCommon` in
  98. `markdown.go`.
  99. ### Custom options, v2
  100. If you want to customize the set of options, use `blackfriday.WithExtensions`,
  101. `blackfriday.WithRenderer` and `blackfriday.WithRefOverride`.
  102. ### `blackfriday-tool`
  103. You can also check out `blackfriday-tool` for a more complete example
  104. of how to use it. Download and install it using:
  105. go get github.com/russross/blackfriday-tool
  106. This is a simple command-line tool that allows you to process a
  107. markdown file using a standalone program. You can also browse the
  108. source directly on github if you are just looking for some example
  109. code:
  110. * <http://github.com/russross/blackfriday-tool>
  111. Note that if you have not already done so, installing
  112. `blackfriday-tool` will be sufficient to download and install
  113. blackfriday in addition to the tool itself. The tool binary will be
  114. installed in `$GOPATH/bin`. This is a statically-linked binary that
  115. can be copied to wherever you need it without worrying about
  116. dependencies and library versions.
  117. ### Sanitized anchor names
  118. Blackfriday includes an algorithm for creating sanitized anchor names
  119. corresponding to a given input text. This algorithm is used to create
  120. anchors for headings when `EXTENSION_AUTO_HEADER_IDS` is enabled. The
  121. algorithm has a specification, so that other packages can create
  122. compatible anchor names and links to those anchors.
  123. The specification is located at https://godoc.org/github.com/russross/blackfriday#hdr-Sanitized_Anchor_Names.
  124. [`SanitizedAnchorName`](https://godoc.org/github.com/russross/blackfriday#SanitizedAnchorName) exposes this functionality, and can be used to
  125. create compatible links to the anchor names generated by blackfriday.
  126. This algorithm is also implemented in a small standalone package at
  127. [`github.com/shurcooL/sanitized_anchor_name`](https://godoc.org/github.com/shurcooL/sanitized_anchor_name). It can be useful for clients
  128. that want a small package and don't need full functionality of blackfriday.
  129. Features
  130. --------
  131. All features of Sundown are supported, including:
  132. * **Compatibility**. The Markdown v1.0.3 test suite passes with
  133. the `--tidy` option. Without `--tidy`, the differences are
  134. mostly in whitespace and entity escaping, where blackfriday is
  135. more consistent and cleaner.
  136. * **Common extensions**, including table support, fenced code
  137. blocks, autolinks, strikethroughs, non-strict emphasis, etc.
  138. * **Safety**. Blackfriday is paranoid when parsing, making it safe
  139. to feed untrusted user input without fear of bad things
  140. happening. The test suite stress tests this and there are no
  141. known inputs that make it crash. If you find one, please let me
  142. know and send me the input that does it.
  143. NOTE: "safety" in this context means *runtime safety only*. In order to
  144. protect yourself against JavaScript injection in untrusted content, see
  145. [this example](https://github.com/russross/blackfriday#sanitize-untrusted-content).
  146. * **Fast processing**. It is fast enough to render on-demand in
  147. most web applications without having to cache the output.
  148. * **Thread safety**. You can run multiple parsers in different
  149. goroutines without ill effect. There is no dependence on global
  150. shared state.
  151. * **Minimal dependencies**. Blackfriday only depends on standard
  152. library packages in Go. The source code is pretty
  153. self-contained, so it is easy to add to any project, including
  154. Google App Engine projects.
  155. * **Standards compliant**. Output successfully validates using the
  156. W3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.
  157. Extensions
  158. ----------
  159. In addition to the standard markdown syntax, this package
  160. implements the following extensions:
  161. * **Intra-word emphasis supression**. The `_` character is
  162. commonly used inside words when discussing code, so having
  163. markdown interpret it as an emphasis command is usually the
  164. wrong thing. Blackfriday lets you treat all emphasis markers as
  165. normal characters when they occur inside a word.
  166. * **Tables**. Tables can be created by drawing them in the input
  167. using a simple syntax:
  168. ```
  169. Name | Age
  170. --------|------
  171. Bob | 27
  172. Alice | 23
  173. ```
  174. * **Fenced code blocks**. In addition to the normal 4-space
  175. indentation to mark code blocks, you can explicitly mark them
  176. and supply a language (to make syntax highlighting simple). Just
  177. mark it like this:
  178. ``` go
  179. func getTrue() bool {
  180. return true
  181. }
  182. ```
  183. You can use 3 or more backticks to mark the beginning of the
  184. block, and the same number to mark the end of the block.
  185. To preserve classes of fenced code blocks while using the bluemonday
  186. HTML sanitizer, use the following policy:
  187. ``` go
  188. p := bluemonday.UGCPolicy()
  189. p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code")
  190. html := p.SanitizeBytes(unsafe)
  191. ```
  192. * **Definition lists**. A simple definition list is made of a single-line
  193. term followed by a colon and the definition for that term.
  194. Cat
  195. : Fluffy animal everyone likes
  196. Internet
  197. : Vector of transmission for pictures of cats
  198. Terms must be separated from the previous definition by a blank line.
  199. * **Footnotes**. A marker in the text that will become a superscript number;
  200. a footnote definition that will be placed in a list of footnotes at the
  201. end of the document. A footnote looks like this:
  202. This is a footnote.[^1]
  203. [^1]: the footnote text.
  204. * **Autolinking**. Blackfriday can find URLs that have not been
  205. explicitly marked as links and turn them into links.
  206. * **Strikethrough**. Use two tildes (`~~`) to mark text that
  207. should be crossed out.
  208. * **Hard line breaks**. With this extension enabled (it is off by
  209. default in the `MarkdownBasic` and `MarkdownCommon` convenience
  210. functions), newlines in the input translate into line breaks in
  211. the output.
  212. * **Smart quotes**. Smartypants-style punctuation substitution is
  213. supported, turning normal double- and single-quote marks into
  214. curly quotes, etc.
  215. * **LaTeX-style dash parsing** is an additional option, where `--`
  216. is translated into `&ndash;`, and `---` is translated into
  217. `&mdash;`. This differs from most smartypants processors, which
  218. turn a single hyphen into an ndash and a double hyphen into an
  219. mdash.
  220. * **Smart fractions**, where anything that looks like a fraction
  221. is translated into suitable HTML (instead of just a few special
  222. cases like most smartypant processors). For example, `4/5`
  223. becomes `<sup>4</sup>&frasl;<sub>5</sub>`, which renders as
  224. <sup>4</sup>&frasl;<sub>5</sub>.
  225. Other renderers
  226. ---------------
  227. Blackfriday is structured to allow alternative rendering engines. Here
  228. are a few of note:
  229. * [github_flavored_markdown](https://godoc.org/github.com/shurcooL/github_flavored_markdown):
  230. provides a GitHub Flavored Markdown renderer with fenced code block
  231. highlighting, clickable heading anchor links.
  232. It's not customizable, and its goal is to produce HTML output
  233. equivalent to the [GitHub Markdown API endpoint](https://developer.github.com/v3/markdown/#render-a-markdown-document-in-raw-mode),
  234. except the rendering is performed locally.
  235. * [markdownfmt](https://github.com/shurcooL/markdownfmt): like gofmt,
  236. but for markdown.
  237. * [LaTeX output](https://bitbucket.org/ambrevar/blackfriday-latex):
  238. renders output as LaTeX.
  239. TODO
  240. ----
  241. * More unit testing
  242. * Improve Unicode support. It does not understand all Unicode
  243. rules (about what constitutes a letter, a punctuation symbol,
  244. etc.), so it may fail to detect word boundaries correctly in
  245. some instances. It is safe on all UTF-8 input.
  246. License
  247. -------
  248. [Blackfriday is distributed under the Simplified BSD License](LICENSE.txt)
  249. [1]: https://daringfireball.net/projects/markdown/ "Markdown"
  250. [2]: https://golang.org/ "Go Language"
  251. [3]: https://github.com/vmg/sundown "Sundown"
  252. [4]: https://godoc.org/gopkg.in/russross/blackfriday.v2#Parse "Parse func"
  253. [5]: https://github.com/microcosm-cc/bluemonday "Bluemonday"
  254. [6]: https://labix.org/gopkg.in "gopkg.in"
  255. [7]: https://github.com/golang/dep/ "dep"
  256. [8]: https://github.com/Masterminds/glide "Glide"
  257. [BuildSVG]: https://travis-ci.org/russross/blackfriday.svg?branch=master
  258. [BuildURL]: https://travis-ci.org/russross/blackfriday
  259. [GodocV2SVG]: https://godoc.org/gopkg.in/russross/blackfriday.v2?status.svg
  260. [GodocV2URL]: https://godoc.org/gopkg.in/russross/blackfriday.v2