0

In my Rails app, I have a textarea box that I want people to be able to use markdown on. The redcarpet gem works fine for purposes of rendering the markdown, but it appends it after the enclosing span. This screws up my design; the top of the paragraph should appear between the "play" icon and the "pencil" icon (same as the title is in this picture):

enter image description here

Here's the html.erb with the markdown call. As you can see, it's entirely enclosed in a <span> tag:

<p class="user_desc">
  <span class="glyphicon glyphicon-play"></span>&nbsp;&nbsp;
  <span class="glyphicon glyphicon-pencil"></span>
  <span id="user_title_text"><%= markdown(@user.description) %></span>
</p>

Here is how the HTML is rendered via ERb and Redcarpet:

<p class="user_desc">
  <span class="glyphicon glyphicon-play"></span>&nbsp;&nbsp;
  <span class="glyphicon glyphicon-pencil"></span>
  <span id="user_title_text"></span></p>
<p>
  Hi! I started <a href="https://www.startthis.org" rel="nofollow" 
  target="_blank">StartThis.org</a>. My background is in philosophy (Ph.D., 
  Ohio State, 2000). I like ideas and thinking them through. I also like 
  organizing things online, especially educational and reference projects.
</p>
(etc.)

In other words, the markdown method is called (and I expected it to be interpolated in) span#user_title_text, it was actually appended after the span.

What's the best way to fix this? I hunted for similar problems but couldn't find any. I guess I could prepare the HTML in a string in a helper method and pass it separately to the view (maybe? Nope, I just tried it and it has the same effect!), but what's the best practice/solution to this? Sorry if this is kind of obvious, I'm a noob to all this.

Thinking a little bit more, considering that preparing the parsed markdown in advance, in the controller, has the same effect, maybe the problem is with how rails/ERb does the interpolation?

TIA

UPDATE: Note the <p> in the above code. This surrounded a span which surrounded the markdown call...which generates <p> tags!

Also note the &nbsp;&nbsp; in the code above. After fixing the <p> tags (changed them to <div>s), thanks to Matt's advice, I tried removing the html entities...and then it was fixed. Why?!

TIA again.

globewalldesk
  • 544
  • 1
  • 3
  • 18

1 Answers1

4

Are you looking at the rendered source in the browser’s inspector?

What I think is happening is the HTML is being rendered as you would expect, resulting in something like:

<p class="user_desc">
  <span class="glyphicon glyphicon-play"></span>&nbsp;&nbsp;
  <span class="glyphicon glyphicon-pencil"></span>
  <span id="user_title_text"><p>Hello, here is some markdown.</p>

<p>Here is another paragraph.</p>
</span>
</p>

However this is not valid HTML, as it contains a <p> inside another <p>, so the browser is correcting it when it parses it. It does this by adding any needed close tags before the next opening <p> tag, and the result is what you see in the browser.

To fix it you will need to ensure your template will result in valid HTML whatever markdown is entered by the user. In particular here you need to change the surrounding p, and also (since p is not allowed as a child of span) you will need to change the last <span>. Replacing them both with divs might work for you:

<div class="user_desc">
  <span class="glyphicon glyphicon-play"></span>&nbsp;&nbsp;
  <span class="glyphicon glyphicon-pencil"></span>
  <div id="user_title_text"><%= markdown(@user.description) %></div>
</div>

Since this produces p elements that are children of divs, they remain valid and are not changed by the browser.

matt
  • 78,533
  • 8
  • 163
  • 197
  • With your help, I got it fixed! Yes, I was looking at the rendered source in the browser's inspector, which I now realize is less useful than the source. Thanks for the pointers re including

    in

    and in . So, I fixed that stuff up. But it still wasn't working--exact same problem. I couldn't understand why. Then I noticed the two ` ` html entities and on a hunch, I tried removing them. That fixed it!

    – globewalldesk Nov 02 '17 at 00:14
  • Fixing the `

    `s was definitely necessary (I tested that separately). But why on earth could those html entities also be part of the problem?! I'll add more details to my original question, and if you could explain in your answer I'll definitely check as the correct one.

    – globewalldesk Nov 02 '17 at 00:24