Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for :emoji: #92

Merged
merged 2 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ansi/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"strings"

east "github.com/yuin/goldmark-emoji/ast"
"github.com/yuin/goldmark/ast"
astext "github.com/yuin/goldmark/extension/ast"
)
Expand Down Expand Up @@ -375,6 +376,14 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
case ast.KindTextBlock:
return Element{}

case east.KindEmoji:
n := node.(*east.Emoji)
return Element{
Renderer: &BaseElement{
Token: string(n.Value.Unicode),
},
}

// Unknown case
default:
fmt.Println("Warning: unhandled element", node.Kind().String())
Expand Down
4 changes: 4 additions & 0 deletions ansi/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/muesli/termenv"
east "github.com/yuin/goldmark-emoji/ast"
"github.com/yuin/goldmark/ast"
astext "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/renderer"
Expand Down Expand Up @@ -79,6 +80,9 @@ func (r *ANSIRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {

// strikethrough
reg.Register(astext.KindStrikethrough, r.renderNode)

// emoji
reg.Register(east.KindEmoji, r.renderNode)
}

func (r *ANSIRenderer) renderNode(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
Expand Down
3 changes: 3 additions & 0 deletions ansi/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/muesli/termenv"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
Expand Down Expand Up @@ -56,6 +57,7 @@ func TestRenderer(t *testing.T) {
goldmark.WithExtensions(
extension.GFM,
extension.DefinitionList,
emoji.Emoji,
muesli marked this conversation as resolved.
Show resolved Hide resolved
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
Expand Down Expand Up @@ -127,6 +129,7 @@ func TestRendererIssues(t *testing.T) {
goldmark.WithExtensions(
extension.GFM,
extension.DefinitionList,
emoji.Emoji,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
Expand Down
9 changes: 9 additions & 0 deletions glamour.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/muesli/termenv"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
Expand Down Expand Up @@ -184,6 +185,14 @@ func WithWordWrap(wordWrap int) TermRendererOption {
}
}

// WithEmoji sets a TermRenderer's emoji rendering.
func WithEmoji() TermRendererOption {
return func(tr *TermRenderer) error {
emoji.New().Extend(tr.md)
return nil
}
}

func (tr *TermRenderer) Read(b []byte) (int, error) {
return tr.renderBuf.Read(b)
}
Expand Down
23 changes: 23 additions & 0 deletions glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package glamour
import (
"bytes"
"io/ioutil"
"strings"
"testing"
)

Expand Down Expand Up @@ -90,6 +91,28 @@ func TestTermRenderer(t *testing.T) {
}
}

func TestWithEmoji(t *testing.T) {
r, err := NewTermRenderer(
WithEmoji(),
)
if err != nil {
t.Fatal(err)
}

b, err := r.Render(":+1:")
if err != nil {
t.Fatal(err)
}
b = strings.TrimSpace(b)

// Thumbs up unicode character
td := "\U0001f44d"

if td != b {
t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", td, b)
}
}

func TestStyles(t *testing.T) {
_, err := NewTermRenderer(
WithAutoStyle(),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require (
github.com/muesli/termenv v0.7.4
github.com/olekukonko/tablewriter v0.0.4
github.com/yuin/goldmark v1.2.1
github.com/yuin/goldmark-emoji v1.0.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/yuin/goldmark v1.2.1 h1:ruQGxdhGHe7FWOJPT0mKs5+pD2Xs1Bm/kdGlHO04FmM=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark-emoji v1.0.1 h1:ctuWEyzGBwiucEqxzwe0SOYDXPAucOrE9NQC18Wa1os=
github.com/yuin/goldmark-emoji v1.0.1/go.mod h1:2w1E6FEWLcDQkoTE+7HU6QF1F6SLlNGjRIBbIZQFqkQ=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
Expand Down
1 change: 1 addition & 0 deletions styles/examples/emoji.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:octopus: :zap: :cat: = :heart:
1 change: 1 addition & 0 deletions styles/examples/emoji.style
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "emoji": {} }
1 change: 1 addition & 0 deletions testdata/emoji.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
🐙 ⚡ 🐱 = ❤️
16 changes: 16 additions & 0 deletions testdata/issues/48.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
emoji in text

:octopus: :zap: :cat: = :heart:

emoji in header

## :octopus: :zap: :cat: = :heart:

no emoji in code blocks
```
:octopus: :zap: :cat: = :heart:
```

no emoji in inline code

`:octopus: :zap: :cat: = :heart:`
17 changes: 17 additions & 0 deletions testdata/issues/48.test

Large diffs are not rendered by default.