Skip to content

Commit

Permalink
Generate a sitemap (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin authored Jul 9, 2023
1 parent 13e9166 commit e19128c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion development/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,39 @@ class SVGFile extends ImageFile {
}
}

class SitemapFile extends DiskFile {
constructor (build) {
super(null);
this.getDiskPath = null;
this.build = build;
}

getType () {
return '.xml';
}

read () {
let xml = '';
xml += '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';

xml += Object.keys(this.build.files)
.filter(file => file.endsWith('.html'))
.map(file => file.replace('index.html', '').replace('.html', ''))
.sort((a, b) => {
if (a.length < b.length) return -1;
if (a.length > b.length) return 1;
return a - b;
})
.map(path => `https://extensions.turbowarp.org${path}`)
.map(absoluteURL => `<url><loc>${absoluteURL}</loc></url>`)
.join('\n');

xml += '</urlset>\n';
return xml;
}
}

const IMAGE_FORMATS = new Map();
IMAGE_FORMATS.set('.png', ImageFile);
IMAGE_FORMATS.set('.jpg', ImageFile);
Expand All @@ -116,7 +149,7 @@ class Build {
}

getFile (path) {
return this.files[path] || this.files[`${path}index.html`] || null;
return this.files[path] || this.files[`${path}.html`] || this.files[`${path}index.html`] || null;
}

export (root) {
Expand Down Expand Up @@ -192,6 +225,8 @@ class Builder {
build.files[oldPath] = build.files[newPath];
}

build.files['/sitemap.xml'] = new SitemapFile(build);

const mostRecentExtensions = extensionFiles
.sort((a, b) => b.getLastModified() - a.getLastModified())
.slice(0, 5)
Expand Down

0 comments on commit e19128c

Please sign in to comment.