Relative urls / any default filters in custom plugins

Is it possible to use default filters as functions in the plugins I write myself? E.g. I could really use some lk = Jekyll::relative_url(context.link) or `lk = “{{ #{context.link} | relative_url }}”

Yes, you can use Jekyll’s filters in your plugins.
But there is a certain way to go by first:

All the filters are actually defined inside modules. These modules can be “included” inside a class to use the filter-methods inside your plugin.

For example, the relative-url is defined inside the module Jekyll::Filters::URLFilters. So my plugin would include the module and use it like so:

module MyPlugin
  class Tag < Liquid::Tag

    include Jekyll::Filters::URLFilters

    def initialize(tag_name, markup, tokens)
      super
      @markup = markup
    end

    def render(context)
      @context = context
      url = relative_url(@markup)

      # do something with the variable url
    end
  end
end
1 Like

Cool! :slight_smile:

For ease of access of future generations, the list of available filters can be found here:
https://www.rubydoc.info/gems/jekyll/Jekyll/Filters

1 Like