Skip to content

Commit

Permalink
add fixHref on admin block editor paragraph to caught user creating a…
Browse files Browse the repository at this point in the history
… link to the edit version instead of public one
  • Loading branch information
RobinDev committed Nov 14, 2023
1 parent 4c45d4d commit c1ad827
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions packages/admin-block-editor/src/Twig/AppExtension.php
Expand Up @@ -2,11 +2,19 @@

namespace Pushword\AdminBlockEditor\Twig;

use Pushword\Core\Component\App\AppPool;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
public function __construct(
private AppPool $appPool,
private readonly \Pushword\Core\Router\RouterInterface $router
) {
}

/**
* @return \Twig\TwigFunction[]
*/
Expand All @@ -18,6 +26,16 @@ public function getFunctions(): array
];
}

/**
* @return \Twig\TwigFilter[]
*/
public function getFilters(): array
{
return [
new TwigFilter('fixHref', $this->fixHref(...), ['is_safe' => ['html'], 'needs_environment' => false]),
];
}

/**
* @param array<mixed>|\stdClass $blockData
* @param array<mixed> $attributes
Expand All @@ -40,4 +58,23 @@ public function needBlockWrapper(array|\stdClass $blockData): bool
{
return '' !== trim($this->blockWrapperAttr($blockData));
}

public function fixHref(string $text): string
{
$regex = '/"(https?)?:\/\/([a-zA-Z0-9.-:]+)\/'.$this->getHostsRegex().'\/?([^"]*)"/';

preg_match_all($regex, $text, $matches);
for ($i = 0; $i < \count($matches[0]); ++$i) {
$text = str_replace($matches[0][$i], '"'.$this->router->generate($matches[4][$i] ?? 'homepage', host: $matches[3][$i]).'"', $text);
}

return $text;
}

private ?string $hostRegex = null;

private function getHostsRegex(): string
{
return $this->hostRegex ??= '('.implode('|', array_map('preg_quote', $this->appPool->getHosts())).')';
}
}
@@ -1,2 +1,2 @@

<p {{ blockWrapperAttr(block, block.data.alignment is defined and block.data.alignment and block.data.alignment != 'left' ? {class: 'text-' ~ block.data.alignment} : {}) }}>{{ block.data.text|raw }}</p>
<p {{ blockWrapperAttr(block, block.data.alignment is defined and block.data.alignment and block.data.alignment != 'left' ? {class: 'text-' ~ block.data.alignment} : {}) }}>{{ block.data.text|fixHref|raw }}</p>
Expand Up @@ -7,7 +7,7 @@
{% if page.video is not null %}replaceBy="{{ url_to_embed(page.video.embed_code|default(page.video))|replace({'"': "'"}) }}"{% endif %}
>
{# md:min-h-[33vh] md:min-h-screen md:min-h-[75vh] #}
<div class="{{ page.hero_container|default(hero_container ?? 'max-w-screen-sm mx-auto relative md:min-h-' ~ heroSize(page.mainImageFormat) ~ ' pt-6') }}">
<div class="{{ page.hero_container|default(hero_container ?? ('max-w-screen-sm mx-auto relative md:min-h-' ~ heroSize(page.mainImageFormat) ~ ' pt-6')) }}">
<div class="{{ page.hero_position|default(hero_position ?? 'p-3 text-gray-50 md:absolute md:bottom-4 md:left-4 z-10 md:[text-shadow:_0_1px_16px_rgb(0_0_0_/_50%),_0_0_1px_rgb(0_0_0_/_50%)]') }}{{ main_image_blur is defined ? ' blur' : '' }}">

{% block subTitle %}
Expand Down
Binary file modified packages/skeleton/var/app.db
Binary file not shown.

0 comments on commit c1ad827

Please sign in to comment.