-
Notifications
You must be signed in to change notification settings - Fork 226
/
generate-sitemap.js
102 lines (86 loc) · 2.72 KB
/
generate-sitemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const sm = require('sitemap')
const path = require('path')
const paths = require('./paths.js')
const fs = require('fs-extra')
const match = require('multimatch')
// Only include keys that have values
const removeEmptyKeys = (obj) => {
const newObj = {}
Object.keys(obj).forEach((prop) => {
if (obj[prop] !== '') {
newObj[prop] = obj[prop]
}
})
return newObj
}
/**
* Plugin to generate a sitemap with sitemap.js
*
* @param {Object} options
* @property {String} hostname
* @property {String} changefreq (optional)
* @property {Date} lastmod (optional)
* @property {String} priority (optional)
* @property {String} destination (optional)
* @property {String} filename (optional)
* @property {String} pattern (optional)
* @return {Function}
*/
const plugin = (opts) => {
opts = opts || {}
if (!opts.hostname) {
throw new Error('"hostname" is required')
}
// sitemap.js options
const hostname = opts.hostname
const changefreq = opts.changefreq
const lastmod = opts.lastmod
const priority = opts.priority
// additional options
const destination = opts.destination || paths.public
const filename = opts.filename || 'sitemap.xml'
const pattern = opts.pattern || '**/*.html'
return function (files, metalsmith, done) {
const sitemap = sm.createSitemap({
hostname
})
// Checks whether files should be processed
const checkPattern = (file, frontmatter) => {
// Only include files that match the pattern
if (!match(file, pattern)[0]) {
return false
}
// Exclude examples from the sitemap
if (frontmatter.layout && frontmatter.layout.startsWith('layout-example')) {
return false
}
// Ignore files if set to true
if (frontmatter.ignore_in_sitemap) {
return false
}
return true
}
Object.keys(files).sort().forEach(function (file) {
// Get the current file's frontmatter
const frontmatter = files[file]
// Only process files that pass the check
if (!checkPattern(file, frontmatter)) {
return
}
// Use canonical in frontmatter instead of the file's canonical data
// set by metalsmith-canonical plugin
const url = frontmatter.canonical ? frontmatter.canonical : files[file].canonical
const sitemapEntry = {
url,
changefreq: frontmatter.changefreq || changefreq,
lastmod: frontmatter.lastmod || lastmod,
priority: frontmatter.priority || priority
}
// Add the files to the sitemap and remove empty keys
sitemap.add(removeEmptyKeys(sitemapEntry))
})
// create a sitemap
fs.writeFile(path.join(destination, filename), sitemap.toString(), done)
}
}
module.exports = plugin