mdhttp

package module
v0.0.0-...-25c1858 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2018 License: MIT Imports: 14 Imported by: 0

README

mdhttp godoc goreport

mdhttp is a small package that lets you seemlessly render markdown files via a Go http middleware. It will (by default) use Github-style theming, and Chroma for syntax highlighting.

License

LICENSE: The MIT License (MIT)
Copyright (c) 2017 Liam Stanley <me@liamstanley.io>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRenderer

func DefaultRenderer(w http.ResponseWriter, r *http.Request, mdf *MarkdownFile)

DefaultRenderer satisfies the Renderer type, and can be used with New() and NewMiddleware().

func New

func New(prefix string, fs http.FileSystem, renderer Renderer) http.Handler

New returns a new markdown renderer. Prefix is url path to strip from the URL. fs is the filesystem you want to use, and renderer is an optional custom renderer (default uses DefaultRenderer). New() differs from NewMiddleware() in that New() only allows rendering markdown files, and will not serve other static files.

func NewMiddleware

func NewMiddleware(prefix string, fs http.FileSystem, renderer Renderer) func(next http.Handler) http.Handler

NewMiddleware is like New() however it allows serving non-markdown files. Note that because this is a middleware, if the middleware encounters a markdown file, it doesn't continue executing the middleware chain, and will return early. Make sure to have this be the last middleware in the chain to have all of middleware ahead of this get executed properly for pages with markdown content on them.

Types

type Markdown

type Markdown struct {
	// contains filtered or unexported fields
}

Markdown is an instantiated handler.

func (*Markdown) Register

func (md *Markdown) Register(next http.Handler) http.Handler

Register is a way of using mdhttp as a middleware that seemlessly renders markdown files.

func (*Markdown) ServeHTTP

func (md *Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MarkdownFile

type MarkdownFile struct {
	Title    string
	Path     string
	FileInfo os.FileInfo
	Content  []byte
	// contains filtered or unexported fields
}

MarkdownFile contains all of the necessary components to render the markdown file.

func (*MarkdownFile) Body

func (mdf *MarkdownFile) Body() template.HTML

Body returns a template.HTML wrapped HTML representation of the Markdown body.

func (*MarkdownFile) GetAttr

func (mdf *MarkdownFile) GetAttr(name string) string

GetAttr gets one of the attributes (metadata) which was defined at the top of the markdown file. For example, you can pull "Created" or "Title" from the following example:

Created: <some date>
Title: Example Page

## Your Markdown Here

func (*MarkdownFile) HTML

func (mdf *MarkdownFile) HTML() (toc, body string)

HTML returns the rendered HTML version of the markdown file, by default sanitized ith UGC policies (bluemonday), and common markdown format (blackfriday).

func (*MarkdownFile) TOC

func (mdf *MarkdownFile) TOC() template.HTML

TOC returns a template.HTML wrapped HTML representation of the Markdown table of contents.

type Renderer

type Renderer func(w http.ResponseWriter, r *http.Request, mdr *MarkdownFile)

Renderer is the render function type which is used by the Markdown middleware. This allows you to customize how the markdown is rendered (e.g. you could use your own template wrapping, or custom styles). See the source code for DefaultRenderer for how you would implement this.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL