Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I monkey-patch a Jekyll extension or plugin?

I'd like to override a gem method (a Jekyll extension) that looks like this:

File: lib/jekyll-amazon/amazon_tag.rb.

module Jekyll
  module Amazon
    class AmazonTag < Liquid::Tag
      def detail(item)
...
      end
    end
  end
end
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)

I have placed code with the same structure in my project in the folder config/initializers/presentation.rb _plugins/presentation.rb. If I change the name of the method detail to a new name, it works, but I can't get it to override the name detail. What have I done wrong?

(Note: In version 0.2.2 of the jekyll-amazon gem, the detail method is private; I have changed this locally so that the method is no longer private.)

like image 285
Scott C Wilson Avatar asked Jan 10 '17 11:01

Scott C Wilson


People also ask

How do I install plugins in Jekyll?

There are two ways to install Jekyll plugins: This is the easiest method. We simply download a plugin to the _plugins directory on our site. In this example we’ll add a plugin for calculating an estimated read time of a piece of content. First let’s create _plugins/reading_time.rb and adding the following source code (credit zachleat ):

What is the use of @Jekyll_plugins in a Gemfile?

Jekyll gives a special treatment to gems listed as part of the :jekyll_plugins group in a Gemfile. Any gem under this group is loaded at the very beginning of any Jekyll process, irrespective of the --safe CLI flag or entries in the configuration file (s).

Can I use GitHub Pages to publish my Jekyll site?

You can still use GitHub Pages to publish your site, but you’ll need to build the site locally and push the generated files to your GitHub repository instead of the Jekyll source files. You may use any of the aforementioned plugin routes simultaneously in the same site if you so choose.

What is Jekyll-spaceship?

jekyll-spaceship - advanced example. Provides powerful supports for table, mathjax, plantuml, video, etc. A boolean flag that informs Jekyll whether this plugin may be safely executed in an environment where arbitrary code execution is not allowed.


1 Answers

You can use alias_method

module Jekyll
  module Amazon
    class AmazonTag < Liquid::Tag

      alias_method :old_detail, :detail

      def detail(item)
        # do your stuff here
        # eventually pass your stuff to old method
        old_detail(item)
      end

    end
  end
end
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)
like image 97
David Jacquel Avatar answered Dec 02 '22 17:12

David Jacquel