Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: adds advanced link handling in core #3455

Merged
merged 4 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions framework/core/src/Extend/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Extend;

use Flarum\Extension\Extension;
use Flarum\Foundation\Config;
use Illuminate\Contracts\Container\Container;
use Laminas\Diactoros\Uri;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;

class Link implements ExtenderInterface
{
protected $setRel = null;
protected $setTarget = null;

public function setRel(callable $callable): static
{
$this->setRel = $callable;

return $this;
}

public function setTarget(callable $callable): static
{
$this->setTarget = $callable;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
$siteUrl = $container->make(Config::class)->url();

(new Formatter)->render(function (Renderer $renderer, $context, string $xml) use ($siteUrl) {
return Utils::replaceAttributes($xml, 'URL', function ($attributes) use ($siteUrl) {
$uri = isset($attributes['url'])
? new Uri($attributes['url'])
: null;

$setRel = $this->setRel;
if ($setRel && $rel = $setRel($uri, $siteUrl, $attributes)) {
$attributes['rel'] = $rel;
}

$setTarget = $this->setTarget;
if ($setTarget && $target = $setTarget($uri, $siteUrl, $attributes)) {
$attributes['target'] = $target;
}

return $attributes;
});
})->extend($container);
}
}
29 changes: 25 additions & 4 deletions framework/core/src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace Flarum\Formatter;

use DOMDocument;
use DOMElement;
use Illuminate\Contracts\Cache\Repository;
use Psr\Http\Message\ServerRequestInterface;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Unparser;
use s9e\TextFormatter\Utils;

class Formatter
{
Expand Down Expand Up @@ -98,7 +102,7 @@ public function parse($text, $context = null)
* Render parsed XML.
*
* @param string $xml
* @param mixed $context
* @param mixed|null $context
* @param ServerRequestInterface|null $request
* @return string
*/
Expand All @@ -110,6 +114,8 @@ public function render($xml, $context = null, ServerRequestInterface $request =
$xml = $callback($renderer, $context, $xml, $request);
}

$xml = $this->configureDefaultsOnLinks($renderer, $xml, $context, $request);

return $renderer->render($xml);
}

Expand Down Expand Up @@ -174,11 +180,13 @@ protected function getConfigurator()
*/
protected function configureExternalLinks(Configurator $configurator)
{
/** @var DOMDocument $dom */
$dom = $configurator->tags['URL']->template->asDOM();

/** @var DOMElement $a */
foreach ($dom->getElementsByTagName('a') as $a) {
$rel = $a->getAttribute('rel');
$a->setAttribute('rel', "$rel nofollow ugc");
$a->prependXslCopyOf('@target');
$a->prependXslCopyOf('@rel');
}

$dom->saveChanges();
Expand Down Expand Up @@ -217,7 +225,7 @@ protected function getParser($context = null)
/**
* Get the renderer.
*
* @return \s9e\TextFormatter\Renderer
* @return Renderer
*/
protected function getRenderer()
{
Expand All @@ -239,4 +247,17 @@ public function getJs()
{
return $this->getComponent('js');
}

protected function configureDefaultsOnLinks(
Renderer $renderer,
string $xml,
$context = null,
ServerRequestInterface $request = null
): string {
return Utils::replaceAttributes($xml, 'URL', function ($attributes) {
$attributes['rel'] = $attributes['rel'] ?? 'ugc nofollow';

return $attributes;
});
}
}