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

Support mixed 'strong' and 'italic' form in the Markdown processor #276

Closed
Jiab77 opened this issue May 7, 2019 · 4 comments
Closed

Support mixed 'strong' and 'italic' form in the Markdown processor #276

Jiab77 opened this issue May 7, 2019 · 4 comments

Comments

@Jiab77
Copy link

Jiab77 commented May 7, 2019

Hello and sorry to disturbing.

If I've understood correctly the code from file /lib/markdown.php, to process strong and italic you're counting the recurrence of '_' and '*' characters but you can't mix them, I mean use **_word_** and get <strong><em>word</em></strong> in return.

So is there a way to support the mixed form?

It took me times to understand the regex used in the code with the website regex101 but I'm lacking skills to support the "mixed" way...

@xfra35
Copy link
Member

xfra35 commented May 8, 2019

What you're saying is that, even though the forms ***word*** and ___word___ are supported, the mixed forms *__word__*, **_word_**, _**word**_ and __*word*__ should also be supported. Right?

Then the regex should be modified so that, instead of having \1 matching the first group in direct order, it should match it in reverse order.

Here's a fix proposal:

$str=preg_replace_callback(
    '/(?<=\s|^)(?<!\\\\)([*_])([*_]?)([*_]?)(.*?)(?!\\\\)\3\2\1(?=[\s[:punct:]]|$)/',
    function($expr) {
        if ($expr[3])
            return '<strong><em>'.$expr[4].'</em></strong>';
        if ($expr[2])
            return '<strong>'.$expr[4].'</strong>';
        return '<em>'.$expr[4].'</em>';
    },
    preg_replace(
        '/(?<!\\\\)~~(.*?)(?!\\\\)~~(?=[\s[:punct:]]|$)/',
        '<del>\1</del>',
        $tmp=$str
    )
);

@Jiab77
Copy link
Author

Jiab77 commented May 8, 2019

@xfra35 yes it's exactly the issue I've tried to describe, glad you understood because sometimes I'm not clear enough... 👍 but I can't say "it should be supported" as it's to the devs to decide what should be supported but it might be really useful for those who use a client side library that don't give the possibility to specify what characters to use as 'strong' and 'italic'.

And this still respect the original Markdown spec that I know you devs want's to follow.

@Jiab77
Copy link
Author

Jiab77 commented May 8, 2019

@xfra35 I've tested your regex on regex101, it took me times to understand how it works but it does the job ! 🤘 well done!

@exodus4d
Copy link

exodus4d commented May 10, 2019

Hint: I had some similar issues recently (parsing MarkDown e.g. from GitHub with F3s Markdown parser).
I replaced the parsing function with a API call to GitHubs API: https://developer.github.com/v3/markdown/ . Maybe this API response could also be used in F3 for validation or testing the F3 parser.

The final result can be seen here https://www.pathfinder-w.space/ . Click the version number in the header.

@xfra35 xfra35 closed this as completed in 3c09688 Jun 20, 2019
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

3 participants