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

added support for vimeo #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
# goldmark-embed

goldmark-embed is an extension for the [goldmark][goldmark] library that extends
goldmark-embed is based on 13rac1's extension for the [goldmark][goldmark] library that extends
the Markdown `![]()` image embed syntax to support additional media formats.

[goldmark]: http://github.com/yuin/goldmark

YouTube only at first.
Supports YouTube and Vimeo links.

## Demo

This markdown:

```md
# Hello goldmark-embed
# Hello goldmark-embed for YouTube

![](https://www.youtube.com/watch?v=dQw4w9WgXcQ)

# Hello goldmark-embed for Vimeo

![](https://vimeo.com/148751763)
```

Becomes this HTML:

```html
<h1>Hello goldmark-embed</h1>
<p><iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe></p>
<h1>Hello goldmark-embed for Youtube</h1>
<p><iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<h1>Hello goldmark-embed for Vimeo</h1>
<p><iframe src="https://player.vimeo.com/video/148751763?&amp;badge=0&amp;autopause=0&amp;player_id=0&amp;app_id=58479" width="724" height="404" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe></p>
```

### Installation

```bash
go get github.com/13rac1/goldmark-embed
go get github.com/PaperPrototype/goldmark-embed
```

## Usage

```go
markdown := goldmark.New(
goldmark.WithExtensions(
embed.Embed,
embed.New(),
),
)
var buf bytes.Buffer
if err := markdown.Convert([]byte(source), &buf); err != nil {
panic(err)
}

// output html
fmt.Print(buf)
}
```
Expand All @@ -57,6 +63,7 @@ go get github.com/13rac1/goldmark-embed

MIT

## Author
## Authors

Brad Erickson
![Brad Erickson](https://github.com/13rac1)
![Abdiel Lopez](https://github.com/PaperPrototype)
68 changes: 32 additions & 36 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,6 @@ func (e *embedExtension) Extend(m goldmark.Markdown) {
)
}

// YouTube struct represents a YouTube Video embed of the Markdown text.
type YouTube struct {
ast.Image
Video string
}

// KindYouTube is a NodeKind of the YouTube node.
var KindYouTube = ast.NewNodeKind("YouTube")

// Kind implements Node.Kind.
func (n *YouTube) Kind() ast.NodeKind {
return KindYouTube
}

// NewYouTube returns a new YouTube node.
func NewYouTube(img *ast.Image, v string) *YouTube {
c := &YouTube{
Image: *img,
Video: v,
}
c.Destination = img.Destination
c.Title = img.Title

return c
}

type astTransformer struct{}

var defaultASTTransformer = &astTransformer{}
Expand All @@ -79,6 +53,7 @@ func (a *astTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
}

img := n.(*ast.Image)
// parse the url
u, err := url.Parse(string(img.Destination))
if err != nil {
msg := ast.NewString([]byte(fmt.Sprintf("<!-- %s -->", err)))
Expand All @@ -87,15 +62,24 @@ func (a *astTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
return ast.WalkContinue, nil
}

if u.Host != "www.youtube.com" || u.Path != "/watch" {
return ast.WalkContinue, nil
}
v := u.Query().Get("v")
if v == "" {
return ast.WalkContinue, nil
if u.Host == "www.youtube.com" && u.Path == "/watch" {
// if YouTube

v := u.Query().Get("v")
if v == "" {
return ast.WalkContinue, nil
}
youtube := NewYouTube(img, v)
n.Parent().ReplaceChild(n.Parent(), n, youtube)

} else if u.Host == "vimeo.com" {
// if Vimeo

// remove the '/' from url
path := u.Path[1:]
vimeo := NewVimeo(img, path)
n.Parent().ReplaceChild(n.Parent(), n, vimeo)
}
yt := NewYouTube(img, v)
n.Parent().ReplaceChild(n.Parent(), n, yt)

return ast.WalkContinue, nil
}
Expand All @@ -115,15 +99,27 @@ func NewHTMLRenderer() renderer.NodeRenderer {
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindYouTube, r.renderYouTubeVideo)
reg.Register(KindVimeo, r.renderVimeoVideo)
}

func (r *HTMLRenderer) renderYouTubeVideo(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
return ast.WalkContinue, nil
}

yt := node.(*YouTube)
youtube := node.(*YouTube)

w.Write([]byte(`<iframe width="560" height="315" src="https://www.youtube.com/embed/` + youtube.Video + `" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`))
return ast.WalkContinue, nil
}

