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

Shortcodes break markdown code #3

Closed
seb-vial opened this issue Feb 4, 2016 · 12 comments
Closed

Shortcodes break markdown code #3

seb-vial opened this issue Feb 4, 2016 · 12 comments
Labels

Comments

@seb-vial
Copy link

seb-vial commented Feb 4, 2016

Hello,
Since the last version, you can't use shortcodes around markdown :(

For exemple I often use [center] around images like this [center]![My Image](image_src.jpeg)[/center]. With the last release, this code doesn't work anymore, it just shows ![My Image](image_src.jpeg) as plain text but centered though...

Other example
[size=10]_some text_[/size] renders _some text_ with size 10 but not italic... So the shortcode is rendered but not the markdown...

@rhukster
Copy link
Member

rhukster commented Feb 4, 2016

Hmm, this is because of some 'smarter' regex that has been added to the plugin that is intended to address the issue with markdown and shortcodes mixing. This should only effect block-level items like [center] because that needs to render a <div>. However, the easy fix is to do this:

[center]
![My Image](image_src.jpeg)
[/center]

i.e. put the shortcode tags on their own line.

It should work fine for inline items like [size], so that's a bit weird. I will double check that today.

@rhukster rhukster added the bug label Feb 4, 2016
@rhukster
Copy link
Member

rhukster commented Feb 4, 2016

Seems ok to me. Which version are you using?

shortcode ui grav 2016-02-04 14-54-41

What parser are you using?

@seb-vial
Copy link
Author

seb-vial commented Feb 5, 2016

Well for [center] Indeed if I put each tag on a line it works.

For [size] I have to do the same for it to work as well

[size=10]_[Test](http://test.com)_[/size] renders _[Test](http://test.com)_ in size 10 but the markdown in not parsed...
If i do

[size=10]
_[Test](http://test.com)_
[/size]

It works fine

I'm using the last version

@rhukster
Copy link
Member

rhukster commented Feb 5, 2016

what is your config for this page? do you have twig enabled? how about twig_first? you didn't answer my question about the parser either.

@seb-vial
Copy link
Author

seb-vial commented Feb 5, 2016

Sorry about the parser, it's the default one (Wordpress).

I don't use a particular config, it's just a blog post, twig is not enabled, that's just markdown

@rhukster
Copy link
Member

rhukster commented Feb 5, 2016

I'm unable to recreate, so maybe its one of the many changes i have in the core. I'm hoping to get a release out today.

@rhukster
Copy link
Member

Please try again with latest Grav + Shortcode plugin. Shortcode 2.0 is a big revamp and requires the latest Grav release.

@seb-vial
Copy link
Author

Well, It works for simple text, ie: [size=10]my texte[/size], but if I use underscore or/and link, I have to create a new bloc.

[size=10]
_[test](http://test.com)_
[/size]

[size=14]
_italic text_
[/size]

@rhukster
Copy link
Member

Ok, here's the problem and i'm not sure if I can fix it, but i have a few simple workarounds.

  1. We're mixing markdown and shortcodes here. Markdown is processed first, then the shortcodes are processed. This is because shortcodes can do a lot more complex stuff like moving/removing sections of content, so it's imperative that markdown is already processed first, and shortcodes operate on the resulting HTML.

  2. By default, markdown will wrap inline level items in <p> tags, but not block level items. This is defined in markdown itself to ensure things like blockquote, lists, tables, etc don't have invalid <p> tags around them.

  3. In this shortcode core plugin, I define that any line that starts with a [ and ends with a ] character and contains a valid shortcode name is a block-level shortcode item. This ensures that shortcodes like [section], [center] or [tabs] or whatever you want can be on a new line so the resulting output is not wrapped in those <p> tags. [center]blah[/center] shortcode for example outputs <div style="text-align: center;">blah</div>. This would be invalid if it had an extraneous <p> tag around it.

https://github.com/getgrav/grav-plugin-shortcode-core/blob/develop/classes/ShortcodeManager.php#L184-L199

  1. These block-level items are skipped by Markdown, so things in them are not processed.

This is what you are seeing. You are putting [size=10]_[Test](http://test.com)_[/size] on a new line, so the [ is found as the first character , and ] the last character on the line, so is being treated as a block-level item, and therefore markdown is no processed. That is why breaking it out onto a new line 'does' work.

However, it will work as long as the [ is not the first character. So options are:

  1. Simply adding a space before will work
  2. Having text after the last closing tag will work
  3. Text before the opening tag will work
  4. Breaking the markdown onto a newline will work too:

The first four options will work, the last one will not for the reasons provided above.

 [size=10]_[Test](http://test.com)_[/size]

[size=10]_[Test](http://test.com)_[/size] will work because there is text after

Text before makes it work [size=10]_[Test](http://test.com)_[/size]

[size=10]
_[Test](http://test.com)_
[/size]

[size=10]_[Test](http://test.com)_[/size]

Anyway, I need to think if this is really solvable, but frankly this is a bit of a corner case. The first two options are simple workarounds though.

@rhukster
Copy link
Member

I think I have a fix!!!! see the commit above.. Just a change in the Regex to make it smarter. Give it a try.

@rhukster
Copy link
Member

Ok release with this fix is out.

@seb-vial
Copy link
Author

Wow you've really worked on that, thanks !
I've updated to the latest release and... it works as a charm ! Even [center] shortcode works.

Thanks a lot and really nice work !

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

No branches or pull requests

2 participants