diff options
| author | Mhykol <mchaeldonald62@pm.me> | 2024-10-13 02:51:51 -0400 |
|---|---|---|
| committer | Mhykol <mchaeldonald62@pm.me> | 2024-10-13 02:51:51 -0400 |
| commit | 611aa084110b24dc00757bc02f647f1675df8dda (patch) | |
| tree | fa34014f84210a33727d06f6d108084518345e41 | |
| parent | e8996373a6dad0bdd6fc6926386b4262730ac279 (diff) | |
Added sitemap
| -rw-r--r-- | app.js | 9 | ||||
| -rw-r--r-- | source/readfile.js | 19 |
2 files changed, 28 insertions, 0 deletions
@@ -14,6 +14,7 @@ const mime = { html: 'text/html', txt: 'text/plain', css: 'text/css', + xml: 'text/xml', gif: 'image/gif', jpg: 'image/jpeg', png: 'image/png', @@ -78,7 +79,15 @@ class App { this.data = this.readfile.GetRobots() this.data.mime.then(output => this.#FileOpen({data: this.data, mime: output, res: res})) break + case 'sitemap.xml': + this.data = this.readfile.Sitemap({keys: Array.from(output.keys()), url: process.env.baseUrl}) + this.data.then(data => { + res.set('Content-Type', mime.xml) + res.send(data) + }) + break case 'rss': + res.set('Content-Type', mime.xml) rss.GetFeed({ title: process.env.rssTitle, description: process.env.rssDescription, diff --git a/source/readfile.js b/source/readfile.js index 9310b8c..def56a1 100644 --- a/source/readfile.js +++ b/source/readfile.js @@ -1,5 +1,7 @@ const fs = require('fs') const path = require('path') +const { SitemapStream, streamToPromise } = require('sitemap') +const { Readable } = require('stream') const logger = require('./logger')() /* @@ -71,6 +73,23 @@ class ReadFile { } } } + async Sitemap(x) { + const object = { + keys: x.keys, + links: [], + remove: ['/', '/blog'], + stream: new SitemapStream({hostname: x.url}) + } + + Array.from(object.remove).forEach(url => { + object.keys.splice(object.keys.indexOf(url), 1) + object.links.push({url: url, changefreq: 'daily', priority: 1}) + }) + + object.keys.forEach(link => object.links.push({url: link, changefreq: 'weekly', priority: 0.8})) + + return streamToPromise(Readable.from(object.links).pipe(object.stream)).then(data => {return data.toString()}) + } GetFile(x) { const path = x.split('/') const object = { |
