0

I am using parsedown and codeigniter. On my textarea if I have added a few empty lines they do not show up in preview. It only adds one \n in preview box

As shown in this image Note: preview is the bottom box in image

enter image description here

As you can see in the top box which is a textarea. There is a large gap just before the last line. It does not show that in preview? If I try and replace the newlines with br or nlbr it effects the indented code on parsedown

Question using codeigniter and parsedown how can I make sure when I echo it from controller that the will show same ammount of lines.

Script

<script type="text/javascript">
$( document ).ready(function() {
    $('#editor').on('paste cut mouseup mousedown mouseout keydown keyup', function(){
        var postData = {
            'html' : $('#editor').val(),
        };
        $.ajax({
            type: "POST",
            url: "<?php echo base_url('example/preview');?>",
            data: postData,
            dataType: 'json',
            success: function(json){
                $('#output').html(json['output']);
            }
        });
    });
});
</script>

Controller

<?php

class Example extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->library('parsedown');
}

public function index()
{
    $this->load->view('example_view');
}

public function preview() {
    $data['success'] = false;
    $data['output'] = '';

    if ($this->input->post('html')) {
        $data['success'] = true;
        // Using br or nlbr effect the code indents so can not use it.
        $data['output'] = str_replace('\n', '<br/>', $this->parsedown->text($this->input->post('html')));
    }

    echo json_encode($data);
}

1 Answers1

0

You need to use double break tags to create a blank line.

ie

Something on this line
<br><br>
This line has a blank line above it.

You can work out what is needed for more than one blank line.

Have a play in http://parsedown.org/demo to test it.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • I don't want to have to put
    for each new line on textarea I want the php end to be able to be able to do it. I have also tried
    in str_replace but not work with parsedown
    –  Nov 25 '16 at 05:22
  • Well when you send it to parsedown, then you translate the \n or \r\n - test for both - and add in
    just like nl2br() does...
    – TimBrownlaw Nov 25 '16 at 05:26
  • That interfere with when ever have pre tags etc will have to find some thing else –  Nov 25 '16 at 05:30
  • Ok, well there will be other options... I'll go and setup this parsedown and take a look. – TimBrownlaw Nov 25 '16 at 05:32