-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an initial version of sitemap generation.
- Loading branch information
Aaron Boushley
committed
Jul 2, 2014
0 parents
commit cd7915b
Showing
7 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# Deployed apps should consider commenting this line out: | ||
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 ExtraHop Networks | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
metalsmith-sitemap | ||
========================== | ||
|
||
This provides a plugin to generate a sitemap according to the | ||
[Sitemaps Protocol](http://www.sitemaps.org/protocol.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
var fs = require('fs'), | ||
Handlebars = require('handlebars'), | ||
_ = require('lodash'), | ||
defaultsDeep = _.partialRight(_.merge, function deep(value, other) { | ||
return _.merge(value, other, deep); | ||
}), | ||
resolve = function(object, property) { | ||
return _.reduce(property.split('.'), function(o, p) { | ||
return o ? o[p] : undefined; | ||
}, object); | ||
}; | ||
|
||
module.exports = plugin; | ||
|
||
function plugin(options) { | ||
options = options || {}; | ||
|
||
var templatesDir = __dirname + '/templates'; | ||
|
||
defaultsDeep(options, { | ||
ignoreFiles: [], | ||
output: 'sitemap.xml', | ||
modifiedProperty: 'modified', | ||
urlProperty: 'path', | ||
entryTemplate: templatesDir + '/entry.xml', | ||
sitemapTemplate: templatesDir + '/sitemap.xml', | ||
defaults: { | ||
priority: 0.5, | ||
changefreq: 'daily' | ||
} | ||
}); | ||
|
||
var entryTemplate = fs.readFileSync(options.entryTemplate, {encoding: 'utf8'}); | ||
var sitemapTemplate = fs.readFileSync(options.sitemapTemplate, {encoding: 'utf8'}); | ||
|
||
entryTemplate = Handlebars.compile(entryTemplate); | ||
sitemapTemplate = Handlebars.compile(sitemapTemplate); | ||
|
||
return function(files, metalsmith, done) { | ||
var entries, | ||
entry, | ||
file, | ||
data; | ||
|
||
entries = _(Object.keys(files)).map(function (file) { | ||
data = files[file]; | ||
|
||
if (!shouldIgnore(file) || data.private) { | ||
return; | ||
} | ||
|
||
data.sitemap = data.sitemap || {}; | ||
|
||
entry = _.defaults({ | ||
loc: resolve(data, options.urlProperty), | ||
lastmod: resolve(data, options.modifiedProperty), | ||
changefreq: data.sitemap.changefreq, | ||
priority: data.sitemap.priority | ||
}, options.defaults); | ||
|
||
return entryTemplate(entry); | ||
}).compact().join(''); | ||
|
||
var contents = sitemapTemplate({entries: entries}); | ||
files[options.output] = { | ||
contents: new Buffer(contents) | ||
}; | ||
|
||
done(); | ||
}; | ||
|
||
function shouldIgnore(file) { | ||
return !_.some(options.ignoreFiles, function(ignore) { | ||
return ignore.test(file); | ||
}); | ||
} | ||
} | ||
|
||
function error(file, message) { | ||
return message + '\nFile: ' + file + | ||
'\nTo skip validation on this file add it to the ' + | ||
'ignoreFiles array.\n\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<url> | ||
<loc>{{loc}}</loc>{{#if lastmod}} | ||
<lastmod>{{lastmod}}</lastmod>{{/if}}{{#if changefreq}} | ||
<changefreq>{{changefreq}}</changefreq>{{/if}}{{#if priority}} | ||
<priority>{{priority}}</priority>{{/if}} | ||
</url> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
{{{entries}}} | ||
</urlset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "metalsmith-sitemap", | ||
"version": "0.0.1", | ||
"description": "A metalsmith plugin for generating a sitemap.xml file.", | ||
"main": "lib/index.js", | ||
"files": ["lib/templates"], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ExtraHop/metalsmith-sitemap.git" | ||
}, | ||
"keywords": [ | ||
"metalsmith", | ||
"sitemap" | ||
], | ||
"author": { | ||
"name": "ExtraHop Networks", | ||
"url": "http://www.extrahop.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Aaron Boushley" | ||
} | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ExtraHop/metalsmith-sitemap/issues" | ||
}, | ||
"homepage": "https://github.com/ExtraHop/metalsmith-sitemap", | ||
"dependencies": { | ||
"handlebars": "^2.0.0-alpha.4", | ||
"lodash": "^2.4.1" | ||
} | ||
} |