Create Markdown anchors and Link to other page’s anchor

I’ve checked this existing topic concerning Kirby v2: Internal anchors

And I was wondering how to create markdown anchors inside a textarea and then create links to other page’s anchors with Kirby 3.2.5?

Many thanks,

You could auto-generate these anchors from headlines, but then linking to them would mean you have to actually check out what the anchor is named before you can link to it.

On the website, for example, each h2 headline is an anker:

This can be achieved with a field method:

        'anchorHeadlines' => function ($field, $headlines = 'h2|h3') {

            $headlinesPattern = is_array($headlines) ? implode('|', $headlines) : $headlines;
            $lastH2 = null;
            // add anchors to headlines
            $field->value = preg_replace_callback('!<(' . $headlinesPattern . ')>(.*?)</\\1>!s', function ($match) use (&$lastH2) {
                $id = Str::slug(Str::unhtml($match[2]));
                if ($match[1] === 'h3') {
                    $id = $lastH2 . '__' . $id;
                } else {
                    $lastH2 = $id;
                return '<' . $match[1] . ' id="' . $id . '"><a href="#' . $id . '">' . $match[2] . '</a></' . $match[1] . '>';
            }, $field->value);
            return $field;

Then in template:

<?php echo $page->text()->kt()->anchorHeadlines() ?>

Does that means, I create a new plugin with the field method? Like here?
In your example, you’re mentionning h2 and h3. How can I extend it up to H5?
Then, in a textarea of a page, how can I define in markdown language a specific anchor to another page?
In the documentation related to the links, I’ve found this:
(link: #some-section text: Link to some section on the same page)

Many thanks.