genert / bbcode

BBCode parser with bidirectional conversion.
http://genert.org/
MIT License
80 stars 27 forks source link

Lists: [ul][li]...[/li][/ul] or [LIST][*]...[/*][/LIST] #2

Closed FilipQL closed 6 years ago

FilipQL commented 6 years ago

Hi,

Currently, Genert/bbcode can parse the following lists:

[list]
[*]List item 1
[*]List item 2
[*]List item 3
[/list]

I am using SCEditor which generates the following lists:

[ul]
[li]List item 1[/li]
[li]List item 2[/li]
[li]List item 3[/li]
[/ul]

... so I've added the following parsers:

        $bbCode->addParser(
            'bullet_list',
            '/\[ul\](.*?)\[\/ul\]/s',
            '<ul>$1</ul>',
            '$1'
        );

        $bbCode->addParser(
            'list_item',
            '/\[li\](.*?)\[\/li\]/',
            '<li>$1</li>',
            '$1'
        );

This works, but the problem is that is also "translates" new lines to <br> so the result is the following:

<ul>
    <br>
    <li>List item 1</li>
    <br>
    <li>List item 2</li>
    <br>
    <li>List item 3</li>
    <br>
</ul>

I've tried to modify that regex (/\[li\](.*?)\[\/li\]/) to include that new line so that the <br> tag will not be generated, but without success. Do you know how to solve this?

anasbud commented 6 years ago

Same problem. Any solution ?

genert commented 6 years ago

@FilipQL @anasbud

You could have used except to disable newline parsing, like this:

$bbCode->except('linebreak')->convertToHtml('[b]Bold[/b] [i]italic[/i]');

However with further thoughts, decided that 1.1.0 release does not include linebreak parser by default for BBCode although it can be added by calling function addLinebreakParser if needed.

This solves this issue by keeping parser minimal. You can check out the test where your issue was used: https://github.com/Genert/bbcode/blob/master/tests/ParserTest.php#L166