emoji

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: MIT Imports: 11 Imported by: 42

README

goldmark-emoji

GoDev

goldmark-emoji is an extension for the goldmark that parses :joy: style emojis.

Installation

go get github.com/yuin/goldmark-emoji

Usage

import (
    "bytes"
    "fmt"

    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark-emoji"
    "github.com/yuin/goldmark-emoji/definition"
)

func main() {
    markdown := goldmark.New(
        goldmark.WithExtensions(
            emoji.Emoji,
        ),
    )
    source := `
    Joy :joy:
    `
    var buf bytes.Buffer
    if err := markdown.Convert([]byte(source), &buf); err != nil {
        panic(err)
    }
    fmt.Print(buf.String())
}

See emoji_test.go for detailed usage.

Options

Options for the extension

Option Description
WithEmojis Definition of emojis. This defaults to github emoji set
WithRenderingMethod Entity : renders as HTML entities, Twemoji : renders as an img tag that uses twemoji, Func : renders using a go function
WithTwemojiTemplate Twemoji img tag printf template
WithRendererFunc renders by a go function

License

MIT

Author

Yusuke Inuzuka

Documentation

Overview

package emoji is a extension for the goldmark(http://github.com/yuin/goldmark).

Index

Constants

View Source
const DefaultTwemojiTemplate = `<img class="emoji" draggable="false" alt="%[1]s" src="https://twemoji.maxcdn.com/v/latest/72x72/%[2]s.png"%[3]s>`

DefaultTwemojiTemplate is a default value for RendererConfig.TwemojiTemplate.

Variables

View Source
var Emoji = &emoji{
	options: []Option{},
}

Emoji is a goldmark.Extender implementation.

Functions

func New

func New(opts ...Option) goldmark.Extender

New returns a new extension with given options.

func NewHTMLRenderer

func NewHTMLRenderer(opts ...RendererOption) renderer.NodeRenderer

NewHTMLRenderer returns a new HTMLRenderer.

func NewParser

func NewParser(opts ...ParserOption) parser.InlineParser

NewParser returns a new parser.InlineParser that can parse emoji expressions.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option interface sets options for this extension.

func WithEmojis

func WithEmojis(value definition.Emojis) Option

WithMaping is a functional option that defines links names to unicode emojis.

func WithRendererFunc

func WithRendererFunc(f RendererFunc) Option

WithRendererFunc is a functional option that changes a renderer func.

func WithRenderingMethod

func WithRenderingMethod(a RenderingMethod) Option

WithRenderingMethod is a functional option that indicates how emojis are rendered.

func WithTwemojiTemplate

func WithTwemojiTemplate(s string) Option

WithTwemojiTemplate is a functional option that changes a twemoji img tag.

type ParserConfig

type ParserConfig struct {
	Emojis definition.Emojis
}

ParserConfig struct is a data structure that holds configuration of the Emoji extension.

func (*ParserConfig) SetOption

func (c *ParserConfig) SetOption(name parser.OptionName, value interface{})

SetOption implements parser.SetOptioner

type ParserOption

type ParserOption interface {
	Option
	parser.Option

	SetEmojiOption(*ParserConfig)
}

A ParserOption interface sets options for the emoji parser.

type RendererConfig

type RendererConfig struct {
	html.Config

	// Method indicates how emojis are rendered.
	Method RenderingMethod

	// TwemojiTemplate is a printf template for twemoji. This value is valid only when Method is set to Twemoji.
	// `printf` arguments are:
	//
	//     1: name (e.g. "face with tears of joy")
	//     2: file name without an extension (e.g. 1f646-2642)
	//     3: '/' if XHTML, otherwise ”
	//
	TwemojiTemplate string

	// RendererFunc is a RendererFunc that renders emojis. This value is valid only when Method is set to Func.
	RendererFunc RendererFunc
}

RendererConfig struct holds options for the emoji renderer.

func (*RendererConfig) SetOption

func (c *RendererConfig) SetOption(name renderer.OptionName, value interface{})

SetOption implements renderer.SetOptioner.

type RendererFunc

type RendererFunc func(w util.BufWriter, source []byte, n *east.Emoji, config *RendererConfig)

RendererFunc will be used for rendering emojis.

type RendererOption

type RendererOption interface {
	Option
	renderer.Option

	SetEmojiOption(*RendererConfig)
}

A RendererOption interface sets options for the emoji renderer.

type RenderingMethod

type RenderingMethod int

RenderingMethod indicates how emojis are rendered.

const (
	// Entity renders an emoji as an html entity.
	Entity RenderingMethod = iota

	// Unicode renders an emoji as unicode character.
	Unicode

	// Twemoji renders an emoji as an img tag with [twemoji](https://github.com/twitter/twemoji).
	Twemoji

	// Func renders an emoji using RendererFunc.
	Func
)

Directories

Path Synopsis
Package ast defines AST nodes that represetns emoji extension's elements.
Package ast defines AST nodes that represetns emoji extension's elements.

Jump to

Keyboard shortcuts

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