mpratt / Embera

A Oembed consumer library, that gives you information about urls. It helps you replace urls to youtube or vimeo for example, with their html embed code. It has advanced features like offline support, responsive embeds and caching support.
MIT License
335 stars 59 forks source link

Allowing CKEditor5 code #66

Closed charlesastwood closed 4 years ago

charlesastwood commented 4 years ago

I am using CKEditor 5 which outputs embedded data such as this:

<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>
<p>Above and below some video</p>

Is there a way using this library to output the iFrame, when I try running this through autoEmbed I get the following:

<p>This is a some text</p>
<figure class="media"><oembed url="<iframe width="480" height="270" src="https://www.youtube.com/embed/NyEq7unEv5I?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>"></oembed></figure>
<p>Above and below some video</p>

It looks like as it's within the oembed url= it isn't displaying on the site

mpratt commented 4 years ago

Hi @charlesastwood

Sorry for the late response. You can use the ignore_tags directive.

use Embera\Embera;

$config = [
    'ignore_tags' => [  'pre', 'code', 'a', 'img', 'iframe', 'oembed'  ]
];

$embera = new Embera($config);
echo $embera->autoEmbed('<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>
<p>Above and below some video</p>');

Should output

<p>This is a some text</p><figure class="media"><oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed></figure><p>Above and below some video</p>

As you can see, the url remains intact.

Let me know if that is what you are searching for.

charlesastwood commented 4 years ago

HI @mpratt

I was hoping it would translate to:

<p>This is a some text</p>
<figure class="media"><iframe width="480" height="270" src="https://www.youtube.com/embed/NyEq7unEv5I?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></figure>
<p>Above and below some video</p>

So it translates the URL into the iFrame using the autoEmbed functionality

mpratt commented 4 years ago

Hi! Oh, my bad I understood it the other way around.

The first thing that comes to mind is converting the oembed tag into the url inside of it. You can pass the output to Embera and process.

There is a setting inside the ProviderCollectionAdapter called url_extractor_regex that could be changed in order to support oembed tags but It would require more changes into the library itself, so I think preparing the text before passing it through Embera is your best choice.

$text = '<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>
<p>Other Video: https://www.youtube.com/watch?v=pILCn6VO_RU</p>';

$text = preg_replace('~<oembed url="(https?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_)))"></oembed>~is', '$1', $text);

$embera = new Embera();
echo $embera->autoEmbed($text);

Outputs:

<p>This is a some text</p>
<figure class="media">
    <iframe width="480" height="270" src="https://www.youtube.com/embed/NyEq7unEv5I?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</figure>
<p>Other Video: <iframe width="480" height="270" src="https://www.youtube.com/embed/pILCn6VO_RU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>

That should work if there is only one oembed element in your content.

However I think the better solution should be something like this

$text = '<p>This is a some text</p>
<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=NyEq7unEv5I"></oembed>
</figure>

<figure class="media">
    <oembed url="https://unsupportedprovider.com/watch?v=NyEq7unEv5I"></oembed>
</figure>

<p>Other Video: https://www.youtube.com/watch?v=pILCn6VO_RU</p>

<figure class="media">
    <oembed url="https://www.youtube.com/watch?v=62EB4JniuTc"></oembed>
</figure>';

if (preg_match_all('~<oembed url="(https?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_)))"></oembed>~is', $text, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $m) {
        $text = str_replace($m['0'], $m['1'], $text);
    }
}

$embera = new Embera();
$text = $embera->autoEmbed($text);

foreach ((array) $matches as $m) {
    $text = str_replace($m['1'], $m['0'], $text);
}

echo $text;

The last loop ensures that we dont loose content in case there is an oembed tag with an unsupported oembed provider.

I think this is the best way to handle this type of scenarios.