PHP code example of bayareawebpro / laravel-dom-pipeline
1. Go to this page and download the library: Download bayareawebpro/laravel-dom-pipeline library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bayareawebpro / laravel-dom-pipeline example snippets
use BayAreaWebPro\DomPipeline\DomPipeline;
use My\Pipes\{
LazyLoadImageTags,
LazyLoadVideoTags,
BuildTableOfContents
};
$modified = DomPipeline::make($html, [
LazyLoadImageTags::class,
LazyLoadVideoTags::class,
BuildTableOfContents::class,
]);
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use Closure;
use DOMElement;
use DOMDocument;
class LazyLoadImages
{
public function handle(DOMDocument $dom, Closure $next)
{
foreach ($dom->getElementsByTagName('img') as $node) {
$this->lazyLoad($node);
}
return $next($dom);
}
protected function lazyLoad(DOMElement $node): void
{
if (!$node->hasAttribute('loading')) {
$node->setAttribute('loading', 'lazy');
}
}
}
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use Closure;
use DOMElement;
use DOMDocument;
class LazyLoadVideos
{
public function handle(DOMDocument $dom, Closure $next)
{
$xpath = new \DOMXPath($dom);
foreach($xpath->query('//iframe[@class="media"]') as $node){
$this->lazyLoad($dom, $node);
}
return $next($dom);
}
protected function lazyLoad(DOMDocument $dom, DOMElement $node): void
{
if(is_null($node->parentNode)) return;
// Match the YouTube Video ID.
// https://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i',
(string) $node->getAttribute('src'), $matches
);
if(isset($matches[1])){
// Create a new HTML fragment.
$fragment = $dom->createDocumentFragment();
$fragment->appendXML(<<<HTML
<v-video id="$matches[1]" label="Click to play..." :show-image="true"></v-video>
HTML);
// Replace Self with Fragment.
$node->parentNode->replaceChild($fragment, $node);
}
}
}
declare(strict_types=1);
namespace App\Services\Html\Formatters;
use DOMElement;
use Closure;
use StdClass;
use DOMDocument;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
class TableOfContents
{
public function handle(DOMDocument $dom, Closure $next)
{
$nodes = Collection::make();
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//h1|//h2|//h3|//h4|//h5|//h6') as $node) {
$text = strip_tags(html_entity_decode((string) $node->nodeValue));
$nodes->push($this->makeBookmark($node, $text));
}
if($nodes->count() > 5){
View::share('tableOfContents', $nodes->take(25));
}
return $next($dom);
}
protected function makeBookmark(DOMElement $node, string $text): StdClass
{
$bookmark = (object)[
'anchor' => Str::slug($text),
'text' => Str::title(Str::replaceLast('.', '', $text)),
];
$node->setAttribute('id', $bookmark->anchor);
return $bookmark;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.