markdown

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2020 License: MIT Imports: 8 Imported by: 0

README

postdog - Markdown plugin

This plugin provides Markdown support for mails.

Example

package main

import (
  "context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/markdown"
	gm "github.com/bounoable/postdog/plugin/markdown/goldmark"
	"github.com/yuin/goldmark"
)

func main() {
  po := postdog.New(
    postdog.WithPlugin(
			markdown.Plugin(
				gm.Converter(goldmark.New()), // use goldmark Markdown parser
				markdown.OverrideHTML(true),  // plugin options
			),
		),
  )

  err := po.Send(context.Background(), letter.Write(
    letter.Text("# Heading 1\n# Heading 2"), // The HTML body of the letter will be replaced with the generated HTML
  ))
}

Documentation

Overview

Package markdown provides Markdown support for letters. It registers a middleware that converts the text body of letters with a Markdown converter and sets the HTML body to the conversion result.

Example (Autowire)
package main

import (
	"context"
	"strings"

	"github.com/bounoable/postdog/autowire"
	"github.com/bounoable/postdog/letter"
)

func main() {
	config := `
plugins:
  - name: markdown
	use: goldmark
`

	cfg, err := autowire.Load(strings.NewReader(config))
	if err != nil {
		panic(err)
	}

	po, err := cfg.Office(context.Background())
	if err != nil {
		panic(err)
	}

	err = po.Send(context.Background(), letter.Write(
		letter.Text("# Heading 1\n# Heading 2"),
	))
}
Output:

Example (Basic)
package main

import (
	"context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/markdown"

	gm "github.com/bounoable/postdog/plugin/markdown/goldmark"
	"github.com/yuin/goldmark"
)

func main() {
	po := postdog.New(
		postdog.WithPlugin(
			markdown.Plugin(
				gm.Converter(goldmark.New()), // use goldmark Markdown parser
				markdown.OverrideHTML(true),  // plugin options
			),
		),
	)

	err := po.Send(context.Background(), letter.Write(
		letter.Text("# Heading 1\n# Heading 2"), // The HTML body of the letter will be replaced with the generated HTML
	))

	_ = err
}
Output:

Example (Disable)
package main

import (
	"context"

	"github.com/bounoable/postdog"
	"github.com/bounoable/postdog/letter"
	"github.com/bounoable/postdog/plugin/markdown"

	gm "github.com/bounoable/postdog/plugin/markdown/goldmark"
	"github.com/yuin/goldmark"
)

func main() {
	po := postdog.New(
		postdog.WithPlugin(
			markdown.Plugin(gm.Converter(goldmark.New())),
		),
	)

	ctx := markdown.Disable(context.Background()) // disable Markdown conversion for this context

	po.Send(ctx, letter.Write(letter.Text("# Heading"))) // letter.HTML will stay empty
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Name is the plugin name.
	Name = "markdown"
)

Functions

func AutowirePlugin added in v0.4.0

func AutowirePlugin(_ context.Context, cfg map[string]interface{}) (postdog.Plugin, error)

AutowirePlugin creates the Markdown plugin from the autowire config.

func Disable

func Disable(ctx context.Context) context.Context

Disable disables Markdown conversions for all letters that are sent with this ctx.

func Disabled

func Disabled(ctx context.Context) bool

Disabled determines if Markdown conversion is disabled for ctx.

func Enable

func Enable(ctx context.Context) context.Context

Enable (re)enables Markdown conversions for ctx.

func Enabled

func Enabled(ctx context.Context) bool

Enabled determines if Markdown conversion is enabled for ctx.

func Plugin

func Plugin(conv Converter, opts ...Option) postdog.PluginFunc

Plugin is the install function for the Markdown plugin. It takes the Text field of the outgoing letters, converts them and sets the HTML field to the result.

func PluginWithConfig

func PluginWithConfig(conv Converter, cfg Config) postdog.PluginFunc

PluginWithConfig is the install function for the Markdown plugin. It takes the Text field of the outgoing letters, converts them and sets the HTML field to the result.

func Register added in v0.4.0

func Register(cfg *autowire.Config)

Register registers the plugin factory in the autowire config.

func RegisterConverter added in v0.4.0

func RegisterConverter(name string, factory ConverterFactory)

RegisterConverter registers a converter factory for the given converter name for autowiring.

Types

type Config

type Config struct {
	// Override HTML field even if it's already filled.
	OverrideHTML bool
}

Config is the plugin configuration.

type Converter

type Converter interface {
	Convert(src []byte, w io.Writer) error
}

Converter converts a Markdown source to HTML.

type ConverterFactory added in v0.4.0

type ConverterFactory interface {
	CreateConverter(cfg map[string]interface{}) (Converter, error)
}

ConverterFactory creates the Markdown converter from the autowire config.

type ConverterFactoryFunc added in v0.4.0

type ConverterFactoryFunc func(map[string]interface{}) (Converter, error)

ConverterFactoryFunc allows functions to be used as a ConverterFactory.

func (ConverterFactoryFunc) CreateConverter added in v0.4.0

func (fn ConverterFactoryFunc) CreateConverter(cfg map[string]interface{}) (Converter, error)

CreateConverter creates the Markdown converter from the autowire config.

type ConverterFunc

type ConverterFunc func([]byte, io.Writer) error

ConverterFunc allows a function to be used as Converter.

func (ConverterFunc) Convert

func (fn ConverterFunc) Convert(src []byte, w io.Writer) error

Convert converts the Markdown in src to HTML and writes the result to w.

type Option

type Option func(*Config)

Option is a plugin option.

func OverrideHTML

func OverrideHTML(override bool) Option

OverrideHTML overrides the HTML body of the letter, even if it is already filled.

type UnregisteredConverterError added in v0.4.0

type UnregisteredConverterError struct {
	Name string
}

UnregisteredConverterError means the autowire config defines the plugin with an unregistered converter name.

func (UnregisteredConverterError) Error added in v0.4.0

func (err UnregisteredConverterError) Error() string

Directories

Path Synopsis
Package goldmark provides an adapter for the goldmark Markdown parser.
Package goldmark provides an adapter for the goldmark Markdown parser.
Package mock_markdown is a generated GoMock package.
Package mock_markdown is a generated GoMock package.

Jump to

Keyboard shortcuts

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