3

I'm using BlueCloth to create html from markdown from the content my users enter into a textarea like this:

def create
  @post = Post.new(params[:post]) do |post|
    body = BlueCloth.new(post.body) 
    post.body = body.to_html
  end

...

end

This works great! I get the html stored in the database fine, But how do I show markdown in the textarea when the user edits? I tried:

def edit
  @post = Post.find(params[:id])
  @post.body = BlueCloth.new(@post.body)
  @post.body.text
end

The output in my textarea looks like:

#<BlueCloth:0x10402d578>
JP Silvashy
  • 46,977
  • 48
  • 149
  • 227

1 Answers1

2

Bluecloth's documentation isn't very well defined. I'm not sure there's an easy way to convert html => markdown.

However, there's nothing stopping you storing the markdown in your database, and convert it to html as necessary.

If you want html to be the default returned by @post.body, then you could always override the accessor.

class Post < ActiveRecord::Base
  ...
  def body
    BlueCloth.new(@body).to_html
  end

  def markdown
    @body
  end
end

Now @post.body returns the html version of the markdown. while @post.markdown returns the markdown source.

EmFi
  • 23,435
  • 3
  • 57
  • 68
  • Yah that's a good point, I may have to do that for now. I was hoping to forgo having to run that method on text every time it renders by just storing the html after it saves. – JP Silvashy Oct 24 '09 at 06:41
  • You raise an interesting point. I've updated my solution with a work around. – EmFi Oct 24 '09 at 06:48
  • 1
    Optionally, you could store the rendered HTML in a separate column in your Post table. – berkes Aug 04 '10 at 19:42
  • 1
    Yeah, I think storing both would probably be your best bet. – docwhat Aug 06 '11 at 01:10
  • @JPSilvashy What is the hesitation with converting from Markdown to HTML every time? Is it performance based? Is this happening for a lot of records and/or requests? We're doing something similar but leaning towards _only_ storing the Markdown in the database and converting it to HTML as needed. Curious what your concern is. Thanks! – Joshua Pinter Apr 29 '22 at 16:34
  • @JoshuaPinter I'm scratching my head trying to even remember what project I was working on all these years later (12.5 years!). I think honestly what you're describing makes more sense to me now, just saving the markdown. – JP Silvashy Apr 29 '22 at 23:13
  • 1
    @JPSilvashy LOL. I didn't even check the date. Man, I know the feeling very well. Sometimes I run across an answer for a problem I'm currently trying to solve and have no idea it's my answer until I look at the author. Ha. Okay, well thanks for the reply and take care! – Joshua Pinter Apr 30 '22 at 19:50
  • Storing the rendered HTML in a the DB is trading processing time (converting markdown to HTML) for disk space in the DB (markdown is significantly more compressed than html, so there may be additional transmission time in the return from the DB). These days, it's largely a wash. The big reason not to store the rendered content in the DB, is that you will have to recalculate it any time the body changes, which may mean coding update/create/delete callbacks to update the post when related objects are modified. Much simpler to just convert to Markdown with the most recent data. – EmFi Jun 14 '22 at 23:13
  • Meant to tag @JoshuaPinter in the previous comment, but missed the edit cutoff. – EmFi Jun 14 '22 at 23:20