Skip to content

Commit

Permalink
Merge pull request googlecodelabs#400 from kroikie/enable-yt
Browse files Browse the repository at this point in the history
Enable YouTube support via markdown
  • Loading branch information
cassierecher authored Mar 24, 2020
2 parents decf7ff + 8244f28 commit d267a62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 4 additions & 0 deletions claat/parser/md/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func isList(hn *html.Node) bool {
return hn.DataAtom == atom.Ul || hn.DataAtom == atom.Ol
}

func isYoutube(hn *html.Node) bool {
return hn.DataAtom == atom.Video
}

// countTwo starts counting the number of a Atom children in hn.
// It returns as soon as the count exceeds 1, so the returned value is inexact.
//
Expand Down
19 changes: 9 additions & 10 deletions claat/parser/md/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ func parseNode(ds *docState) (types.Node, bool) {
return survey(ds), true
case isTable(ds.cur):
return table(ds), true
case isYoutube(ds.cur):
return youtube(ds), true
}
return nil, false
}
Expand Down Expand Up @@ -696,17 +698,14 @@ func image(ds *docState) types.Node {
}

func youtube(ds *docState) types.Node {
u, err := url.Parse(nodeAttr(ds.cur, "alt"))
if err != nil {
return nil
}
v := u.Query().Get("v")
if v == "" {
return nil
for _, attr := range ds.cur.Attr {
if attr.Key == "id" {
n := types.NewYouTubeNode(attr.Val)
n.MutateBlock(true)
return n
}
}
n := types.NewYouTubeNode(v)
n.MutateBlock(true)
return n
return nil
}

func iframe(ds *docState) types.Node {
Expand Down

0 comments on commit d267a62

Please sign in to comment.