-
Notifications
You must be signed in to change notification settings - Fork 824
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
Replace ShortcodeParser with external library #5535
Comments
quote @sminnee
I would actually like to have the shortcodes registered via the yml file eventually. That would save a lot I think. Any pros/cons? |
It makes sense, but I'd wait until we have found the right shortcode library to see what it expects. |
|
(The initial use case, for those interested, was so a shortcode could insert a This situation is covered in |
From the looks of it, most frameworks actually implement a parser themselves, indeed thunderer seems the only viable option. Considering how much it's used, there are surprisingly few libraries available. |
Having looked into this more, I have mixed feelings.
I wonder if it's actually better just to leave the shortcode handler as-is for now? In providing an HTML-aware shortcode handler, the current system does provide a unique service. |
Yep, I've not yet found a good alternative either. Although looking at the BBCode parser, I'm getting slightly twitchy. |
As an aside, these seem to be the open bugs in the shortcode parser:
And these seem to be bugs to do with how the shortcode system is being used (which pulling in a new handler wouldn't fix)
I believe that this is a bug in a particular shortcode handler: |
Maybe our efforts are actually better spend fixing those bugs? ;-) |
Possibly/probably :) |
Based on the previous discussion, do you think we should close this one, Simon? |
Can't we use https://github.com/thunderer/Shortcode and then create a custom "HTML aware" processor? |
The issue is that we'd wind up rewriting the internals of Thunderer so fundamentally that I'm not sure they'd accept a PR. As an alternative, it might be worth spinning out our shortcode system as a separate micropackage, so as to decouple it. |
Looking at https://github.com/thunderer/Shortcode#processing - all the context is provided so why can't we write a I am taking a very quick look at the docs, of course, without digging so I may have missed someting - but it looks like we won't get any less context than we currently do and we can write/extend a parser to behave as we need. Sorry if I've missed something obvious or I'm assuming a leap in functionality that isn't there. |
I think, at the moment, it's too much effort to try and write a wrapper around an external shortcode parser, while what's already in core, is sufficient for now. |
That's a shame - I think it'd be great to pull out what we can, even if it means supporting a 3rd party library. that parser looks pretty well architected and the kind of thing that would work well for us. |
Agreed, but it is the only one that actually seems to be somewhat maintained. And, as @sminnee said, maintained by 1 person, not a community. |
+1 on moving |
I'd suggest we quickly shift BBCodeParser to an abandoned module on SilverStripe-archive, make that a dependency of forum, and raise an issue on forum to switch to another parser lib? |
@chillu I've done this here #5998 and here https://github.com/silverstripe-archive/silverstripe-bbcodeparser |
@Firesphere I'm closing this - i think that #5998 covers the most important change |
FYI the question of whether we can use the thunderer library has been picked up again at #5987 |
Hi. I've been doing a little work with shortcodes lately. Our content team needed nested shortcodes in particular. For anyone who is interested in an immediate (interim) solution it's here: https://github.com/GOVTNZ/aacustomshortcodes It is a backwards compatible (runs all existing framework tests) drop-in replacement for the framework's shortcode parser. When registering a shortcode you can tell the parser more about the shortcode so it understands it's nestable, which guides the parser. While it inherits from ShortcodeParser, it is almost completely self-contained, so could form a direct replacement for the framework's parser if that was considered desirable. M |
Thanks @mrmorphic ; I'll keep an eye on your work, and in particular the possibility to support nested shortcodes, once we move to a new shortcode parser. :) |
I'm going to re-open this; Although the specific crash has been removed, the shortcode parser is currently not working optimally, and the need to replace this with an external lib (my pick is thunderer) still exists. |
If anyone who stumbles upon this wants to use the Thunder parser (e.g. to support nested shortcodes), it’s fairly straightforward to do that while leaving the core shortcode functionality untouched: // _config.php
$parser = new ThunderShortcodeParser();
$parser->install();
$parser->registerThunderShortcode('my_shortcode', function(ShortcodeInterface $s) {
return sprintf('Hello, %s!', $s->getParameter('name'));
}); <?php
namespace App\View\Parsers;
use SilverStripe\View\Parsers\ShortcodeParser;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
class ThunderShortcodeParser extends ShortcodeParser
{
protected $thunderShortcodes = [];
public function install()
{
$legacyParser = self::$instances['default'] ?? null;
self::$instances['default'] = $this;
// Copy registered shortcodes from core parser
if ($legacyParser) {
$this->shortcodes = $legacyParser->getRegisteredShortcodes();
}
}
public function registerThunderShortcode($shortcode, $callback)
{
$this->thunderShortcodes[$shortcode] = $callback;
return $this;
}
public function parse($content)
{
$handlers = new HandlerContainer();
foreach ($this->thunderShortcodes as $shortcode => $callback) {
$handlers->add($shortcode, $callback);
}
$processor = new Processor(new RegularParser(), $handlers);
$content = $processor->process($content);
return parent::parse($content);
}
} |
We haven't actioned this after all these years, and the shortcodeparser is not currently a high priority. |
Follow-up from #5487
Replace the current ShortcodeParser library with an external, maintained package. Up for the job so far, seems to be thunderer/Shortcode.
The text was updated successfully, but these errors were encountered: