Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tasmanwebsolutions opened this issue Nov 25, 2016 · 34 comments

Comments

@tasmanwebsolutions
Copy link

tasmanwebsolutions commented Nov 25, 2016

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.

@aidantwoods
Copy link
Collaborator

aidantwoods commented Nov 25, 2016

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

@tasmanwebsolutions
Copy link
Author

Yes. The second image is correct but also not using code format.

@PhrozenByte
Copy link
Contributor

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

@tasmanwebsolutions
Copy link
Author

tasmanwebsolutions commented Nov 25, 2016

@PhrozenByte I tried using nl2br but when it does it effect the code indent. Codeigniter


<?php

class Example extends CI_Controller {

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

	public function index()
	{
		// $Parsedown = new Parsedown();
		// $Parsedown->setBreaksEnabled(true);
		// echo $Parsedown->text('Hello _Parsedown_!    ss');

		$this->load->view('example_view');
	}

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

		if ($this->input->post('html')) {
			$data['success'] = true;
			
			$data['output'] = $this->parsedown->text(str_replace('\n', '<p></p>', $this->input->post('html')));
		}

		echo json_encode($data);
	}
}

@PhrozenByte
Copy link
Contributor

You are not supposed to use nl2br() 😆 This was just to explain @aidantwoods what you're trying to achieve 😉

@aidantwoods
Copy link
Collaborator

Issue #448 (comment) may help RE inserting lines within normal text, if you really can't use HTML to do it.

