How to add giphy to markdown-x
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
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
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
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;
}
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
















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 🙌