Bitwise
Bitwise

Reputation: 8461

Support Markdown editing within rails app

I allow my users to create assignments and distribute them to their employees. Assignments have a attribute called description. Instead of users filling out a plain text field to create a description I want to give them a editable markdown supported box to fill in. exactly like the one I'm filling out now. I've never built anything like this out before, but I'm wondering if there is a Ruby gem or plugin that will help me out with this?

Upvotes: 0

Views: 1322

Answers (2)

Alex Kojin
Alex Kojin

Reputation: 5204

I can recommend you SimpleMDE javascript markdown editor. In this case you don't need a ruby gem for markdown because SimpleMDE can generate html version for you. You need to just save in your database both versions - markdown and html.

We have table assignments with two fields description to keep a markdown version and description_html to keep a html version of a question. As far you create common rails assignments' form and bind simplemde instance to description textarea. For description_html create hidden field tag:

<%= f.hidden_field :description_html %>
<%= f.textarea :description %>

On any change simplemde will save a html version to description_html hidden field:

var simplemde = new SimpleMDE({ element: $("#MyID")[0] });
  simplemde.codemirror.on("change", function(){
  # set a html to a hidden field 
  $('#description_html_id_CHANGE_IT').val(simplemde.getHtmlValue());
});

Upvotes: 1

yoones
yoones

Reputation: 2474

I suggest you use github's gem for that: https://github.com/github/markup

First install a couple of gems:

  • github/markup
  • html-pipeline
  • nokogiri
  • nokogiri-diff

Then try the following:

require "github/markup"
require 'html/pipeline'
require 'nokogiri'
require 'nokogiri/diff'

filename = ARGV.first
puts GitHub::Markup.render(filename, File.read(filename)).strip.force_encoding("utf-8")

Upvotes: 1

Related Questions