parsedown: Extra empty lines not showing up in preview using parsedown and codeigniter

Previewing parsedown not displaying correct newlines in preview

demo

With <br/> manually entered texarea I do not want to have to enter <br/> all the time if need to have extra lines

demo1

Question: How can I get it to show correct lines amount of lines that are in the textarea to preview?

Controller How I load the parsedown.php

if I replace \n with <br> it will effect the code indents I have tried nl2br() also

<?php

class Example extends CI_Controller {

	public function __construct() {
		parent::__construct();
                // application > libraries > Parsedown.php
		$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')) {
			
			$string = str_replace('\n', '\r\n', $this->input->post('html'));
			
			$data['success'] = true;
			$data['output'] = $this->parsedown->text($string);
			
		}

		echo json_encode($data);
	}
}

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>

it’s one of the best markdowns this is the only thing I can that need to fix.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 34 (9 by maintainers)

Most upvoted comments

@erusev Alright, no worries 👍

@wolfgang1983 I’ve created a seperate branch with setLiteralBreaks added in there – so just grab Parsedown.php out of there to apply the patch, and use ->setLiteralBreaks(true) to enable it.

Branch: https://github.com/aidantwoods/parsedown/tree/patch-6

Diff: https://github.com/aidantwoods/parsedown/commit/fd681ee683a1e17f0e2afd75909d7dabf453a1b7#diff-ed0d3da57330712681e64f838087ea47

Seems this is an issue with Parsedown (rather than this particular extension).

setLiteralBreaks(true) will prevent a (blank) line from interrupting a block structure. So the extension will cause Parsedown to see something like

1. list item
2. list item


```
code
```

as

1. list item
2. list item
```
code
```

Parsedown parses the former (correctly) as

<ol>
<li>list item</li>
<li>list item</li>
</ol>
<pre><code>code</code></pre>

But fails when parsing the latter by neglecting to honour list content indentation rules specified in commonmark, and so the following is produced for the latter:

<ol>
<li>list item</li>
<li>list item
<pre><code>code</code></pre></li>
</ol>

The correct result for a commonmark compliant parser for the latter markdown text can be found here: http://spec.commonmark.org/dingus/?text=1. list item 2. list item ``` code ```

and is as follows:

<ol>
<li>list item</li>
<li>list item</li>
</ol>
<pre><code>code
</code></pre>

Probably the best thing to do is open another issue with Parsedown about code blocks not being able to interrupt a list block based on indentation. Once Parsedown can do this then you’ll be able to start a new code block after a list with setLiteralBreaks enabled.

Thanks once again

@wolfgang1983 Just updated the branch (diff: https://github.com/aidantwoods/parsedown/commit/539f152bab5e34551263bc8585f80d93e3ba08a9)

Moved the code that creates text on a blank line to before the continuable/completable checks are made (which rely on the first char in the line).

By the way, because setLiteralBreaks essentially just removes the ability for a blank line to interrupt a paragraph – you won’t be able to start an unmarked code block (i.e. from your example '••••<div></div>' (‘•’ in place of a space here)) without further modification. Using a fenced block will work though.

@aidantwoods I have just started testing it getting code from database with new lines ->setLiteralBreaks(true) but getting it from database now throws error. Was working fine with jquery preview but now have to get from db but throws error when enable ->setLiteralBreaks(true)


A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: libraries/Parsedown.php

Line Number: 636

Backtrace:

File: C:\xampp\htdocs\myproject\application\libraries\Parsedown.php
Line: 636
Function: _error_handler

File: C:\xampp\htdocs\myproject\application\libraries\Parsedown.php
Line: 177
Function: blockQuoteContinue

File: C:\xampp\htdocs\myproject\application\libraries\Parsedown.php
Line: 39
Function: lines

File: C:\xampp\htdocs\myproject\application\modules\catalog\controllers\qna\Thread.php
Line: 94
Function: text

File: C:\xampp\htdocs\myproject\application\modules\catalog\controllers\qna\Thread.php
Line: 53
Function: get_form

File: C:\xampp\htdocs\myproject\index.php
Line: 315
Function: require_once

@aidantwoods works fine now thanks

He’s not talking about code blocks @aidantwoods, he wants something similar to nl2br() 😉 But we should fix this CommonMark incompatibility 😄

@wolfgang1983: Markdown works with paragraphs, not with line breaks. You’ll have to explicitly tell Parsedown to create a “empty” paragraph. There are many ways to accomplish this, here are some examples:

paragraph 1

<br />

paragraph 2
paragraph 1

&nbsp;

paragraph 2

The following is the most syntactically correct, but depending on your website’s stylesheet it may not be visible:

paragraph 1

<p></p>

paragraph 2

Edit: For readability, this is not the bug being reported here

Just to clarify a little, is this the bug? (i.e. Parsedown stripping line breaks inside a code block)

screen shot 2016-11-25 at 14 15 35

With this being the expected output? screen shot 2016-11-25 at 14 15 39