Created
July 9, 2013 20:19
-
-
Save delba/5960902 to your computer and use it in GitHub Desktop.
Markdown to HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'markdown' | |
| class Article < ActiveRecord::Base | |
| before_save do | |
| self.html = Markdown.new(content).to_html | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Markdown | |
| attr_reader :text | |
| def initialize(text) | |
| @text = text | |
| end | |
| def to_html | |
| markdown.render(text) | |
| end | |
| private | |
| def markdown | |
| Redcarpet::Markdown.new HTMLWithPants.new(hard_wrap: true), { | |
| no_intra_emphasis: true, | |
| fenced_code_blocks: true, | |
| hard_wrap: true | |
| } | |
| end | |
| end | |
| class HTMLWithPants < Redcarpet::Render::HTML | |
| include Redcarpet::Render::SmartyPants | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment