Kirbytext without wrapping paragraph-text

I have a markdown-field with some text in it;

The only markdown-option is bold, as you can see in the example above.

Now I want to spit out this text - with all the “bold” markup - inside a H2-tag;

I styled the H2, so the “bold” text is not bold - and has a lighter gray color.

This way, my client can easily select the words in the H2-tag that must have some highlights / accents.


But of course the html-output is something like this;

<h1><p>Normal text with <strong>a nice accent</strong> in it</h1>


It’s not allowed to have <p> wrapped in a <h2>, so how can I remove the wrapped-p-tags?

I get the value with something like <?php echo $page->titletag()->kirbytext(); ?>

I tried this solution, but none of them worked;


###Is there anyway to preserve the markdown, but without the wrapper p-tags?

I used to use this function to remove p tags from images in older Kirby versions, you could adapt that for your purposes:

<?php
function remove_ptags_from_images($text) {
  return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $text);
}

You might use this Plugin: https://github.com/jbeyerstedt/kirby-plugin-kirbytextRaw

2 Likes

Thanks - I did indeed ended up with that “plugin”.

I can now parse markdown in my header-tags, without p

Maybe it’s not the best approach to style text inside a header; but I prefer to style it visually (using the markdown textfield).

I think it’s a good way. Actually I’m doing nearly the same on my website (I’m using a simple text field to save some space. To make sure I alway get a proper <title> tag I use two fields for the title, where the one containing kirby tags / markdown is optional.

In this field I’m using Kirby pre text filters to replace {{TeX}} with the TeX logo; see http://tobiw.de/en/tex-consulting-and-programming :slight_smile:

I created a plugin getTitle.php to fetch the right title field:

<?php
/*

    usage: getTitle($page, $echo)
    - $page = page object
    - $echo = boolean (optional)
    
*/
function getTitle($page, $echo=true) {

    // get title
    if ($page->htmltitle()->empty()) {
        $title = $page->title();
    } else {
        $title = $page->htmltitle()->kirbytextRaw();
    }
    
    // echo or return title
    if ($echo) {
        echo $title;
        return null;
    } else {
        return $title;
    }

}
1 Like

Instead of the helper function, you could use a page method from Kirby 2.3:

page::$methods['htmltitle'] = function($page, $echo=true) {

    // get title
    if ($page->content()->htmltitle()->empty()) {
        $title = $page->title();
    } else {
        $title = $page->content()->htmltitle()->kirbytextRaw();
    }
    
    // echo or return title
    if ($echo) {
        echo $title;
        return null;
    } else {
        return $title;
    }

};
1 Like

Thanks … that looks nice. I’ll keep it in mind until 2.3 is stable :slight_smile:

A tidy option would be to use PHP’s strip_tag function and set <strong> as the allowed tag.

<?= strip_tags( $page->titletag()->kt(), '<strong>' ) ?>

Just in case you’re still wondering after 2 years. :grin:

1 Like

And for those of us who came through a search here:
Kirby 3 has now kirbytextinline()

9 Likes