mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
01e833376b
refs https://github.com/TryGhost/Team/issues/1044 refs https://github.com/TryGhost/Ghost/pull/13298 - This splits the sitemaps according to the limit set by Google https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps Co-authored-by: - Kevin Ansfield (@kevinansfield)
57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
const _ = require('lodash');
|
|
const xml = require('xml');
|
|
const moment = require('moment');
|
|
const urlUtils = require('../../../shared/url-utils');
|
|
const localUtils = require('./utils');
|
|
|
|
const XMLNS_DECLS = {
|
|
_attr: {
|
|
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
|
|
}
|
|
};
|
|
|
|
class SiteMapIndexGenerator {
|
|
constructor(options) {
|
|
options = options || {};
|
|
this.types = options.types;
|
|
this.maxPerPage = options.maxPerPage;
|
|
}
|
|
|
|
getXml() {
|
|
const urlElements = this.generateSiteMapUrlElements();
|
|
|
|
const data = {
|
|
// Concat the elements to the _attr declaration
|
|
sitemapindex: [XMLNS_DECLS].concat(urlElements)
|
|
};
|
|
|
|
// Return the xml
|
|
return localUtils.getDeclarations() + xml(data);
|
|
}
|
|
|
|
generateSiteMapUrlElements() {
|
|
return _.map(this.types, (resourceType) => {
|
|
// `|| 1` = even if there are no items we still have an empty sitemap file
|
|
const noOfPages = Math.ceil(Object.keys(resourceType.nodeLookup).length / this.maxPerPage) || 1;
|
|
const pages = [];
|
|
|
|
for (let i = 0; i < noOfPages; i++) {
|
|
const page = i === 0 ? '' : `-${i + 1}`;
|
|
const url = urlUtils.urlFor({relativeUrl: '/sitemap-' + resourceType.name + page + '.xml'}, true);
|
|
const lastModified = resourceType.lastModified;
|
|
|
|
pages.push({
|
|
sitemap: [
|
|
{loc: url},
|
|
{lastmod: moment(lastModified).toISOString()}
|
|
]
|
|
});
|
|
}
|
|
|
|
return pages;
|
|
}).flat();
|
|
}
|
|
}
|
|
|
|
module.exports = SiteMapIndexGenerator;
|