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
Solved

How to add giphy to markdown-x

Solved
superdev

Dec 22nd, 2021 08:42 PM

Hey there devdojo users! In a bit of a pickle. I don't understand how to integrate giphy into markdown-x. I cant find a spot that works. Any solution?

I already added the api key and allowed it as a useable component. It just wont render the block. I checked documentation and found the snippet, just don't know how to implement it myself.

Thanks in advance

bobbyiliev

Dec 22nd, 2021 11:18 PM

Hi there,

What you could do is use the following MarkdownHelper method here:

https://devdojo.com/markdownx/docs/concepts/liquid-tags

This will allow you to actually render the liquid tags too.

You can add the method to the MarkdownX.php Controller file and then call it in the updateContentPreview method.

Let me know how it goes!

Best,

Bobby

superdev

Dec 23rd, 2021 06:32 AM

Hi, I tried that and it gives a function error. Do you have a snippet I can use to implement it? Much appreciated.

I implemented the function but cant figure out how to implement the 'class MarkdownHelper {'

bobbyiliev

Dec 23rd, 2021 02:11 PM

Hi there,

You can update the updateContentPreview() method to the following:

    public function updateContentPreview(){

        $this->contentPreview = $this->parseLiquidTags(Str::markdown($this->content));
    }

And then right bellow it, you could add the methods that you want to include like this:

    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 'buymeacoffee':
                            $html = self::replaceBuyMeACoffeeTag($html, $matchArray, $match);
                            break;
                        case 'giphy':
                            $html = self::replaceGiphyTag($html, $matchArray, $match);
                            break;
                    }
                }
            }
        }

        return $html;
    }

public static function replaceYouTubeTag($html, $tagArray, $original_string){
    if(isset($tagArray[2])){
        $youtubeEmbedURL = $tagArray[2];
        $youtubeEmbed = '<iframe width="100%" height="399" src="https://www.youtube.com/embed/' . $youtubeEmbedURL . '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
        $html = str_replace($original_string, $youtubeEmbed, $html);
    }
    return $html;
}

public static function replaceGiphyTag($html, $tagArray, $original_string){
    if(isset($tagArray[2])){
        $giphyEmbed = $tagArray[2];
        $giphyEmbed = '<div style="width:100%;height:0;padding-bottom:56%;position:relative;display:block"><iframe src="' . $giphyEmbed . '" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></p></div>';
        $html = str_replace($original_string, $giphyEmbed, $html);
    }
    return $html;
}

Let me know if you have any questions. Basically there is no need to include the whole MarkdownHelper class but just add the methods that you need as shown above.

Best, Bobby

superdev

Dec 23rd, 2021 02:40 PM

Hey there, Bobby! This worked! Unfortunately, giphy returns a 404 error.

What we did below is the implementation you provided.

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

    }

 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 'buymeacoffee':
                            $html = self::replaceBuyMeACoffeeTag($html, $matchArray, $match);
                            break;
                        case 'giphy':
                            $html = self::replaceGiphyTag($html, $matchArray, $match);
                            break;
                    }
                }
            }
        }

        return $html;
    }

public static function replaceYouTubeTag($html, $tagArray, $original_string){
    if(isset($tagArray[2])){
        $youtubeEmbedURL = $tagArray[2];
        $youtubeEmbed = '<iframe width="100%" height="399" src="https://www.youtube.com/embed/' . $youtubeEmbedURL . '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
        $html = str_replace($original_string, $youtubeEmbed, $html);
    }
    return $html;
}

public static function replaceGiphyTag($html, $tagArray, $original_string){
    if(isset($tagArray[2])){
        $giphyEmbed = $tagArray[2];
        $giphyEmbed = '<div style="width:100%;height:0;padding-bottom:56%;position:relative;display:block"><iframe src="' . $giphyEmbed . '" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></p></div>';
        $html = str_replace($original_string, $giphyEmbed, $html);
    }
    return $html;
}
bobbyiliev

Dec 23rd, 2021 11:59 PM

Best Answer

Hi there,

Indeed I was able to replicate the problem. I've patched the replaceGiphyTag method a little bit to avoid this from happening.

You can change the method with the following content:

public static function replaceGiphyTag($html, $tagArray, $original_string){
    if(isset($tagArray[2]) && isset($tagArray[3])){
        $giphyEmbedLink = $tagArray[2] . ' ' .$tagArray[3];
        preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $giphyEmbedLink, $result);
        if (empty($result['href'])) {
            return $html;
        }
        $giphyEmbed = $result['href'][0];
        $giphyEmbed = '<div style="width:100%;height:0;padding-bottom:56%;position:relative;display:block"><iframe src="' . $giphyEmbed . '" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></p></div>';
        $html = str_replace($original_string, $giphyEmbed, $html);
    }
    return $html;
}

I will look into adding this patch to the official version too.

Thank you for reporting the problem!

Best, Bobby

Report
1
superdev

Dec 24th, 2021 10:05 AM

I was also wondering, how would I implement liquid tags into the final output of the editor? Like, the published instance and not the preview editor.

Ive marked your solution as an accepted answer. Have a good Christmas.

bobbyiliev

Dec 24th, 2021 01:03 PM

Hi there,

You can use the same method to parse the markdown content and the liquid tags when displaying them to your actual website.

You can add that method to your controller directly.

Let me know how it goes!

Happy holidays to you too 🙌

superdev

Sep 20th, 2022 06:07 PM

Sounds good! Sorry for the late reply. If I can get it working, ill make a pull request to wordsmith, so everyone can have the nice features ( know quite a few who want videos in there blogs )