Giter Site home page Giter Site logo

voidpart / markdown-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joliss/markdown-rails

0.0 1.0 0.0 154 KB

Markdown as a static templating language for Rails views and partials

License: MIT License

Ruby 93.00% JavaScript 2.82% CoffeeScript 1.01% CSS 3.17%

markdown-rails's Introduction

markdown-rails

Dependency Status

This gem allows you to write static Rails views and partials using the Markdown syntax. No more editing prose in HTML!

Usage

Add the following to your Gemfile:

gem 'markdown-rails'

Now add views or partials ending in .md or .markdown.

Examples

Static View

In app/views/home/about.html.md:

# About This Site

*Markdown code goes here ...*

Keep in mind that unlike static files dropped in public, you still need a matching route, such as get ':action', :controller => :home, to route /about to home#about.

Static Partial

In app/views/posts/edit.html.erb:

<form>... dynamic code goes here ...</form>
<div class="help">
  <%= render :partial => "posts/edit_help" %>
</div>

In app/views/posts/_edit_help.html.md:

## How To Edit

This text is written in **Markdown**. :-)

Note: If you are including Markdown partials from a Haml view, <pre> blocks inside your Markdown may be indented when Haml is not in "ugly" (production) mode, causing leading white-space to appear in development mode. To fix this, set Haml::Template.options[:ugly] = true.

Configuration

By default markdown-rails uses the RDiscount parser. You can change this by calling config.render like so:

MarkdownRails.configure do |config|
  config.render do |markdown_source|
    # Return compiled HTML here ...
  end
end

You might in particular want to use Redcarpet, which allows you to enable various aspects of GitHub Flavored Markdown through its parser options. To do so, add the redcarpet gem to your Gemfile, and add the following into a config/initializers/markdown.rb file:

MarkdownRails.configure do |config|
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
    :fenced_code_blocks => true,
    :autolink => true,
    ... etc ...)
  config.render do |markdown_source|
    markdown.render(markdown_source)
  end
end

Security

Despite Markdown being a static language, you should not use this gem to process untrusted Markdown views (or partials). In other words, do not add Markdown views from a source if you wouldn't trust Erb views from them.

Limitations

  • It's not possible to embed Ruby code in the Markdown code. Unfortunately, you cannot simply chain template handlers (.md.erb) like you can with asset handlers. This is reasonable if you consider that unlike assets, templates are precompiled not into strings but into Ruby code, which is then called every time the template is served. Still, the performance of modern Markdown parsers is good enough that you could afford to reparse the Markdown on every template view, so having Markdown with Erb in it should be possible in principle.

    In the meantime, you can use HAML's :markdown filter to the same effect.

  • The only truly Markdown-specific code in the source is RDiscount.new(markdown_source).to_html and the .md/.markdown file name extensions. This gem can and should be generalized into a general-purpose static template gem, so that you can easily use other static templating languages in Rails. Perhaps tilt will come in useful.

markdown-rails's People

Contributors

afeld avatar bill-transue avatar joliss avatar voidpart avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.