Heavily inspired by mdeditor
Use the following libraries:
- pagedown as the markdown parser,
- pagedown-extra as the extra plugins for pagedown
- google-code-prettify as the code highlighter.
- use with simple_form
= f.input :content, as: :mdtext_area, input_html: { rows: 10 }
- view helper
= mdtext_area_tag :content, '' , rows: 10
- form builder
= f.mdtext_area :content, rows: 10
rails new myapp, cd myapp
add dependencies to Gemfile
gem 'mdeditor_rails', path: '/Users/nickelchen/work/workspaces/my_gems/mdeditor_rails'
gem 'twitter-bootstrap-rails', '2.1.9'
bundle install
install bootstrap
rails generate bootstrap:install static
install mdeditor_rails, add this to application.js
//= require 'mdeditor_rails'
add this to application.css ( you know the meaning of this code )
*= require mdeditor_rails
generate a scaffold
rails g scaffold Post title:string content:text
modify posts/_form.html.erb from f.text_area
to f.mdtext_area
there you go
Now you can save your markdown-style text in database, after all, you want to format it, display it, use the following javascript api.
MdeditorRails.start_format(container, '#{ j(raw text)}')
container is a jQuery object where you want to hold the formatted text.