You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+93-2Lines changed: 93 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -404,13 +404,104 @@ You can now use shortcodes in Twig templates and process them with the `|shortco
404
404
405
405
## Developing Shortcode Plugins
406
406
407
-
The **Shortcode Core** plugin is developed on the back of the [Thunderer Advanced Shortcode Engine](https://github.com/thunderer/Shortcode) and as such loads the libraries and classes required to build third party shortcode plugins. Also we introduce a new event called `onShortcodeHandlers()` that allows a 3rd party plugin to create and add their own custom handlers. These are then all processed by the core plugin in one shot.
407
+
The **Shortcode Core** plugin is developed on the back of the [Thunderer Advanced Shortcode Engine](https://github.com/thunderer/Shortcode) and as such loads the libraries and classes required to build third party shortcode plugins.
408
+
409
+
The simplest way to add your own custom shortcodes, it to simply create a new shortcode in a directory (e.g. `user/custom/shortcodes`) such as this simple one to allow for strike-through text:
410
+
411
+
```php
412
+
<?php
413
+
namespace Grav\Plugin\Shortcodes;
414
+
415
+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
Then simply set the plugin to look in this directory for custom shortcodes by editing the `user/config/plugins/shortcode-core.yaml` file (create it if missing):
429
+
430
+
```yaml
431
+
custom_shortcodes: '/user/custom/shortcodes'
432
+
```
433
+
434
+
The more flexible approach is to create a custom plugin to do provide a tidy package for your shotdcodes.
435
+
436
+
We introduced a new event called `onShortcodeHandlers()` that allows a 3rd party plugin to create and add their own custom handlers. These are then all processed by the core plugin in one shot.
Lastly create your shortcode in the `user/plugins/my-plugin/shortcodes/` folder, in this example we created a simple `[red][/red]` shortcode as `RedShortcode.php`:
457
+
458
+
```php
459
+
<?php
460
+
namespace Grav\Plugin\Shortcodes;
461
+
462
+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
> If you have not already done so, I suggest reading the [Grav Plugin Tutorial](http://learn.getgrav.org/plugins/plugin-tutorial) first to gain a full understanding of what you need to develop a Grav plugin.
410
476
411
-
The best way to see how to create a new shortcode-based plugin is to look at the **Shortcode UI** plugin that extends the **Shortcode Core** by adding more shortcodes. It also makes use of Twig to handle processing and has some more advanced shortcode techniques.
477
+
The best way to see how to create a new shortcode-based plugins is to look at the **Shortcode UI** plugin that extends the **Shortcode Core** by adding more shortcodes. It also makes use of Twig to handle processing and has some more advanced shortcode techniques.
## Processing Shortcodes Before or After Markdown processing
486
+
487
+
There are basically two ways of processing a shortcode:
488
+
489
+
1. After markdown is processed
490
+
2. Before markdown is processed
491
+
492
+
These two approaches are important because, for the most part, shortcodes make more sense when they can 'wrap' markdown, so they process **after** markdown.
493
+
494
+
For example a `[div][/div]` shortcode would be useless if it ran before markdown is processed because it would add the relevant HTML `<div></div>` tags, and then the markdown parser would promptly **skip** all markdown processing between those divs because it won't process markdown **inside** HTML. So this shortcode and most others run after markdown processing has already occurred using this approach:
Notice the `getHandlers()` call is the standard way to add a handler.
500
+
501
+
However, there are situations when you need to process the shortcode **before** the markdown processing to ensure markdown **is skipped**, like in the example of a code block. This is why in the [Prism Highlighter](https://github.com/trilbymedia/grav-plugin-prism-highlight) plugin, we use this approach to defining the shortcode:
0 commit comments