Skip to content

Commit

Permalink
Add support for HTML sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 8, 2021
1 parent 49d9e4a commit a69208a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v3.0.1
## mm/dd/2021

1. [](#new)
* Added support for new optional `html_support` option that allows you to render the sitemap as an HTML page in your site when you access the sitemap URL with no extension or `.html`. Can be customized and extended in your theme as needed.

# v3.0.0
## 01/30/2021

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ignores:
- /ignore-this-route
- /ignore-children-of-this-route/.*
whitelist:
html_support: false
urlset: 'http://www.sitemaps.org/schemas/sitemap/0.9'
short_date_format: true
include_changefreq: true
Expand Down Expand Up @@ -97,6 +98,12 @@ If you want your sitemap to only be accessible via `sitemap.xml` for example, se

`Redirect 301 /sitemap /sitemap.xml`

## HTML Support

As of Sitemap version `3.0.1` you can enable `html_support` in the configuration and then when you go to `/sitemap` or `/sitemap.html` you will view an HTML version of the sitemap per the `templates/sitemap.html.twig` template.

You can copy and extend this Twig template in your theme to customize it for your needs.

## Manually add pages to the sitemap

You can manually add URLs to the sitemap using the Admin settings, or by adding entries to your `sitemap.yaml` with this format:
Expand Down
7 changes: 6 additions & 1 deletion languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ en:
INCLUDE_PRIORITY: 'Include Priority'
SHORT_DATE_FORMAT: 'Short Date Format'
SHORT_DATE_FORMAT_HELP: 'Use Short or Long Date format'

HTML_SUPPORT: 'HTML Support'
HTML_SUPPORT_HELP: 'Use "sitemap.html.twig" if no extension or ".html" passed in the URL'
TITLE_LOCATION: 'Location'
TITLE_TITLE: 'Title'
TITLE_LASTMOD: 'Last Modified'
UNTITLED: 'Untitled'
ru:
PLUGIN_SITEMAP:
SITEMAP: 'Карта сайта'
Expand Down
14 changes: 12 additions & 2 deletions sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Twig\Twig;
use Grav\Common\Uri;
use Grav\Common\Page\Pages;
use Grav\Common\Utils;
Expand Down Expand Up @@ -104,6 +105,7 @@ public function onPagesInitialized()
/** @var Language $language */
$language = $this->grav['language'];
$default_lang = $language->getDefault() ?: 'en';
$active_lang = $language->getActive() ?? $default_lang;
$languages = $language->enabled() ? $language->getLanguages() : [$default_lang];

$this->multilang_skiplang_prefix = $this->config->get('system.languages.include_default_lang') ? '' : $language->getDefault();
Expand All @@ -119,14 +121,21 @@ public function onPagesInitialized()
$this->ignore_protected = $this->config->get('plugins.sitemap.ignore_protected');
$this->ignore_redirect = $this->config->get('plugins.sitemap.ignore_redirect');

// Gather data
// Gather data for all languages
foreach ($languages as $lang) {
$language->init();
$language->setActive($lang);
$pages->reset();
$this->addRouteData($pages, $lang);
}

// Reset back to active language
if ($language->enabled() && $language->getActive() !== $active_lang) {
$language->init();
$language->setActive($active_lang);
$pages->reset();
}

// Build sitemap
foreach ($languages as $lang) {
foreach($this->route_data as $route => $route_data) {
Expand Down Expand Up @@ -166,7 +175,8 @@ public function onPageInitialized($event)
$route = $this->config->get('plugins.sitemap.route');

if (is_null($page) || $page->route() !== $route) {
$extension = $this->grav['uri']->extension() ?? 'xml';
$html_support = $this->config->get('plugins.sitemap.html_support', false);
$extension = $this->grav['uri']->extension() ?? ($html_support ? 'html': 'xml');

// set a dummy page
$page = new Page;
Expand Down
1 change: 1 addition & 0 deletions sitemap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ignore_protected: true
ignore_redirect: true
ignores:
whitelist:
html_support: false
urlset: 'http://www.sitemaps.org/schemas/sitemap/0.9'
short_date_format: true
include_changefreq: true
Expand Down
31 changes: 31 additions & 0 deletions templates/sitemap.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends 'partials/base.html.twig' %}

{% block content %}
{% set language = grav.language %}
{% set lang = language.enabled ? (language.active ?: language.default) : null %}
{% set count = 1 %}

<table class="sitemap">
<thead>
<tr>
<th>#</th>
<th>{{ 'PLUGIN_SITEMAP.TITLE_TITLE'|t }}</th>
<th>{{ 'PLUGIN_SITEMAP.TITLE_LOCATION'|t }}</th>
<th>{{ 'PLUGIN_SITEMAP.TITLE_LASTMOD'|t }}</th>
</tr>
</thead>
<tbody>
{% for entry in sitemap %}
{% if lang is null or entry.lang == lang %}
<tr>
<td>{{ count }}</td>
<td><a href="{{ entry.location }}">{{ entry.title ?: 'PLUGIN_SITEMAP.UNTITLED'|t }}</a></td>
<td><a href="{{ entry.location }}">{{ entry.location }}</a></td>
<td>{{ entry.lastmod }}</td>
</tr>
{% set count = count + 1 %}
{% endif %}
{% endfor %}
</tbody>
</table>
{% endblock %}

0 comments on commit a69208a

Please sign in to comment.