Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll filter for regex substitution in content?

Is there a Jekyll filter that will replace text using a regular expression (regex) filter?

I believe the "built-in" filter replace does simple string substitution.

like image 659
sameers Avatar asked Sep 12 '14 06:09

sameers


1 Answers

In the event there isn't a (better) solution, I'll throw in the very obviously simple plugin that will do the trick - drop this into your _plugins/ folder as the file regex_filter.rb - it takes the regex as a string, as the first arg, and the replacement as the second arg (for example, {{ page.url | replace_regex: '/$', '' }}:

module Jekyll
  module RegexFilter
    def replace_regex(input, reg_str, repl_str)
      re = Regexp.new reg_str

      # This will be returned
      input.gsub re, repl_str
    end
  end
end

Liquid::Template.register_filter(Jekyll::RegexFilter)
like image 108
sameers Avatar answered Oct 23 '22 17:10

sameers