getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.31k stars 168 forks source link

kirbytags:after Hook Happens Before Markdown Parsing #1337

Closed neildaniels closed 5 years ago

neildaniels commented 5 years ago

Describe the bug According to the docs, the kirbytags:after hook should occur after Markdown is parsed. However, it seems to occur before parsing occurs, because the $text variable in the callback still has unparsed Markdown.

This has been tested on latest Starterkit with the following set plugin:

Kirby::plugin('plugin-debugging/kirbytag-after-test', [
    'hooks' => [
        'kirbytags:after' => function (string $text, array $data, array $options) {
            echo '<pre><code>' . htmlspecialchars($text) . '</code></pre>';
            return '<pre><code>' . htmlspecialchars($text) . '</code></pre>';
        },
    ],
]);

If using the Starterkit content, I simply replaced the /photography album description with:

What *does* the fox say?

## Test Title
My title here.

To Reproduce Steps to reproduce the behavior:

  1. Create a plugin with a kirbytags:after hook
  2. Load a page with a field that is KirbyText parsed

Expected behavior The example above is a bit confusing, because I echo out the text and return the text. The echo'd version is more obvious, because the returned value may still be parsed by Markdown (which is unexpected).

I would expect that the $text variable would have no * characters and no # style headings. It should have proper HTML entities.

The $text variable should not have raw markdown at this point.

Kirby Version 3.0.0-RC-4.0 (StarterKit)

bastianallgeier commented 5 years ago

I always had planned to add a second hook that wraps around Kirbytext entirely but I have to admit that I forgot about it. It's now implemented.

That means that you have the following hooks:

kirbytags:before kirbytags:after kirbytext:before kirbytext:after

The kirbytext:before hook is running before kirbytags are being parsed and kirbytext:after is running after the tags have been parsed and markdown has been applied.

With this you have full control where to interject the parsing.

It might make sense to add markdown:before and markdown:after later, but with this change we have no regression in comparison to v2 anymore.