front

package module
v0.0.0-...-2d1c01f Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2021 License: MIT Imports: 7 Imported by: 0

README

front

Extracts front matter.

Modified fork of github.com/gernest/front

Features

  • Custom delimiters (any three character string. e.g +++, $$$, ---, %%%)
  • Supports YAML frontmatter
  • Supports JSON frontmatter
  • JSON or Map output
  • Supports additional delimiters in the body

Installation

go get github.com/barshociaj/front

How to use

package main

import (
	"fmt"
	"strings"

	"github.com/barshociaj/front"
)

var txt = `+++
{
    "title":"front"
}
+++

# Body
Over my dead body
`

func main() {
	m := front.NewMatter("+++")
	front, body, err := m.JSONToMap(strings.NewReader(txt))
	if err != nil {
		panic(err)
	}

	fmt.Printf("The front matter is:\n%#v\n", front)
	fmt.Printf("The body is:\n%q\n", body)
}
m.YAMLToJSON(fileReader)

Convert YAML front matter to JSON ([]byte)

m := front.NewMatter("---")
front, body, err := m.YAMLToJSON(fileReader)
m.YAMLToMap(fileReader)

Convert YAML front matter to a map (map[string]interface{})

m := front.NewMatter("---")
front, body, err := m.YAMLToMap(fileReader)
m.JSONToMap(fileReader)

Convert JSON front matter to a map (map[string]interface{})

m := front.NewMatter("---")
front, body, err := m.JSONToMap(fileReader)

Licence

This project is under the MIT Licence. See the LICENCE file for the full license text.

Documentation

Overview

Package front is a frontmatter extraction library.

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrNoFront is an error indicating no front matter was found
	ErrNoFront = errors.New("front: no front matter found")
)

Functions

func JSONToMap

func JSONToMap(front string) (map[string]interface{}, error)

JSONToMap decodes JSON string into a go map[string]interface{}

func JSONViaPointer

func JSONViaPointer(front string, rst interface{}) error

func YAMLHandler

func YAMLHandler(front string) (map[string]interface{}, error)

YAMLHandler decodes yaml string into a go map[string]interface{}

Types

type Matter

type Matter struct {
	Delim string
}

Matter is all what matters here.

func NewMatter

func NewMatter(delim string) *Matter

NewMatter creates a new Matter instance

func (*Matter) JSONToMap

func (m *Matter) JSONToMap(input io.Reader) (front map[string]interface{}, body string, err error)

JSONToMap parses the input and extract JSON frontmatter as map[string]interface{}

func (*Matter) JSONViaPointer

func (m *Matter) JSONViaPointer(input io.Reader, front interface{}) (body string, err error)

func (*Matter) YAMLToJSON

func (m *Matter) YAMLToJSON(input io.Reader) (front []byte, body string, err error)

YAMLToJSON parses the input and extract YAML frontmatter as []byte containing JSON

func (*Matter) YAMLToMap

func (m *Matter) YAMLToMap(input io.Reader) (front map[string]interface{}, body string, err error)

YAMLToMap parses the input and extract YAML frontmatter as map[string]interface{}

Jump to

Keyboard shortcuts

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