Mustdown provides helpers to ease the use of markdown, mustache and both of them.
Add the gem to your Gemfile
:
gem 'mustdown'
Then call bundle install
to install it for your application.
This will render the given text through Markdown.
<%= markdown "# Hello World" %>
Output:
<h1>Hello World</h1>
The mustache
helper renders a mustache template with the given binding object.
<%= mustache "Hello {{name}}", name: 'John' %>
Output:
Hello John
This helper is more complex since it provides the two previous helpers in one.
<%= mustdown "# {{title}}", title: 'Hello World' %>
Output:
<h1>Hello World</h1>
If you're not using Rails, it's possible to use the Mustdown module directly.
This will render the given text through Markdown.
Mustdown.markdown("# Hello World")
# => "<h1>Hello World</h1>"
The mustache
method renders a mustache template with the given binding object.
Mustdown.mustache("Hello {{name}}", name: 'John')
# => "Hello John"
This method is more complex since it provides the two previous methods in one.
Mustdown.mustdown("# {{title}}", title: 'Hello World')
# => "<h1>Hello World</h1>"
You can generate a default initializer by calling:
bundle exec rails generate mustdown:install
The markdown configuration is provided by Redcarpet. All Redcarpet options are available.
The markdown
and mustdown
helpers accept additional parameters for markdown
configuration. The settings you pass are merged with the default ones and take
precedence.
<%= markdown "**Hello World**", { autolink: false }, { hard_wrap: true} %>
A very useful technique is to use templates from I18n.
Let's take an example. Given the following models in your app:
Define the following in config/locales/en.yml
:
en:
companies:
show:
text: |
# {{name}}
{{name}} is a great company ! Here are some of their projects:
{{#projects}}
* [{{title}}]({{url}})
{{/projects}}
Now we can use it in our show template (located in
app/views/companies/show.html.erb
):
<%= mustdown t('.text'), @company %>
Output:
<h1>Github</h1>
<p>Github is a great company ! Here are some of their projects:</p>
<ul>
<li><a href="https://github.com/github/hubot">Hubot</a></li>
<li><a href="https://github.com/github/gollum">Gollum</a></li>
</ul>
It is possible to use Mustdown when composing your emails but to do so you need to reference it as a helper
class MyAwesomeMailer < ActionMailer::Base
helper :'mustdown/mustdown'
# ...
end
If you try to use Mustdown with Cells, you need to add it as a helper like this:
class HomeListingsCell < Cell::Rails
helper :'mustdown/mustdown'
# ...
end