Merge pull request #192 from Klikini/master

Generate SEO files (robots.txt and sitemap.xml)
pull/195/head
Romain 4 years ago committed by GitHub
commit 6c9f01e8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,37 @@ 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.writeFileSync(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.writeFileSync(path.join(output, 'sitemap.xml'), sitemapXML)
}

Loading…
Cancel
Save