Path: blob/main/_plugins/file_exists.rb
1677 views
module Jekyll1module Tags2##3# This class adds a tag that checks if a file exists.4class FileExistsTag < Liquid::Tag5def initialize(tag_name, path, tokens) # :nodoc:6super7@path = path8end910##11# file_exists - Check if a file exists and return an appropriate boolean12#13# Examples:14#15# {% capture hasfaq %}{% file_exists {{faqpage}} %}{% endcapture %}16def render(context)17# Pipe parameter through Liquid to make additional replacements possible18url = Liquid::Template.parse(@path).render context1920# Adds the site source, so that it also works with a custom one21site_source = context.registers[:site].config['source']22file_path = "#{site_source}/#{url}"2324# Check if file exists (returns true or false)25File.exist?(file_path.strip!).to_s26end27end28end29end3031Liquid::Template.register_tag('file_exists', Jekyll::Tags::FileExistsTag)323334