internal linking
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Finds a real, working favicon/icon URL for a site. `{domain}/favicon.ico`
|
||||
* is a guess, not a guarantee -- plenty of sites serve their icon from a
|
||||
* hashed asset path, an SVG, or declare it only via a <link rel="icon">
|
||||
* tag. This tries progressively more reliable sources before giving up.
|
||||
*/
|
||||
class FaviconResolver
|
||||
{
|
||||
public function resolve(string $siteUrl): ?string
|
||||
{
|
||||
return $this->fromWellKnownPath($siteUrl)
|
||||
?? $this->fromHtmlLinkTag($siteUrl)
|
||||
?? $this->fromGoogleFallback($siteUrl);
|
||||
}
|
||||
|
||||
private function fromWellKnownPath(string $siteUrl): ?string
|
||||
{
|
||||
$candidate = rtrim($siteUrl, '/').'/favicon.ico';
|
||||
|
||||
return $this->isImage($candidate) ? $candidate : null;
|
||||
}
|
||||
|
||||
private function fromHtmlLinkTag(string $siteUrl): ?string
|
||||
{
|
||||
$response = $this->safeGet($siteUrl);
|
||||
|
||||
if (! $response || ! $response->successful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! preg_match_all(
|
||||
'/<link\b[^>]*rel=["\'](?:shortcut icon|icon|apple-touch-icon)["\'][^>]*>/i',
|
||||
$response->body(),
|
||||
$tags
|
||||
)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($tags[0] as $tag) {
|
||||
if (! preg_match('/href=["\']([^"\']+)["\']/i', $tag, $hrefMatch)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$absolute = $this->toAbsoluteUrl($hrefMatch[1], $siteUrl);
|
||||
|
||||
if ($absolute && $this->isImage($absolute)) {
|
||||
return $absolute;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Google's favicon service resolves almost anything (it does its own
|
||||
* multi-source lookup), making it a reliable last resort rather than
|
||||
* leaving the tool with no logo at all.
|
||||
*/
|
||||
private function fromGoogleFallback(string $siteUrl): string
|
||||
{
|
||||
return 'https://www.google.com/s2/favicons?sz=128&domain_url='.urlencode($siteUrl);
|
||||
}
|
||||
|
||||
private function toAbsoluteUrl(string $href, string $base): ?string
|
||||
{
|
||||
if (Str::startsWith($href, ['http://', 'https://'])) {
|
||||
return $href;
|
||||
}
|
||||
|
||||
$parsed = parse_url($base);
|
||||
|
||||
if (! $parsed || ! isset($parsed['scheme'], $parsed['host'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Str::startsWith($href, '//')) {
|
||||
return "{$parsed['scheme']}:{$href}";
|
||||
}
|
||||
|
||||
return "{$parsed['scheme']}://{$parsed['host']}/".ltrim($href, '/');
|
||||
}
|
||||
|
||||
private function isImage(string $url): bool
|
||||
{
|
||||
$response = $this->safeGet($url);
|
||||
|
||||
return (bool) $response?->successful()
|
||||
&& str_starts_with((string) $response->header('Content-Type'), 'image/');
|
||||
}
|
||||
|
||||
private function safeGet(string $url): ?Response
|
||||
{
|
||||
try {
|
||||
return Http::timeout(6)->get($url);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\Tool;
|
||||
use DOMDocument;
|
||||
use DOMText;
|
||||
use DOMXPath;
|
||||
|
||||
/**
|
||||
* Auto-links the first mention of each approved tool's name in rendered
|
||||
* post HTML to that tool's page, so "Top 10" style posts (and any post
|
||||
* that happens to name-drop a tool we list) link back into the directory
|
||||
* without editors having to hand-add the links.
|
||||
*/
|
||||
class ToolLinker
|
||||
{
|
||||
public function link(string $html): string
|
||||
{
|
||||
if (trim($html) === '') {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$tools = Tool::approved()->get(['id', 'name', 'slug'])
|
||||
->sortByDesc(fn (Tool $tool) => mb_strlen($tool->name))
|
||||
->values();
|
||||
|
||||
if ($tools->isEmpty()) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$document = new DOMDocument();
|
||||
libxml_use_internal_errors(true);
|
||||
$document->loadHTML(
|
||||
'<?xml encoding="utf-8"?><div>'.$html.'</div>',
|
||||
LIBXML_NOERROR | LIBXML_NOWARNING
|
||||
);
|
||||
libxml_clear_errors();
|
||||
|
||||
$xpath = new DOMXPath($document);
|
||||
$textNodes = $xpath->query(
|
||||
'//text()[not(ancestor::a) and not(ancestor::code) and not(ancestor::pre) '.
|
||||
'and not(ancestor::script) and not(ancestor::style)]'
|
||||
);
|
||||
|
||||
$alreadyLinked = [];
|
||||
|
||||
foreach ($textNodes as $node) {
|
||||
/** @var DOMText $node */
|
||||
foreach ($tools as $tool) {
|
||||
if (isset($alreadyLinked[$tool->id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$pattern = '/\b'.preg_quote($tool->name, '/').'\b/ui';
|
||||
|
||||
if (! preg_match($pattern, $node->wholeText, $match, PREG_OFFSET_CAPTURE)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->wrapMatchInLink($document, $node, $match[0], $tool->slug);
|
||||
$alreadyLinked[$tool->id] = true;
|
||||
|
||||
// The node has been split and detached from the document;
|
||||
// move on to the next original text node.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$wrapper = $document->getElementsByTagName('div')->item(0);
|
||||
$result = '';
|
||||
|
||||
foreach (iterator_to_array($wrapper->childNodes) as $child) {
|
||||
$result .= $document->saveHTML($child);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{0: string, 1: int} $match [matched text, byte offset] from PREG_OFFSET_CAPTURE
|
||||
*/
|
||||
private function wrapMatchInLink(DOMDocument $document, DOMText $node, array $match, string $slug): void
|
||||
{
|
||||
[$matchedText, $offset] = $match;
|
||||
|
||||
// PREG_OFFSET_CAPTURE offsets are byte offsets, so slice with
|
||||
// substr() (byte-based), not mb_substr() -- the offsets are still
|
||||
// valid UTF-8 boundaries since the /u match itself respects them.
|
||||
$before = substr($node->wholeText, 0, $offset);
|
||||
$after = substr($node->wholeText, $offset + strlen($matchedText));
|
||||
|
||||
$link = $document->createElement('a');
|
||||
$link->setAttribute('href', "/tools/{$slug}");
|
||||
$link->appendChild($document->createTextNode($matchedText));
|
||||
|
||||
$parent = $node->parentNode;
|
||||
|
||||
if ($before !== '') {
|
||||
$parent->insertBefore($document->createTextNode($before), $node);
|
||||
}
|
||||
|
||||
$parent->insertBefore($link, $node);
|
||||
|
||||
if ($after !== '') {
|
||||
$parent->insertBefore($document->createTextNode($after), $node);
|
||||
}
|
||||
|
||||
$parent->removeChild($node);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user