Skip to content

Commit

Permalink
change vimeo & youtube embed plugins & README plugin list
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Dec 27, 2023
1 parent fc090b2 commit 155a78b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 43 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ converter.Use(plugin.Strikethrough(""))

For more information have a look at the example [github_flavored](/examples/github_flavored/main.go).

---

These are the plugins located in the [plugin folder](/plugin) which you can use by importing "github.com/JohannesKaufmann/html-to-markdown/plugin".

| Name | Description |
| --------------------- | ------------------------------------------------------------------------------------------- |
| GitHubFlavored | GitHub's Flavored Markdown contains `TaskListItems`, `Strikethrough` and `Table`. |
| TaskListItems | (Included in `GitHubFlavored`). Converts `<input>` checkboxes into `- [x] Task`. |
| Strikethrough | (Included in `GitHubFlavored`). Converts `<strike>`, `<s>`, and `<del>` to the `~~` syntax. |
| Table | (Included in `GitHubFlavored`). Convert a `<table>` into something like this... |
| TableCompat | |
| | |
| VimeoEmbed | |
| YoutubeEmbed | |
| | |
| ConfluenceCodeBlock | Converts `<ac:structured-macro>` elements that are used in Atlassian’s Wiki "Confluence". |
| ConfluenceAttachments | Converts `<ri:attachment ri:filename=""/>` elements. |

These are the plugins in other repositories:

| Name | Description |
| ---------------------------- | ------------------- |
| \[Plugin Name\]\(Your Link\) | A short description |

I you write a plugin, feel free to open a PR that adds your Plugin to this list.

## Writing Plugins

Have a look at the [plugin folder](/plugin) for a reference implementation. The most basic one is [Strikethrough](/plugin/strikethrough.go).
Expand Down
4 changes: 2 additions & 2 deletions plugin/vimeo.go → plugin/iframe_vimeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ const (
VimeoWithDescription
)

// EXPERIMENTALVimeoEmbed registers a rule (for iframes) and
// VimeoEmbed registers a rule (for iframes) and
// returns a markdown compatible representation (link to video, ...).
func EXPERIMENTALVimeoEmbed(variation vimeoVariation) md.Plugin {
func VimeoEmbed(variation vimeoVariation) md.Plugin {
return func(c *md.Converter) []md.Rule {
getVimeoData := func(id string) (*vimeoVideo, error) {
u := fmt.Sprintf("http://vimeo.com/api/oembed.json?url=https://vimeo.com/%s", id)
Expand Down
40 changes: 40 additions & 0 deletions plugin/iframe_youtube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package plugin

import (
"fmt"
"regexp"
"strings"

md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)

var youtubeID = regexp.MustCompile(`youtube\.com\/embed\/([^\&\?\/]+)`)

// YoutubeEmbed registers a rule (for iframes) and
// returns a markdown compatible representation (link to video, ...).
func YoutubeEmbed() md.Plugin {
return func(c *md.Converter) []md.Rule {
return []md.Rule{
{
Filter: []string{"iframe"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
src := selec.AttrOr("src", "")
if !strings.Contains(src, "youtube.com") {
return nil
}
alt := selec.AttrOr("title", "")

parts := youtubeID.FindStringSubmatch(src)
if len(parts) != 2 {
return nil
}
id := parts[1]

text := fmt.Sprintf("[![%s](https://img.youtube.com/vi/%s/0.jpg)](https://www.youtube.com/watch?v=%s)", alt, id, id)
return &text
},
},
}
}
}
10 changes: 5 additions & 5 deletions plugin/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/PuerkitoBio/goquery"
)

// TableCompat is a compatibility plugon for environments where
// TableCompat is a compatibility plugin for environments where
// only commonmark markdown (without Tables) is supported.
//
// Note: In an environment that supports "real" Tables, like GitHub's Flavored Markdown
Expand Down Expand Up @@ -132,10 +132,10 @@ func Table() md.Plugin {
}

// A tr is a heading row if:
// - the parent is a THEAD
// - or if its the first child of the TABLE or the first TBODY (possibly
// following a blank THEAD)
// - and every cell is a TH
// - the parent is a THEAD
// - or if its the first child of the TABLE or the first TBODY (possibly
// following a blank THEAD)
// - and every cell is a TH
func isHeadingRow(s *goquery.Selection) bool {
parent := s.Parent()

Expand Down
36 changes: 0 additions & 36 deletions plugin/youtube.go

This file was deleted.

0 comments on commit 155a78b

Please sign in to comment.