Last active
April 14, 2025 14:41
-
-
Save pixelbrackets/186d7955993d0f8c47a62c61fb5dd1c5 to your computer and use it in GitHub Desktop.
sitemap.xml XML to HTML visualisation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// composer require guzzlehttp/guzzle | |
// composer require pixelbrackets/html5-mini-template | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
function fetchSitemapUrls(string $sitemapUrl, Client $client): array { | |
$response = $client->get($sitemapUrl); | |
$xml = simplexml_load_string($response->getBody()->getContents()); | |
$urls = []; | |
// follow nested sitemaps | |
if (isset($xml->sitemap)) { | |
foreach ($xml->sitemap as $sitemap) { | |
$urls = array_merge($urls, fetchSitemapUrls((string)$sitemap->loc, $client)); | |
} | |
} | |
if (isset($xml->url)) { | |
foreach ($xml->url as $url) { | |
$urls[] = (string)$url->loc; | |
} | |
} | |
return $urls; | |
} | |
function buildUrlTree(array $urls, int $maxLevel = 4): array { | |
$tree = []; | |
foreach ($urls as $url) { | |
$parts = parse_url($url); | |
$parts['path'] = isset($parts['path']) ? $parts['path'] : ''; | |
$pathSegments = explode('/', trim($parts['path'], '/')); | |
// Only include up to maxLevel | |
$pathSegments = array_slice($pathSegments, 0, $maxLevel); | |
$current =& $tree; | |
foreach ($pathSegments as $segment) { | |
if (!isset($current[$segment])) { | |
$current[$segment] = []; | |
} | |
$current =& $current[$segment]; | |
} | |
} | |
return $tree; | |
} | |
function generateHtml(array $tree, string $baseUrl, string $path = '', bool $skipShortlinks = true): string { | |
$html = PHP_EOL . '<ul>'; | |
foreach ($tree as $segment => $children) { | |
// Skip shortlinks (first-level item with no subpages) | |
if ($skipShortlinks && $path === '' && empty($children)) { | |
continue; | |
} | |
// Guess the correct title | |
$formattedTitle = ucwords(str_replace('-', ' ', $segment)); | |
$fullPath = $path . '/' . $segment; | |
$html .= PHP_EOL . ' <li><a href="' . $baseUrl . $fullPath . '">' . $formattedTitle . '</a><!--<span>/' . $segment . '</span>-->'; | |
if (!empty($children)) { | |
$html .= generateHtml($children, $baseUrl, $fullPath, true); | |
} | |
$html .= '</li>'; | |
} | |
$html .= PHP_EOL . '</ul>'; | |
return $html; | |
} | |
$sitemapUrl = 'https://www.example.com/sitemap.xml'; | |
$baseUrl = parse_url($sitemapUrl, PHP_URL_SCHEME) . '://' . parse_url($sitemapUrl, PHP_URL_HOST); | |
$urls = fetchSitemapUrls($sitemapUrl, new Client()); | |
$urlTree = buildUrlTree($urls, 3); | |
$html = generateHtml($urlTree, $baseUrl); | |
$template = (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate()) | |
->setStylesheet('https://raw.githubusercontent.com/astuteo/slickmap/refs/heads/master/slickmap.css') | |
->setTitle('Sitemap') | |
->setContent('<div class="sitemap"><nav class="primaryNav">' . $html . '</nav></div>'); | |
echo $template->getMarkup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment