Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (76 sloc) 2.9 KB
// Package markdown renders Markdown documents to HTML with the "blackfriday"
// processor. You can specify which Markdown extensions and HTML features to
// use by directly passing the blackfriday flags to this plugin. Note that
// unlike other static site generators, Markdown processing does not
// automatically parse frontmatter; you will need to use the "frontmatter"
// plugin to extract any metadata which may be present in your source content.
package markdown
import (
"bytes"
"path"
"strings"
"github.com/FooSoft/goldsmith"
"github.com/FooSoft/goldsmith-components/filters/wildcard"
"github.com/russross/blackfriday"
)
// Markdown chainable context.
type Markdown struct {
htmlFlags int
markdownFlags int
}
// New creates a new instance of the Markdown plugin.
func New() *Markdown {
htmlFlags := blackfriday.HTML_USE_XHTML |
blackfriday.HTML_USE_SMARTYPANTS |
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
blackfriday.HTML_SMARTYPANTS_DASHES |
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
markdownFlags := 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
return &Markdown{htmlFlags: htmlFlags, markdownFlags: markdownFlags}
}
// HtmlFlags sets the HTML flags used by the blackfriday markdown processor;
// see https://github.com/russross/blackfriday/blob/master/html.go for options.
func (plugin *Markdown) HtmlFlags(flags int) *Markdown {
plugin.htmlFlags = flags
return plugin
}
// MarkdownFlags sets the markdown flags used by the blackfriday markdown processor;
// see https://github.com/russross/blackfriday/blob/master/markdown.go for options.
func (plugin *Markdown) MarkdownFlags(flags int) *Markdown {
plugin.markdownFlags = flags
return plugin
}
func (*Markdown) Name() string {
return "markdown"
}
func (plugin *Markdown) Initialize(context *goldsmith.Context) (goldsmith.Filter, error) {
return wildcard.New("**/*.md", "**/*.markdown"), nil
}
func (plugin *Markdown) Process(context *goldsmith.Context, inputFile *goldsmith.File) error {
outputPath := strings.TrimSuffix(inputFile.Path(), path.Ext(inputFile.Path())) + ".html"
if outputFile := context.RetrieveCachedFile(outputPath, inputFile); outputFile != nil {
outputFile.Meta = inputFile.Meta
context.DispatchFile(outputFile)
return nil
}
var buff bytes.Buffer
if _, err := buff.ReadFrom(inputFile); err != nil {
return err
}
var (
renderer = blackfriday.HtmlRenderer(plugin.htmlFlags, "", "")
data = blackfriday.Markdown(buff.Bytes(), renderer, plugin.markdownFlags)
)
outputFile := context.CreateFileFromData(outputPath, data)
outputFile.Meta = inputFile.Meta
context.DispatchAndCacheFile(outputFile, inputFile)
return nil
}
You can’t perform that action at this time.