func (r *HTMLRenderer) renderVimeoVideo(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
return ast.WalkContinue, nil
}

vimeo := node.(*Vimeo)

w.Write([]byte(`<iframe width="560" height="315" src="https://www.youtube.com/embed/` + yt.Video + `" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`))
w.Write([]byte(`<iframe src="https://player.vimeo.com/video/` + vimeo.Video + `?&amp;badge=0&amp;autopause=0&amp;player_id=0" width="724" height="404" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>`))
return ast.WalkContinue, nil
}
24 changes: 20 additions & 4 deletions embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"testing"

embed "github.com/13rac1/goldmark-embed"
embed "github.com/PaperPrototype/goldmark-embed"
"github.com/yuin/goldmark"
)

Expand All @@ -14,18 +14,34 @@ func TestMeta(t *testing.T) {
embed.New(),
),
)
source := `# Hello goldmark-embed
source := `# Hello goldmark-embed for Youtube

![](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
`
var buf bytes.Buffer
if err := markdown.Convert([]byte(source), &buf); err != nil {
panic(err)
}
if buf.String() != `<h1>Hello goldmark-embed</h1>
if buf.String() != `<h1>Hello goldmark-embed for Youtube</h1>
<p><iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
` {
t.Error("Invalid HTML output")
t.Error("Invalid HTML output for YouTube")
t.Log(buf.String())
}

source2 := `# Hello goldmark-embed for Vimeo

![](https://vimeo.com/148751763)
`

var buf2 bytes.Buffer
if err2 := markdown.Convert([]byte(source2), &buf2); err2 != nil {
panic(err2)
}
if buf2.String() != `<h1>Hello goldmark-embed for Vimeo</h1>
<p><iframe src="https://player.vimeo.com/video/148751763?&amp;badge=0&amp;autopause=0&amp;player_id=0&amp;app_id=58479" width="724" height="404" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe></p>
` {
t.Error("Invalid HTML output for Vimeo")
t.Log(buf2.String())
}
}
31 changes: 31 additions & 0 deletions embed_vimeo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package embed

import (
"github.com/yuin/goldmark/ast"
)

// Vimeo struct represents a Vimeo Video embed of the markdown text.
type Vimeo struct {
ast.Image
Video string
}

// KindVimeo is a NodeKind of the Vimeo node.
var KindVimeo = ast.NewNodeKind("Vimeo")

// implements Node.Kind.
func (node *Vimeo) Kind() ast.NodeKind {
return KindVimeo
}

// New Vimeo returns a new Vimeo node.
func NewVimeo(img *ast.Image, video string) *Vimeo {
vimeo := &Vimeo{
Image: *img,
Video: video,
}
vimeo.Destination = img.Destination
vimeo.Title = img.Title

return vimeo
}
31 changes: 31 additions & 0 deletions embed_youtube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package embed

import (
"github.com/yuin/goldmark/ast"
)

// YouTube struct represents a YouTube Video embed of the Markdown text.
type YouTube struct {
ast.Image
Video string
}

// KindYouTube is a NodeKind of the YouTube node.
var KindYouTube = ast.NewNodeKind("YouTube")

// Kind implements Node.Kind.
func (node *YouTube) Kind() ast.NodeKind {
return KindYouTube
}

// NewYouTube returns a new YouTube node.
func NewYouTube(img *ast.Image, video string) *YouTube {
vimeo := &YouTube{
Image: *img,
Video: video,
}
vimeo.Destination = img.Destination
vimeo.Title = img.Title

return vimeo
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/13rac1/goldmark-embed
module github.com/PaperPrototype/goldmark-embed

go 1.14
go 1.17

require github.com/yuin/goldmark v1.2.1
require github.com/yuin/goldmark v1.4.12
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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 v1.4.12 h1:6hffw6vALvEDqJ19dOJvJKOoAOKe4NDaTqvd2sktGN0=
github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=