PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Unsolved

Using markdown x liquidtags outside Livewire

hilmanhgb

Feb 14th, 2023 09:46 PM

Hi I'd like to use the liquidtags outside livewire

So I create a helper function something like this (copying from docs regarding LuqidTags)

except the return part, I wrap it in Str::markdown

<?php

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;

class MarkdownHelper {

    public static function parseLiquidTags($html){
        $matches = "";

        // If we find at least one liquid tag
        if( preg_match_all('/{% .* %}/', $html, $matches) && isset($matches[0]) ){

            // loop through each of the liquid tags
            foreach($matches[0] as $index => $match){

                // replace multiple spaces with single space
                $matchArray = explode(" ", preg_replace('!\s+!', ' ', $match));

                // gaurantee we have the first value and run specific function for specific tag
                if($matchArray[1]){
                    switch($matchArray[1]){
                        case 'youtube':
                            $html = self::replaceYouTubeTag($html, $matchArray, $match);
                            break;
                        case 'codepen':
                            $html = self::replaceCodePenTag($html, $matchArray, $match);
                            break;
                        case 'codesandbox':
                            $html = self::replaceCodeSandboxTag($html, $matchArray, $match);
                            break;
                        case 'giphy':
                            $html = self::replaceGiphyTag($html, $matchArray, $match);
                            break;
                    }
                }
            }
        }

        // return as unescaped raw html
        return Str::markdown($html);
    }

So in my blade template plan to display it like this:

{!! MarkdownHelper::parseLiquidTags($lesson->subject) !!}

Problem is it renders the codepen tags for example as normal text Screen Shot 2023-02-15 at 13.46.11.png

Without Str::markdown indeed it works for Luqidtags, BUT won't render other normal element from markdown to HTML

I guess because Str::markdown see it as string need to be escaped. Has anyone solved this issue ?

thanks

hilmanhgb

Feb 14th, 2023 10:08 PM

Here's how I solve it so far not sure, if there's correct/cleaner way to do this

   public static function replaceCodePenTag($html, $tagArray, $original_string){
        if(isset($tagArray[2])){
            
            $embedLink = $tagArray[2] . ' ' .$tagArray[3];
            preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $embedLink, $result);
            if (empty($result['href'])) {
                return $html;
            }
            $codepenEmbedURL = $result['href'][0];
            $codepenEmbedURL = str_replace("/pen/", "/embed/", $codepenEmbedURL);
            
            // $defaultTag = 'default-tab=result';
            // if(isset($tagArray[3]) && $tagArray[3] != '%}'){
            //     $defaultTag = $tagArray[3];
            // }

            $codepenEmbed = '<div class="overflow-hidden border border-gray-100 rounded-lg">
                                <iframe loading="lazy" height="600" style="width: 100%;" 
                                        scrolling="no" 
                                        src="' . $codepenEmbedURL . '" 
                                        frameborder="no" 
                                        allowtransparency="true" 
                                        allowfullscreen="true">
                                    </iframe></div>';
            $html = str_replace($original_string, $codepenEmbed, $html);
        }
        return $html;
    }
bobbyiliev

Feb 16th, 2023 12:43 AM

Hi there,

What I usually do is to:

  • Add the helper function
  • When displaying the content, first use the Str::markdown($this->content); method first and then parse the liquid tags

If you first parse the liquid tags, the Str::markdown method will escape the HTML if this is how you've configured the markdown parser to work.

Let me know how it goes!

hilmanhgb

Feb 16th, 2023 02:14 AM

Can you provide a working code sample for codepen with your method ?

bobbyiliev

Mar 6th, 2023 08:34 AM

Hi there,

Sorry missed that comment last week. Did you get this working?

The way that you have it is more or less correct, you just need to parse the Markdown first and then pass it to the liquid tag helper, eg:

    public function updateContentPreview(){
        $this->contentPreview = $this->parseLiquidTags( Str::markdown($this->content) );
    }
hilmanhgb

Mar 6th, 2023 02:41 PM

Hi @bobby as I mentioned in the question, I'd like to use it outside livewire, just a normal blade render

bobbyiliev

Mar 7th, 2023 01:49 AM

Hi there,

Yes, it will be the absolute same thing, but you just call your helper method wherever you need it, eg:

{!! MarkdownHelper::parseLiquidTags(Str::markdown($leson->subject)) !!}

You will just have to update your MarkdownHelper class and change the last line from:

        return Str::markdown($html);

To:

        return $html;

Best,

Bobby