Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow default options for Tilt filters #1121

Open
Kallin opened this issue Jan 5, 2023 · 0 comments
Open

allow default options for Tilt filters #1121

Kallin opened this issue Jan 5, 2023 · 0 comments

Comments

@Kallin
Copy link

Kallin commented Jan 5, 2023

I'm working on a Rails app, using the Haml Gem w/ :markdown filter along with RedCarpet. We're having trouble finding a clean way to set options on the Tilt template. When rendering the tiltbase filter in haml, https://github.com/haml/haml/blob/main/lib/haml/filters/tilt_base.rb , the code looks like

      def self.render(name, source, indent_width: 0)
        text = ::Tilt["t.#{name}"].new { source }.render
        return text if indent_width == 0
        if text.frozen?
          text.gsub(/^/, ' ' * indent_width)
        else
          text.gsub!(/^/, ' ' * indent_width)
        end
      end

It would be nice if there were a way to set options, as the initialize method in the Tilt libraries template method, which is what is being instantiated here, does accept an options hash https://github.com/rtomayko/tilt/blob/master/lib/tilt/template.rb#L60 . In our case, it's a redcarpet template we're using, https://github.com/rtomayko/tilt/blob/b6338e5e6d2be7d57f5d6fdfcb8b1161fe222fb3/lib/tilt/redcarpet.rb .

As a workaround, we're doing this in a rails initializer:

module MyCompany
  class MarkdownTemplate < Tilt::RedcarpetTemplate
    def initialize(file = nil, line = 1, options = {}, &block)
      options[:autolink] = true
      options[:hard_wrap] = true
      super
    end
  end
end

Tilt.register(MyCompany::MarkdownTemplate, 'markdown')

This works, but would much prefer to have an option where we could tell Haml to use a set of default options for a given filter. I guess we could also implement our own haml filter, but seems like overkill. A simple way to set default options per haml filter would be nice.

Would love to hear the team's thoughts, and I'm happy to work on a PR if you think this is appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant