website: use custom markdown options

This is all so that we can enable auto creating header IDs based on the
text of the header.  For example "# Foo" is converted to:

  <h1 id="foo">Foo</h1>

I'm not really sure why this isn't a default option, but whatever.  It's
also unfortunate that we have to copy all of the flags and extensions
from MarkdownCommon, but at least this will make it easier to add more
options in the future, I gues.

Change-Id: I65800c75560ad3b9a3fd0389b722c07ba11ee098
This commit is contained in:
Will Norris 2016-04-26 10:33:18 -07:00
parent e3777187be
commit 066db0952c
1 changed files with 26 additions and 1 deletions

View File

@ -342,6 +342,31 @@ func findAndServeFile(rw http.ResponseWriter, req *http.Request, root string) {
serveFile(rw, req, relPath, absPath)
}
// configure blackfriday options. These are the same options that
// blackfriday.MarkdownCommon uses with minor additions.
const (
markdownHTMLFlags = 0 |
blackfriday.HTML_USE_XHTML |
blackfriday.HTML_USE_SMARTYPANTS |
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
blackfriday.HTML_SMARTYPANTS_DASHES |
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
markdownExtensions = 0 |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS |
blackfriday.EXTENSION_AUTO_HEADER_IDS
)
var markdownRenderer = blackfriday.HtmlRenderer(markdownHTMLFlags, "", "")
// serveFile serves a file from disk, converting any markdown to HTML.
func serveFile(rw http.ResponseWriter, req *http.Request, relPath, absPath string) {
data, err := ioutil.ReadFile(absPath)
@ -350,7 +375,7 @@ func serveFile(rw http.ResponseWriter, req *http.Request, relPath, absPath strin
return
}
data = blackfriday.MarkdownCommon(data)
data = blackfriday.MarkdownOptions(data, markdownRenderer, blackfriday.Options{Extensions: markdownExtensions})
title := ""
if m := h1TitlePattern.FindSubmatch(data); len(m) > 1 {