Also, for the literal '—' character inside ` you'll need to use the actual character. But if just in regular text, Parsedown should cope fine with that HTML entity encoding you're using (I.e. It will work if not within backticks)

@aidantwoods
Copy link
Collaborator

cc @erusev shall I make a PR with something similar to the diff linked to in the above comment? (similar: can probably implement a bit cleaner). I guess this is a feature that a few people would find useful (even though it will break a few commonmark tests if enabled).

@erusev
Copy link
Owner

erusev commented Nov 25, 2016

Is this about code blocks or not? I'm confused :/

Parsedown has an option called setBreaksEnabled that could probably help if the issue is about regular text as opposed to code blocks.

@aidantwoods
Copy link
Collaborator

@erusev I think it's about regular text (though I guess the code blocks is a separate issue). I think setBreaksEnabled only copes with adding a single line break (as far as I can tell with testing). The diff should honour them all. (is this a bug in setBreaksEnabled?)

@tasmanwebsolutions
Copy link
Author

Have updated question/issue with image

@tasmanwebsolutions tasmanwebsolutions changed the title The correct ammont of new lines not showing in preview. Extra empty lines not showing up in preview using parsedown and codeigniter Nov 25, 2016
@PhrozenByte
Copy link
Contributor

As said, this is expected behavior, it's consistent to both the original Markdown description and the CommonMark specs. @wolfgang1983, you'll either have to extend Parsedown (see wiki) or use one of the workarounds described in #450 (comment).

@erusev
Copy link
Owner

erusev commented Nov 25, 2016

With setBreaksEnabled set to true, new lines should get converted to <br> and if I understand correctly, this is what the issue is about.

@PhrozenByte
Copy link
Contributor

PhrozenByte commented Nov 26, 2016

@erusev: I always thought that setBreaksEnabled(true) is about following GitHub's line break principles. So

This is a longer text with
a custom line break

gets


This is a longer text with
a custom line break


Normally Parsedown (and most other Markdown parsers; this btw complies to both the original Markdown description and the CommonMark specs) would parse this to (i.e. no line break)


This is a longer text with a custom line break


This doesn't affect multiple empty lines, those are still parsed as two paragraphs (and not three). So

This is a longer text with



three empty lines

gets


This is a longer text with

three empty lines


with both GitHub flawored Markdown and Parsedown, no matter setBreaksEnabled() is set to true or not. This is IMO the expected behavior, as it matches GitHub flavored markdown and other Markdown parsers with similar behavior to setBreaksEnabled(true).

@wolfgang1983 in contrast is trying to achieve something like this with the previous example:


This is a longer text with

three empty lines


@erusev
Copy link
Owner

erusev commented Nov 26, 2016

@PhrozenByte

Right, I believe you are correct, setBreaksEnabled won't be able to resolve this issue as I hoped.

@aidantwoods
Copy link
Collaborator

@erusev is it worth adding in another built in setter for literal line breaks (similar (but possibly cleaner than) this patch I threw together #448 (comment))? Given this issue (and the one opened in the linked comment) are both requesting the same functionality I'd guess it might be a useful addon for at least a few people.

Also, since lines handler is really the gating function here (responsible for losing the excess line breaks before setBreaksEnabled can see them), I can't really see this being an easy thing to write an extension for (without just overwriting the entire lines function with almost identical functionality). Having extensions completely replace that core function is probably quite likely to break things across updates too IMO.

@PhrozenByte
Copy link
Contributor

PhrozenByte commented Nov 28, 2016

@aidantwoods: Just my 2 cents: Sure, why not (with another option like your suggested setLiteralBreaks()), however I personally don't like this nl2br()-like behavior... This produces semantically incorrect results. The example Markdown

paragraph 1



paragraph 2

should result in the HTML

<p>paragraph 1</p>
<p></p>
<p>paragraph 2</p>

when this new option setLiteralBreaks() is set to true (what should also imply setBreaksEnabled(true)).

@tasmanwebsolutions
Copy link
Author

@aidantwoods on my code a where would place it I did a test tun but nothing happened.

@erusev
Copy link
Owner

erusev commented Nov 28, 2016

@aidantwoods This seems like a feature that should rather be built is an extension, I appreciate the offer, though.

@aidantwoods
Copy link
Collaborator

@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: aidantwoods@fd681ee#diff-ed0d3da57330712681e64f838087ea47

@tasmanwebsolutions
Copy link
Author

@aidantwoods will give it ago. I like this parsedown.php my fav so far.

@tasmanwebsolutions
Copy link
Author

@aidantwoods works fine now thanks

@tasmanwebsolutions
Copy link
Author

tasmanwebsolutions commented Dec 16, 2016

@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
Copy link
Collaborator

aidantwoods commented Dec 16, 2016

@wolfgang1983 Have you got some test data you could give me that replicates the issue so I can track it down?

@tasmanwebsolutions
Copy link
Author

tasmanwebsolutions commented Dec 16, 2016

@aidantwoods I use codeigniter I have put the parsedown.php in my libraries folder here is my controller I have loaded the library in the __construct are and on the get_form is where I use ->text() http://pastebin.com/raw/HaQ3eBR9 I just seems $Parsedown->setLiteralBreaks(true); if I have text and then code under it will throw error. Message: Uninitialized string offset: 0

@aidantwoods
Copy link
Collaborator

@wolfgang1983 have you got any of the actual text that caused the error?

(i.e. something that would be returned by $post_tmp_info['message'] – which is given to $Parsedown->text() in your code)

@tasmanwebsolutions
Copy link
Author

tasmanwebsolutions commented Dec 19, 2016

@aidantwoods I tried it just like this way it does not seem which is similar to text I would get from database but sill produces error I just put \n\n for new lines same as me pressing enter for new line in text area as testing but still error when set this to $Parsedown->setLiteralBreaks(true);

$hello = '> hello' . "\n\n" . '    <div></div>';

$Parsedown = new Parsedown();
$Parsedown->setLiteralBreaks(true);
echo  $Parsedown->text($hello);

And throws same error.


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\Newthread.php
Line: 27
Function: text

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

@aidantwoods
Copy link
Collaborator

aidantwoods commented Dec 19, 2016

@wolfgang1983 Just updated the branch (diff: aidantwoods@539f152)

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.

@tasmanwebsolutions
Copy link
Author

tasmanwebsolutions commented Jan 13, 2017

@aidantwoods Hi again, I Know this is closed but small bug in your add on https://github.com/aidantwoods/parsedown When I have enabled setLiteralBreaks(true) for some reason it add br to every line it should only be for the extra spaces I create. Because now it effects the code indents and removes pre tags for code

link

@aidantwoods
Copy link
Collaborator

Is this happening with the markerless code block? (I.e. Text that is indented >= 4 times to become code?)

@tasmanwebsolutions
Copy link
Author

@aidantwoods when I have my code intended 4 spaces and have setLiteralBreaks(true) in preview as showing in image for some reason removes pre and code tags. What I would of liked is only when I click enter on a line does it add newline only for that line i use $Parsedown->text()

@aidantwoods
Copy link
Collaborator

Yeah, that's expected behaviour really – as said in #450 (comment).

You'd need to use ```, or ~~~ to start and end a marked code block.

Per commonmark spec: an unmarked code block cannot interrupt a paragraph (but a line containing only whitespace can). (An unmarked code block is one that is indented, and contains no (non-whitespace) 'markers' to delimit the code).

->setLiteralBreaks(true) prevents a new line interrupting a paragraph (so no newlines can interrupt the paragraph before your unmarked code block starts – so it cannot start).

You can use a marked code block to enclose code, the only other solution would be to have ->setBreaksEnabled enable some logic that allows an unmarked code block to interrupt a paragraph (possibly based on some criteria – like the last line was blank).

@tasmanwebsolutions
Copy link
Author

tasmanwebsolutions commented Jan 13, 2017

Thanks once again

@tasmanwebsolutions
Copy link
Author

Hi, @aidantwoods I have noticed a small issue with the code you gave me with lists the if press enter after the last list number or bulleted one it should clear it but for some reason when ever I start a new line after the last list item it still thinks its in list. How to solve this

@aidantwoods
Copy link
Collaborator

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.%20list%20item%0A2.%20list%20item%0A%60%60%60%0Acode%0A%60%60%60

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants