UNPKG

sitemap-generator-static-suite

Version:

Generador de sitemaps para proyectos static-suite que emplear un iterador sobre todos los contenidos creados al más puro estilo Bidasoa.

60 lines (59 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sitemapProcessor = void 0; var sitemapData = []; var domain = ''; var sitemapDataIds = new Map(); // Use a function to avoid conf to be instantiated when this file is required. var getDomain = function () { return domain; }; var setDomain = function (value) { return domain = value; }; var setSitemapData = function (node) { var nodeData = { id: node.data.content.id, changed: node.data.content.changed, content: "<url><loc>".concat("".concat(getDomain()).concat(node.data.content.url.path), "</loc><lastmod>").concat(new Date((node.data.content.changed || 1546344000) * 1000) .toISOString() .replace('000Z', '000000Z'), "</lastmod></url>"), }; sitemapData.push(nodeData); sitemapDataIds.set(nodeData.id, true); }; var sitemapHeader = "<?xml version='1.0' encoding='utf-8'?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">"; var sitemapFooter = '</urlset>'; var sitemapIndexHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"; var sitemapIndexFooter = '</sitemapindex>'; exports.sitemapProcessor = { getSitemapData: function () { var sitemaps = []; var sitemapIndex = 1; sitemaps[sitemapIndex] = []; var maxItems = 2000; var indexItems = []; indexItems.push("<sitemap><loc>".concat(getDomain(), "/sitemap/sitemap.").concat(sitemapIndex, ".xml</loc></sitemap>")); sitemapData .sort(function (a, b) { return b.changed - a.changed; }) .forEach(function (item, index) { sitemaps[sitemapIndex].push(item.content); if ((index + 1) % maxItems === 0) { sitemapIndex += 1; indexItems.push("<sitemap><loc>".concat(getDomain(), "/sitemap/sitemap.").concat(sitemapIndex, ".xml</loc></sitemap>")); sitemaps[sitemapIndex] = []; } }); return { index: "".concat(sitemapIndexHeader).concat(indexItems.join('')).concat(sitemapIndexFooter), sitemaps: sitemaps.map(function (sitemap) { return "".concat(sitemapHeader).concat(sitemap.join('')).concat(sitemapFooter); }), }; }, addStaticNode: function (data) { setSitemapData(data); }, processNode: function (node) { setSitemapData(node); return node; }, setDomain: setDomain, bootstrap: function () { sitemapData = []; }, };