Skip to content

Commit 64266e3

Browse files
eric-gadeigorkorenfeldjduss4
committed
Adding filter for fully-qualified URL option
Co-authored-by: Igor Korenfeld <[email protected]> Co-authored-by: Jessica Dussault <[email protected]>
1 parent eb26c37 commit 64266e3

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

.eleventy.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const yaml = require("js-yaml");
1515

1616
const { imageShortcode, imageWithClassShortcode } = require('./config');
1717

18+
const siteData = yaml.load(fs.readFileSync('./_data/site.yaml', 'utf8'));
19+
1820
module.exports = function (config) {
1921
// Set pathPrefix for site
2022
let pathPrefix = '/';
@@ -73,6 +75,12 @@ module.exports = function (config) {
7375
config.addFilter('contrastRatio', contrastRatio);
7476
config.addFilter('humanReadableContrastRatio', humanReadableContrastRatio);
7577

78+
// Create absolute urls
79+
config.addFilter('asAbsoluteUrl', (relativeUrl) => {
80+
const host = siteData.host;
81+
return new URL(relativeUrl, host).href;
82+
});
83+
7684
// Create an array of all tags
7785
config.addCollection('tagList', function (collection) {
7886
let tagSet = new Set();

sitemap.xml.njk

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ eleventyExcludeFromCollections: true
44
---
55
<?xml version="1.0" encoding="utf-8"?>
66
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
7-
{%- for page in collections.all %}
8-
{% if page.url %}
9-
{% set absoluteUrl %}{{ page.url | url | absoluteUrl }}{% endset %}
10-
<url>
11-
<loc>{{ absoluteUrl }}</loc>
12-
<lastmod>{{ page.date | htmlDateString }}</lastmod>
13-
</url>
14-
{% endif %}
15-
{%- endfor %}
7+
{%- for page in collections.all %}
8+
{%- if page.url %}
9+
{%- set renderedUrl %}{{ page.url | url }}{%- endset %}
10+
<url>
11+
{%- if env.production %}
12+
<loc>{{ renderedUrl | asAbsoluteUrl }}</loc>
13+
{%- else %}
14+
<loc>{{ renderedUrl }}</loc>
15+
{%- endif %}
16+
<lastmod>{{ page.date | htmlDateString }}</lastmod>
17+
</url>
18+
{%- endif %}
19+
{%- endfor %}
1620
</urlset>

0 commit comments

Comments
 (0)