Generate SEO files (robots.txt and sitemap.xml)

pull/192/head
Andy Castille 4 years ago
parent d899528ecb
commit 5ce7937f6f

@ -280,6 +280,12 @@ const OPTIONS = {
type: 'string',
'default': 'en'
},
'seo-location': {
group: 'Website options:',
description: 'Location where the site will be hosted. If provided, sitemap.xml and robots.txt will be created.',
type: 'string',
'default': null
},
// ------------------------------------
// Misc options

@ -40,6 +40,8 @@ exports.build = function (rootAlbum, opts, callback) {
next => theme.prepare(next),
next => async.parallel(tasks, next)
], callback)
outputSEO(opts.output, opts.seoLocation, rootAlbum)
}
function createRenderingTasks (theme, templateData, currentAlbum, breadcrumbs) {
@ -79,3 +81,39 @@ function readThemeSettings (filepath) {
throw new Error('Failed to parse JSON theme settings file: ' + filepath)
}
}
function outputSEO (output, seoLocation, rootAlbum) {
// SEO (sitemap and robots.txt)
if (!seoLocation) {
return
}
// Guaranteed to end with slash
const seoPrefix = seoLocation + (seoLocation.endsWith('/') ? '' : '/')
// Generate robots.txt
const robotsTxt = 'User-Agent: *\nDisallow:\n\nSitemap: ' + seoPrefix + 'sitemap.xml\n'
fs.writeFile(path.join(output, 'robots.txt'), robotsTxt, () => {
})
// Generate sitemap
let sitemapXML = '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
const now = new Date().toISOString()
function addToSitemap (album) {
sitemapXML +=
' <url>\n' +
' <loc>' + seoPrefix + album.url + '</loc>\n' +
' <lastmod>' + now + '</lastmod>\n' +
' </url>\n'
album.albums.forEach((subAlbum) => addToSitemap(subAlbum))
}
addToSitemap(rootAlbum)
sitemapXML += '</urlset>\n'
fs.writeFile(path.join(output, 'sitemap.xml'), sitemapXML, () => {
})
}

Loading…
Cancel
Save