Skip to content

Commit 3b7546e

Browse files
committed
Implement consistent HTML preservation modifier
1 parent f88f7b5 commit 3b7546e

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

app/View/Components/Docs/Content.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\View\Modifications\BladeComponentModifier;
66
use App\View\Modifications\BlockquoteColorModifier;
77
use App\View\Modifications\HeaderLinksModifier;
8+
use App\View\Modifications\HTMLCleanseModifier;
89
use App\View\Modifications\ImageAltModifier;
910
use App\View\Modifications\RemoveFirstHeaderModifier;
1011
use App\View\Modifications\ResponsiveTableModifier;
@@ -50,13 +51,10 @@ public function render()
5051
public function toHtml(): string
5152
{
5253
return Cache::remember('doc-content-'.sha1($this->content), now()->addWeek(), function () {
53-
$crawler = new Crawler();
54-
$crawler->addHtmlContent(mb_convert_encoding($this->content, 'UTF-8'));
55-
$content = $crawler->filterXpath('//body')->first()->html();
56-
5754
return app(Pipeline::class)
58-
->send($content)
55+
->send($this->content)
5956
->through([
57+
HTMLCleanseModifier::class, // Стандартизирует HTML
6058
BlockquoteColorModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
6159
RemoveFirstHeaderModifier::class, // Удаляет h1 заголовок
6260
HeaderLinksModifier::class, // Добавляет ссылки для заголовков

app/View/Components/Posts/Content.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\View\Modifications\BladeComponentModifier;
66
use App\View\Modifications\BlockquoteColorModifier;
7+
use App\View\Modifications\HTMLCleanseModifier;
78
use App\View\Modifications\ImageAltModifier;
89
use App\View\Modifications\ResponsiveTableModifier;
910
use App\View\Modifications\TypografModifier;
@@ -57,6 +58,7 @@ public function toHtml(): string
5758
return app(Pipeline::class)
5859
->send($content)
5960
->through([
61+
HTMLCleanseModifier::class, // Стандартизирует HTML
6062
BlockquoteColorModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
6163
ResponsiveTableModifier::class, // Добавляет к таблице класс table-responsive
6264
BladeComponentModifier::class, // Применяет компоненты blade

app/View/Modifications/BlockquoteColorModifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function handle(string $content, \Closure $next)
4444
$html = Str::of($tag)
4545
->replace('<blockquote>', '<blockquote class="'.$class.'"><div>')
4646
->replace('</blockquote>', '</div></blockquote>')
47-
->replace($fragment, '');
47+
->remove($fragment);
4848
});
4949

5050
$content = Str::of($content)->replace($tag, $html);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\View\Modifications;
4+
5+
use Symfony\Component\DomCrawler\Crawler;
6+
7+
class HTMLCleanseModifier extends HTMLModifier
8+
{
9+
/**
10+
* @param string $content The HTML content to be modified.
11+
* @param \Closure $next The next method in the middleware pipeline.
12+
*
13+
* @return mixed The modified HTML content.
14+
*/
15+
public function handle(string $content, \Closure $next)
16+
{
17+
$crawler = new Crawler();
18+
$crawler->addHtmlContent(mb_convert_encoding($content, 'UTF-8'));
19+
$content = $crawler->filterXpath('//body')->first()->html();
20+
21+
return $next($content);
22+
}
23+
}

0 commit comments

Comments
 (0)