Giter VIP home page Giter VIP logo

embeditor-rails's Introduction

Embeditor

Embeditor is a set of client-side adapters for various embed codes. It aims to keep your article bodies clean while still allowing you to have rich embeds within them.

Installation

It's a Rails engine. Add it to your Gemfile:

gem 'embeditor'

Dependencies

The following dependencies must be included manually. I hope to remove these eventually.

Usage

Note Currently Embeditor (the JS library) exists only in this Rails plugin. Eventually it will be extracted to a pure-JS library and this gem will be a simple wrapper around it.

First, add embeditor and dependencies to your application.js manifest file:

//= require jquery
//= require underscore
//= require embeditor

Next, initialize Embeditor.Base on any page where you want the swapping to occur, and call its swap() function to perform the swapping:

<script type="text/javascript">
    embeditor = new Embeditor.Base()
    embeditor.swap()
</script>

Embeditor works by replacing A tags with a specific class with the appropriate embed. The default class is embed-placeholder, but that can be configured.

There are several adapters included with this engine:

  • Embedly - Covers several services, such as SoundCloud, Spotify, Scribd, Google Maps, and others. See the list of Embedly's providers. Embedly doesn't always work perfectly, so Embeditor provides manual adapters for some of the providers that Embedly claims to support.
  • Cover It Live
  • Polldaddy
  • KPCC's Fire Tracker
  • Instagram
  • Facebook
  • Twitter
  • Storify
  • Brightcove
  • Document Cloud - Not yet supported.
  • Rebel Mouse

None of these are included automatically. To install just the ones you need, add them to your application.js :

//= include embeditor/adapters/embedly
//= include embeditor/adapters/cover_it_live

Or you can add all of them at once:

//= require embeditor/adapters

You can also selectively require templates:

//= require embeditor/templates/cover_it_live

... or require them all at once:

//= require embeditor/templates

Configuration

Embeditor

You can configure:

  • defaultAdapter - The adapter that will be used if no adapter is found for the provided service.
  • defaultService - The service that will be used if no service is provided on the placeholder link.
  • wrapperClass - The class of the div that will get wrapped around the embed.
  • placeholderClass - The class of the <A> tags that Embeditor will look for.

Embeds

Embeditor offers a system of configuration precedence. The order of precedence is:

  1. data-attributes on the placeholder link itself.
  2. Adapter-specific configuration in the Embeditor.Base object.
  3. Global configuration on Embeditor.Base
  4. Adapter-specific defaults.

data-attributes

You may add a data-attribute to any placeholder link, which will be used in the query or embed code. This method of specifying configuration takes precedence over any other configuration. The names of the data-attributes get passed directly to the query or embed code (depending on the adapter). Only the data-attributes which have to do with the embed will be passed through; attributes like data-service won't be used.

In the following example, the maxheight property will be sent to the oembed endpoint:

<a href="http://projects.scpr.org/firetracker/oembed?url=http://projects.scpr.org/firetracker/rim-fire/" data-maxheight="450" class="embed-placeholder" data-service="firetracker">Rim Fire</a>

Adapter-specific configuration

When initializing the global Embeditor.Base object, you may specify configuration for individual adapters.

The display object is for configuring display options. Properties:

  • placement - the method to use for placing the embed, such as before, after, or replace. Default is after.

The query object is for configuring the query parameters, or in the case of an adapter which doesn't use a query, it's for configuring the embed properties.

For the most part, everything will be pretty consistent.

new Embeditor.Base({
    Embedly: {
        query: {
            maxheight: 450
        }
    },
    FireTracker: {
        query: {
            maxheight: 350
        },
        display: {
          placement: 'replaceWith'
        }
    },
    CoverItLive: {
        query: {
            maxheight: 500
        }
    }
})

Global configuration

You may also specify global configuration in Embeditor.Base, which will be used for all adapters:

new Embeditor.Base({
    maxheight: 450,
    maxwidth: 200
})

There are also some Embeditor options you can configure:

  • defaultAdapter - Adapter that gets used when the service isn't recognized. default: Embedly
  • defaultServer - Service that gets used when the data-service attribute is missing. default: other
  • placeholderClass - The class that the embed placeholders are given. default: embed-placeholder
  • wrapperClass - The class the embed's wrapper is given. default: embed-wrapper
  • wrapperElement - The element which should be wrapped around all embeds. default: div
  • defaultPlacement - Default embed placement, if a placement is somehow missing or it doesn't exist in the PlacementFunctions object. default: after

Adapter-specific defaults

Finally, if you're writing an adapter, you should specify a QueryDefaults object on the adapter, which will be used as a fallback for that adapter's parameters:

class Embeditor.Adapters.Twitter extends Embeditor.Adapter
    @QueryDefaults =
        maxheight: 500

    @DisplayDefaults =
        placement: 'before'

    # ...

Embedly

Embedly requires an API key. You can provide it in the query options for the Embedly adapter when initializing Embeditor.Base:

new Embeditor.Base({
    Embedly: {
        query: {
            key: 'YOUR_API_KEY'
        }
    }
})

oEmbed vs. non-oEmbed

This library isn't necessarily tied to oEmbed, however it does have support for it. Even for services which support oEmbed, there are static templates which are able to render the embed properly just based off of the provided URL. This eliminates any oEmbed headaches (Access-Control-Allowed-Origin, for example), and also reduces the time it takes for an embed to load.

CKEditor plugin

An unofficial CKEditor plugin can be found HERE. Copy and paste it into your own repository.

Extending

You can/should add your own adapters! It's easy, I promise. If you add one which you think could be useful to a lot of people, please submit a PR and share the wealth!

You can extend your adapter from:

  • Embeditor.Adapter, which is the base adapter.
  • Embeditor.Adapters.Oembed, for oEmbed endpoints.
  • Embeditor.Adapters.StaticTemplate, for embeds where the embed code is stored in the /templates directory and rendered with JST.

NOTE Any function beginning with an underscore, like _extractData(), should be considered a private function.

Known Issues

  • Cover It Live and Facebook embeds don't currently work together on the same page, due to a javascript conflict between the two.

Contributing

If you have an adapter that you think would be useful for many, please open up a pull request.

embeditor-rails's People

Contributors

bricker avatar nakedsushi avatar sdillingham avatar

Stargazers

Eaton avatar Greg Connour avatar

Watchers

 avatar Andy Beger avatar Chris Keller avatar Ben Hochberg avatar James Cloos avatar Jason avatar Jon White avatar Andy Cheatwood avatar  avatar  avatar Jay Arella avatar Brian Vasallo avatar

embeditor-rails's Issues

License missing from gemspec

RubyGems.org doesn't report a license for your gem. This is because it is not specified in the gemspec of your last release.

via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

Including a license in your gemspec is an easy way for rubygems.org and other tools to check how your gem is licensed. As you can imagine, scanning your repository for a LICENSE file or parsing the README, and then attempting to identify the license or licenses is much more difficult and more error prone. So, even for projects that already specify a license, including a license in your gemspec is a good practice. See, for example, how rubygems.org uses the gemspec to display the rails gem license.

There is even a License Finder gem to help companies/individuals ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec. This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue with a nice message. In either case, I'll follow up. Thanks for your time!

Appendix:

If you need help choosing a license (sorry, I haven't checked your readme or looked for a license file), GitHub has created a license picker tool. Code without a license specified defaults to 'All rights reserved'-- denying others all rights to use of the code.
Here's a list of the license names I've found and their frequencies

p.s. In case you're wondering how I found you and why I made this issue, it's because I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too, and make issues for gemspecs not specifying a license as a public service :). See the previous link or my blog post about this project for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.