aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2023-12-24 08:13:33 +0000
committerKevin J Hoerr <kjhoerr@protonmail.com>2023-12-24 08:13:33 +0000
commit1c4c99f0c19f19424e3aee04e868c38333164dd9 (patch)
tree783970ddbd3d1f696493a8b66d9cff6aed533b68 /src
parent3bc3700152504ac0ff4f9a42a45ab59cde2e189d (diff)
downloadsubmelon.dev-1c4c99f0c19f19424e3aee04e868c38333164dd9.tar.gz
submelon.dev-1c4c99f0c19f19424e3aee04e868c38333164dd9.tar.bz2
submelon.dev-1c4c99f0c19f19424e3aee04e868c38333164dd9.zip
Generate sitemap.xml, robots.txt
Diffstat (limited to 'src')
-rw-r--r--src/config.ts6
-rw-r--r--src/pages/robots.njk7
-rw-r--r--src/pages/sitemap.njk15
-rw-r--r--src/util/timestamp.ts7
4 files changed, 34 insertions, 1 deletions
diff --git a/src/config.ts b/src/config.ts
index 75ba2dd..c9f5ba0 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -1,4 +1,4 @@
-import { getTimestamp } from "./util/timestamp";
+import { formatDate, getTimestamp } from "./util/timestamp";
import BuildInfo from "../config.json";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -9,10 +9,14 @@ export default function (eleventyConfig: any) {
});
// hook in configured build time for GH action to update
+ eleventyConfig.addGlobalData("site", () => BuildInfo);
eleventyConfig.addGlobalData("buildTimeEncoded", () => {
return getTimestamp(Number(BuildInfo.version));
});
+ // add `date` filter
+ eleventyConfig.addFilter("formatDate", formatDate);
+
// ensure eleventy marks this config as watched, since it's typescript
eleventyConfig.addWatchTarget("./src/config.ts");
diff --git a/src/pages/robots.njk b/src/pages/robots.njk
new file mode 100644
index 0000000..efb8b84
--- /dev/null
+++ b/src/pages/robots.njk
@@ -0,0 +1,7 @@
+---
+eleventyExcludeFromCollections: true
+permalink: /robots.txt
+---
+User-agent: *
+Allow: /
+Sitemap: {{ site.url }}/sitemap.xml \ No newline at end of file
diff --git a/src/pages/sitemap.njk b/src/pages/sitemap.njk
new file mode 100644
index 0000000..455c26b
--- /dev/null
+++ b/src/pages/sitemap.njk
@@ -0,0 +1,15 @@
+---
+eleventyExcludeFromCollections: true
+permalink: /sitemap.xml
+---
+<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+{% for item in collections.all %}
+ <url>
+ <loc>{{ site.url }}{{ item.url }}</loc>
+ <lastmod>{{ item.date | formatDate }}</lastmod>
+ <changefreq>{{ item.data.sitemapChangefreq | default("monthly") }}</changefreq>
+ <priority>{{ item.data.sitemapPriority | default(0.8) }}</priority>
+ </url>
+{% endfor %}
+</urlset> \ No newline at end of file
diff --git a/src/util/timestamp.ts b/src/util/timestamp.ts
index 01b78fb..39683cc 100644
--- a/src/util/timestamp.ts
+++ b/src/util/timestamp.ts
@@ -63,3 +63,10 @@ export function getTimestamp(seconds: number): string {
return dateArr.map(encodeBase62).join(".") + "-0";
}
+
+/**
+ * Returns a date in the ISO-8601 format
+ */
+export function formatDate(date: Date): string {
+ return date.getUTCFullYear() + "-" + (date.getUTCMonth() + 1) + "-" + date.getUTCDate();
+}