Skip to content

Instantly share code, notes, and snippets.

@kixiQu
Created Nov 20, 2020
Embed
What would you like to do?
oembed for jekyll

modified version of https://gist.github.com/vanto/1455726 so go there for the full readme

modifications

  • add filter to be able to neatly know if a URL is embeddable
  • a bit of logging for debug
  • swallow error
  • hacky insta iframe embed fallback don't @ me that it's not oembed :D

Use:

	  {% if embeddable %} {% oembed {{page.your_fun_page_url_attribute_here}} %} {% endif%}

or (note no quotes!!)

	  {% oembed https://www.instagram.com/p/B_-zo6sJpFG/ %}
##
# OEmbed Liquid Tag for Jekyll
# - requires https://github.com/judofyr/ruby-oembed/
#
# Copyright 2011 Tammo van Lessen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
require 'oembed'
require 'uri'
# register all default OEmbed providers
::OEmbed::Providers.register_all()
# since register_all does not register all default providers, we need to do this here. See https://github.com/judofyr/ruby-oembed/issues/18
::OEmbed::Providers.register(::OEmbed::Providers::Slideshare, ::OEmbed::Providers::Yfrog, ::OEmbed::Providers::MlgTv)
::OEmbed::Providers.register_fallback(::OEmbed::ProviderDiscovery, ::OEmbed::Providers::Embedly, ::OEmbed::Providers::OohEmbed)
INSTA_EXP = /^https?:\/\/((m\.)|(www\.))?instagram.com\/p\/([^\/]+)\/.*/
module Jekyll
class OEmbed < Liquid::Tag
def initialize(tag_name, markup, tokens)
super
# parse optional max. width/height parameters
@text, @embed_w, @embed_h = markup.split
@embed_w ||= 500
@text.strip!
end
def render(context)
# pipe param through liquid to make additional replacements possible
url = Liquid::Template.parse(@text).render context
m = INSTA_EXP.match(url)
return "<iframe src=\"https://www.instagram.com/p/#{m[4]}/embed\" width=\"500\" height=\"590\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\"></iframe>" if INSTA_EXP.match?(url)
# oembed look up
# additionally add max. width/height parameters
begin
result = ::OEmbed::Providers.get(url, { :format => :json, :maxwidth => @embed_w, :maxheight => @embed_h })
rescue => e
Jekyll.logger.debug("Oembed issue with #{url}: #{e.inspect}")
return ""
end
# Odd: slideshare uses provider-name instead of provider_name
provider = result.fields['provider_name'] || result.fields['provider-name'] || 'unknown'
"<div class=\"embed #{result.type} #{provider}\">#{result.html}</div>"
end
end
module OEmbeddable
def oembeddable(input)
# pipe param through liquid to make additional replacements possible
url = Liquid::Template.parse(input.strip).render @context
# oembed look up
return true if INSTA_EXP.match?(url)
begin
result = ::OEmbed::Providers.get(url, {:format => :json})
rescue ::OEmbed::Error => e
Jekyll.logger.debug("#{url} not embeddable: #{e.inspect}")
return false
else
return true
end
end
end
end
Liquid::Template.register_tag('oembed', Jekyll::OEmbed)
Liquid::Template.register_filter(Jekyll::OEmbeddable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment