rails generate scaffold Post title content:text rake db:migrate
# Gemfile ... gem "redcarpet"
# app/helpers/application_helper.rb module ApplicationHelper def markdown(text) renderer = Redcarpet::Render::HTML.new(hard_wrap: true, filter_html: true) options = { autolink: true, no_intra_emphasis: true, fenced_code_blocks: true, lax_html_blocks: true, strikethrough: true, superscript: true, space_after_headers: true } Redcarpet::Markdown.new(renderer, options).render(text).html_safe end end
-# app/views/posts/show.html.haml %h3= @post.title %p= markdown(@post.content) = link_to 'Edit', edit_post_path(@post) \| = link_to 'Back', posts_path
> this is quotes List: - item 1 - item 2 - item 3 [this is link](http://example.com)  ``` class Cat < Animal def say "Meow!" end end ```
# Gemfile ... gem 'markitup-rails'
# app/assets/javascripts/application.js … //= require markitup …
// app/assets/stylesheets/application.css.scss … @import "markitup"; @import "markitup-markdown";
# app/assets/javascripts/posts.js.coffee jQuery -> markdownSettings = { previewParserPath: '/markdown/preview' onShiftEnter: {keepDefault:false, openWith:'\n\n'} markupSet: [ { name:'First Level Heading', key:'1', placeHolder:'Your title here...', closeWith: (markItUp) -> markdownTitle(markItUp, '=') }, { name:'Second Level Heading', key:'2', placeHolder:'Your title here...', closeWith: (markItUp) -> markdownTitle(markItUp, '-') }, {name:'Heading 3', key:'3', openWith:'### ', placeHolder:'Your title here...' } {name:'Heading 4', key:'4', openWith:'#### ', placeHolder:'Your title here...' } {name:'Heading 5', key:'5', openWith:'##### ', placeHolder:'Your title here...' } {name:'Heading 6', key:'6', openWith:'###### ', placeHolder:'Your title here...' } {separator:'---------------' } {name:'Bold', key:'B', openWith:'**', closeWith:'**'} {name:'Italic', key:'I', openWith:'_', closeWith:'_'} {separator:'---------------' } {name:'Bulleted List', openWith:'- ' } {name:'Numeric List', openWith: (markItUp) -> markItUp.line+'. ' } {separator:'---------------' } { name:'Picture', key:'P', replaceWith:'![[![Alternative text]!]]([![Url:!:http://]!] "[![Title]!]")' }, { name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...' }, {separator:'---------------'} {name:'Quotes', openWith:'> '} {name:'Code Block / Code', openWith:'(!(\t|!|`)!)', closeWith:'(!(`)!)'} {separator:'---------------'} {name:'Preview', call:'preview', className:"preview"} ] } markdownTitle = (markItUp, char) -> heading = ''; n = $.trim(markItUp.selection||markItUp.placeHolder).length; for i in [0..n] heading += char '\n'+heading $('#post_content').markItUp(markdownSettings)
# app/assets/javascripts/posts.js.coffee ... previewParserPath: '/markdown/preview'
# app/controllers/markdown_controller.rb class MarkdownController < ApplicationController def preview @text = params[:data] end end
-# app/views/markdown/preview.html.haml = markdown(@text)
# config/routes.rb ... post "markdown/preview"
Source: https://habr.com/ru/post/163947/
All Articles