frostedmd

package
v0.0.0-...-cf5c4a9 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package frostedmd implements Frosted Markdown: standard Markdown to HTML conversion with a meta map and a default title. Parsing and rendering are handled by the excellent Blackfriday package; the Meta map is extracted from the first code block encountered, but only if it is not preceded by anything other than an optional header. The order can be reversed globally by setting META_AT_END to true, or at the Parser level. In reversed order the meta code block must be the last element in the Markdown source.

If the Meta contains no Title (nor "title" nor "TITLE") then the first heading is used, if and only if that heading was not preceded by any other block besides the Meta Block.

Supported languages for the meta block are JSON and YAML (the default); additional languages as well as custom parsers are planned for the future.

If an appropriate meta block is found it will be excluded from the rendered HTML content.

NOTE: This package will most likely be renamed, and might also be moved out of kisipar. "Greysunday" was pretty tempting but then the sun came out...

Example
package main

import (
	"fmt"
	"github.com/biztos/kisipar/frostedmd"
)

func main() {

	// The easiest way to get things done:
	input := `# My Markdown

    # Meta:
    Tags: ["fee","fi","foe"]

Obscurantism threatens clean data.
`

	res, err := frostedmd.MarkdownCommon([]byte(input))
	if err != nil {
		panic(err)
	}
	mm := res.Meta()
	fmt.Println("Title:", mm["Title"])
	fmt.Println("Tags:", mm["Tags"])
	fmt.Println("HTML:", string(res.Content()))

}
Output:

Title: My Markdown
Tags: [fee fi foe]
HTML: <h1>My Markdown</h1>

<p>Obscurantism threatens clean data.</p>

Index

Examples

Constants

The "Common" set of Blackfriday extensions; highly recommended for productive use of Markdown.

The "Common" set of Blackfriday HTML flags; also highly recommended.

Variables

View Source
var META_AT_END = false

If true, get meta block from end of the Markdown source by default.

Functions

This section is empty.

Types

type ParseResult

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

ParseResult defines the result of a Parse operation.

func MarkdownBasic

func MarkdownBasic(input []byte) (*ParseResult, error)

MarkdownBasic converts Markdown input using the same options as blackfriday.MarkdownBasic. This is simply a convenience method for:

NewBasic().Parse(input)

func MarkdownCommon

func MarkdownCommon(input []byte) (*ParseResult, error)

MarkdownCommon converts Markdown input using the same options as blackfriday.MarkdownCommon. This is simply a convenience method for:

New().Parse(input)

func (*ParseResult) Content

func (r *ParseResult) Content() []byte

Content returns the content portion of a ParseResult.

func (*ParseResult) Meta

func (r *ParseResult) Meta() map[string]interface{}

Meta returns the meta portion of a ParseResult.

type Parser

type Parser struct {
	MetaAtEnd          bool
	MarkdownExtensions int // uses blackfriday EXTENSION_* constants
	HtmlFlags          int // uses blackfridy HTML_* constants
}

Parser defines a parser-renderer that implements the page.Parser interface. It may be also be used to

func New

func New() *Parser

New returns a new Parser with the common flags and extensions enabled.

Example
package main

import (
	"fmt"
	"github.com/biztos/kisipar/frostedmd"
)

func main() {

	input := `# Lots of Data

Here we have a full dataset, which (for instance) a template engine
will turn into something cool and dynamic.  Thus we put it at the end
so we can read our nice summary using the *head* command.

    {
        "datasets": {
             "numbers": [11,22,33,44,55,66],
             "letters": ["a","B","ß","í"]
        }
    }

`

	parser := frostedmd.New()
	parser.MetaAtEnd = true
	res, err := parser.Parse([]byte(input))
	if err != nil {
		panic(err)
	}
	mm := res.Meta()
	fmt.Println("Title:", mm["Title"])
	fmt.Println("HTML:", string(res.Content()))

	// Order within a map is random in Go, so let's make it explicit.
	fmt.Println("Data sets:")
	if ds, ok := mm["datasets"].(map[interface{}]interface{}); ok {
		fmt.Println("  numbers:", ds["numbers"])
		fmt.Println("  letters:", ds["letters"])
	} else {
		fmt.Printf("NOT A MAP: %T\n", mm["datasets"])
	}

}
Output:

Title: Lots of Data
HTML: <h1>Lots of Data</h1>

<p>Here we have a full dataset, which (for instance) a template engine
will turn into something cool and dynamic.  Thus we put it at the end
so we can read our nice summary using the <em>head</em> command.</p>

Data sets:
  numbers: [11 22 33 44 55 66]
  letters: [a B ß í]

func NewBasic

func NewBasic() *Parser

NewBasic returns a new Parser without the common flags and extensions.

func (*Parser) Parse

func (p *Parser) Parse(input []byte) (*ParseResult, error)

Parse converts Markdown input into a meta map and HTML content fragment, thus implementing the page.Parser interface. If an error is encountered while parsing the meta block, the rendered content is still returned. Thus the caller may choose to handle meta errors without interrupting flow.

Jump to

Keyboard shortcuts

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