fabianmichael / kirby-markdown-field

Super-sophisticated markdown editor for Kirby 3, community built.
Other
162 stars 14 forks source link

Support for the `==highlight==` => `<mark>highlight</mark>` syntax #167

Closed alexkazachkov closed 1 year ago

alexkazachkov commented 1 year ago

I wanted to use this highlight feature, where the == is converted to <mark>, and the following info is in the code file of KirbytextLanguage.js beginning with line 68:

Support for the ==highlight== => <mark>highlight</mark> syntax

However, I cannot get this to work and it always stays as==highlight== on the rendered Kirby page.

What am I missing, or is there somewhere a mistake? In the readme it is written, that the textmarker is not supported by Kirby’s default markdown parser, but I thought this beforementioned code is doing something for me to be able to use it with the html block <mark></mark>.

fabianmichael commented 1 year ago

You are right, the ==highlight== syntax is not supported by Kirby’s markdown parser and not part of the original Markdown parser. I needed it in a few projects, so I baked it right into the editor but did not want to go as far as forcing an alternate Markdown parser on everyone. Basic support can easily be added with a simple plugin:

# site/plugins/markdown-highlight-tag/index.php
Kirby::plugin('my/markdown-highlight-tag', [
    'hooks' => [
        'kirbytags:before' => function($text, $data, $options) {
            // `==highlighted text==`
            return preg_replace('/==(?=\S)(.+?)(?<=\S)==/', '<mark>$1</mark>', $text);
        },
    ],
]);

You could go even further by extending the Parsedown parser, but I found that this simple regex gets the job done for me and is much easier to maintain over time.

alexkazachkov commented 1 year ago

Thank you, Fabian.