Skip to content

Commit

Permalink
Implement consistent HTML preservation modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Mar 13, 2024
1 parent f88f7b5 commit 3b7546e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
8 changes: 3 additions & 5 deletions app/View/Components/Docs/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\View\Modifications\BladeComponentModifier;
use App\View\Modifications\BlockquoteColorModifier;
use App\View\Modifications\HeaderLinksModifier;
use App\View\Modifications\HTMLCleanseModifier;
use App\View\Modifications\ImageAltModifier;
use App\View\Modifications\RemoveFirstHeaderModifier;
use App\View\Modifications\ResponsiveTableModifier;
Expand Down Expand Up @@ -50,13 +51,10 @@ public function render()
public function toHtml(): string
{
return Cache::remember('doc-content-'.sha1($this->content), now()->addWeek(), function () {
$crawler = new Crawler();
$crawler->addHtmlContent(mb_convert_encoding($this->content, 'UTF-8'));
$content = $crawler->filterXpath('//body')->first()->html();

return app(Pipeline::class)
->send($content)
->send($this->content)
->through([
HTMLCleanseModifier::class, // Стандартизирует HTML
BlockquoteColorModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
RemoveFirstHeaderModifier::class, // Удаляет h1 заголовок
HeaderLinksModifier::class, // Добавляет ссылки для заголовков
Expand Down
2 changes: 2 additions & 0 deletions app/View/Components/Posts/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\View\Modifications\BladeComponentModifier;
use App\View\Modifications\BlockquoteColorModifier;
use App\View\Modifications\HTMLCleanseModifier;
use App\View\Modifications\ImageAltModifier;
use App\View\Modifications\ResponsiveTableModifier;
use App\View\Modifications\TypografModifier;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function toHtml(): string
return app(Pipeline::class)
->send($content)
->through([
HTMLCleanseModifier::class, // Стандартизирует HTML
BlockquoteColorModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
ResponsiveTableModifier::class, // Добавляет к таблице класс table-responsive
BladeComponentModifier::class, // Применяет компоненты blade
Expand Down
2 changes: 1 addition & 1 deletion app/View/Modifications/BlockquoteColorModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function handle(string $content, \Closure $next)
$html = Str::of($tag)
->replace('<blockquote>', '<blockquote class="'.$class.'"><div>')
->replace('</blockquote>', '</div></blockquote>')
->replace($fragment, '');
->remove($fragment);
});

$content = Str::of($content)->replace($tag, $html);
Expand Down
23 changes: 23 additions & 0 deletions app/View/Modifications/HTMLCleanseModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\View\Modifications;

use Symfony\Component\DomCrawler\Crawler;

class HTMLCleanseModifier extends HTMLModifier
{
/**
* @param string $content The HTML content to be modified.
* @param \Closure $next The next method in the middleware pipeline.
*
* @return mixed The modified HTML content.
*/
public function handle(string $content, \Closure $next)
{
$crawler = new Crawler();
$crawler->addHtmlContent(mb_convert_encoding($content, 'UTF-8'));
$content = $crawler->filterXpath('//body')->first()->html();

return $next($content);
}
}

0 comments on commit 3b7546e

Please sign in to comment.