☰
×
Jonathan ThomBlogProjectsContact

Gem You Should Know About: auto_html

3.13.2017


One of the things I like about Ruby is the tremendous ecosystem that surrounds it. I'm always excited when I find a new gem to incorporate into my apps. A neat one I found recently is auto_html.

Basically, this gem will recognize when there's some html in your text record and apply it. But what I thought was especially cool is that you can enable it look for other things, such as links, markdown, images, and even emoji.

The links part is neat because you don't need to wrap the text in a tags for it to recognize it as a link.

The docs do a good job of describing the set up; it's pretty easy. Instantiate a new Auto_Html instance like so:

markdown = AutoHtml::Markdown.new

Then pass what you want to have the effect into that variable's call method:

@output = markdown.call('### some markdown')

If you want to have multiple wrappers, you can create a pipeline like so:

FORMAT = AutoHtml::Pipeline.new( AutoHtml::Markdown.new, AutoHtml::Link.new )

I did this in my Application Helper, but there are perhaps better locations for it (maybe someone else can tell me). Then I called:

@object_output = FORMAT.call(object.text)

As an added note, I did still have to add 'raw' to the front of my instance variables in the view, i.e. <%= raw @variable %>.

Let me know about some other gems or ways to do this. Or other cool gems in general!

Other Posts

The Best Projects Can Be Done in a Weekend

Everyone Has Something To Offer

Book Thoughts: Capital and Ideology

Naive Diffie-Hellman Implementation in Ruby

PGP/GPG

Lessons from Sandi Metz

When Does the Magic Comment Work, and When Does it Not?

Benchmarking Arrays Full of Nils

Go, and When It's Okay to Learn New Things

Favorite Talks from RailsConf

Grouping Records by Month with Ruby

Front End Refactor

Add Timestamps to Existing Tables in Rails

Let Your Projects Breathe

The Busy and (Somewhat) Fit Developer

TuxedoCSS and the Rails Asset Pipeline

Querying for Today's Date with ActiveRecord

Getting the Action Mailer to Actually Mail (with Mailgun)

Advice for New Epicodus Students