3

I am using a RichText field type of my site content and I am inserting blocks of code within some <pre> tags which I add in the HTML view of the editor, however when I leave the HTML view, all of the empty lines within the <pre> block are stripped out.

I've tried turning off the clean up HTML option etc with no luck. Is there a way to keep the empty lines (multiple consecutive newlines).

  • I don't know what they mean with this bug-fix description in their 10.0.6 release notes: "'Pre'-wrapped HTML code of a webpage being rendered improperly", but maybe your problem has something to do with it? (Craft does not yet use this version.) – carlcs Jan 22 '15 at 11:28
  • Marty, do the already get removed after switching back and forth between HTML and WYSIWYG mode? Or does this happen on entry save? – carlcs Jan 22 '15 at 11:36
  • @carlcs When toggling back and forth between views. It remains OK on saving the entry. – Marty Jan 22 '15 at 11:37
  • Ignore my first comment, tested the sample editor on imperavi.com/redactor again and it shows your described behavior. If I then add the empty lines in WYSIWYG mode the HTML gets messed up with span tags <span class="redactor-invisible-space">!! – carlcs Jan 22 '15 at 11:46
4

Don't use Redactor / a Rich Text field for code blocks is probably the only answer one can give right now.

Instead you could use a Plain Text field for your article and get the formatting done with markdown. Or you could use Matrix and keep using Redactor for the text parts of your article and have a separate block type for code.

| improve this answer | |
  • I prefer the markdown-enabled freetext field vs RichText anyway, good suggestion. I'd like to see the original problem solved down the track, though. – Marty Jan 23 '15 at 2:08
2

Im not sure if this is what you mean but you must encode the html you put inside the tag <pre>.

A browser always render html code, so the only way is to encode the html to make it render as text.

for example for <p> you have to write &lt;p&gt;

| improve this answer | |
  • Of course, in fact the editor already does this for you. What I mean is that if I paste in some code that contains empty lines, they are stripped out. – Marty Jan 22 '15 at 11:20
  • oh I understand now, hmmm Im not sure at moment you can do what you want with Redactor. I agree with carlcs about his suggestion, then it depends if you have to use only for yourself or give it to users and how much is important have more consecutive empty lines. – Fabio Carriere Jan 22 '15 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.