Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Mar 9, 2016
0 parents commit 299d1eb
Show file tree
Hide file tree
Showing 49 changed files with 412 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "airbnb/base",
"rules": {
"comma-dangle": "off",
"func-names": "off"
}
}
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Automatically normalize line endings for all text-based files
# http://git-scm.com/docs/gitattributes#_end_of_line_conversion
* text=auto

# For the following file types, normalize line endings to LF on
# checkin and prevent conversion to CRLF when they are checked out
# (this is required in order to prevent newline related issues like,
# for example, after the build script is run)
.* text eol=lf
*.css text eol=lf
*.html text eol=lf
*.js text eol=lf
*.json text eol=lf
*.md text eol=lf
*.sh text eol=lf
*.txt text eol=lf
*.xml text eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sudo: false
language: node_js
node_js:
- node
script:
- npm run lint
- npm run test
3 changes: 3 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1.0.0 - March 9, 2016
---------------------
* Initial release
79 changes: 79 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# metalsmith-robots

[![npm version][version-badge]][version-url]
[![build status][build-badge]][build-url]
[![dependency status][dependency-badge]][dependency-url]
[![devdependency status][devdependency-badge]][devdependency-url]
[![downloads][downloads-badge]][downloads-url]

> A metalsmith plugin for generating a robots.txt file
[![stack overflow][stackoverflow-badge]][stackoverflow-url]
[![slack chat][slack-badge]][slack-url]

This plugin allows you to generate a robots.txt file. It accepts global options, and can be triggered from a file's frontmatter with the `public` and `private` keywords. Works well with [metalsmith-mapsite](https://github.com/superwolff/metalsmith-mapsite), as that also accepts setting a page to private from the frontmatter.

For support questions please use [stack overflow][stackoverflow-url] or the metalsmith [slack channel][slack-url].

## Installation

```
$ npm install metalsmith-robots
```

## Example

Configuration in `metalsmith.json`:

```json
{
"plugins": {
"metalsmith-robots": {
"useragent": "googlebot",
"allow": ["index.html", "about.html"],
"disallow": ["404.html"],
"sitemap": "https://www.site.com/sitemap.xml"
}
}
}
```

Which will generate the following robots.txt:

```
User-agent: googlebot
Allow: index.html
Allow: about.html
Disallow: 404.html
Sitemap: https://www.site.com/sitemap.xml
```

## Options

You can pass options to `metalsmith-robots` with the [Javascript API](https://github.com/segmentio/metalsmith#api) or [CLI](https://github.com/segmentio/metalsmith#cli). The options are:

* `useragent`: the useragent - String, default: `*`
* `allow`: an array of the url(s) to allow - Array of Strings
* `disallow`: an array of the url(s) to disallow - Array of Strings
* `sitemap`: the sitemap url - String

Besides these options, settings `public: true` or `private: true` in a file's frontmatter will add that page to the `allow` or `disallow` option respectively. `metalsmith-robots` expects at least one of the last three options, without them it will not generate a robots.txt.

## License

MIT

[build-badge]: https://travis-ci.org/superwolff/metalsmith-robots.svg
[build-url]: https://travis-ci.org/superwolff/metalsmith-robots
[dependency-badge]: https://david-dm.org/superwolff/metalsmith-robots.svg
[dependency-url]: https://david-dm.org/superwolff/metalsmith-robots
[devdependency-badge]: https://david-dm.org/superwolff/metalsmith-robots/dev-status.svg
[devdependency-url]: https://david-dm.org/superwolff/metalsmith-robots#info=devDependencies
[downloads-badge]: https://img.shields.io/npm/dm/metalsmith-robots.svg
[downloads-url]: https://www.npmjs.com/package/metalsmith-robots
[slack-badge]: https://img.shields.io/badge/Slack-Join%20Chat%20→-blue.svg
[slack-url]: http://metalsmith-slack.herokuapp.com/
[stackoverflow-badge]: https://img.shields.io/badge/stack%20overflow-%23metalsmith-red.svg
[stackoverflow-url]: http://stackoverflow.com/questions/tagged/metalsmith
[version-badge]: https://img.shields.io/npm/v/metalsmith-robots.svg
[version-url]: https://www.npmjs.com/package/metalsmith-robots
86 changes: 86 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable no-param-reassign */
'use strict';

/**
* Dependencies
*/
const debug = require('debug')('metalsmith-robots');
const robotize = require('robotize');

/**
* Metalsmith plugin for generating a robots.txt file.
*
* @param {Object} options
* @property {String} useragent (optional)
* @property {Array} allow
* @property {Array} disallow
* @property {String} sitemap
* @return {Function}
*/
module.exports = function plugin(opts) {
/**
* Init
*/
opts = opts || {};

// Create local arrays for allowed and disallowed pages
const allow = [];
const disallow = [];

/**
* Main plugin function
*/
return function (files, metalsmith, done) {
/**
* Check for files with `public: true` in their metadata
*/
for (const file in files) {
if (files[file].public) {
debug(`file marked as public: ${file}`);
allow.push(file);
}
}

// Add allowed pages to options
if (allow.length) {
if (opts.allow) {
opts.allow = opts.allow.concat(allow);
} else {
opts.allow = allow;
}
}

/**
* Check for files with `private: true` in their metadata
*/
for (const file in files) {
if (files[file].private) {
debug(`file marked as private: ${file}`);
disallow.push(file);
}
}

// Add allowed pages to options
if (disallow.length) {
if (opts.disallow) {
opts.disallow = opts.disallow.concat(disallow);
} else {
opts.disallow = disallow;
}
}

robotize(opts, (err, robots) => {
if (err) {
debug('skipping creation of robots.txt');
done();
} else {
debug('creating robots.txt');
// Create file
files['robots.txt'] = {
contents: new Buffer(robots)
};
done();
}
});
};
};
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "metalsmith-robots",
"author": "superwolff <[email protected]> (http://superwolff.nl/)",
"description": "A metalsmith plugin for generating a robots.txt file",
"repository": "git://github.com/superwolff/metalsmith-robots.git",
"version": "1.0.0",
"license": "MIT",
"main": "lib/index.js",
"scripts": {
"test": "mocha --reporter spec"
},
"dependencies": {
"debug": "^2.2.0",
"robotize": "^1.0.0"
},
"devDependencies": {
"assert-dir-equal": "^1.0.1",
"eslint": "^2.3.0",
"eslint-config-airbnb": "^6.1.0",
"eslint-plugin-react": "^4.2.1",
"metalsmith": "^2.1.0",
"mocha": "^2.4.5"
}
}
1 change: 1 addition & 0 deletions test/fixtures/allow/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
3 changes: 3 additions & 0 deletions test/fixtures/allow/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Allow: allowed-1.html
Allow: allowed-2.html
1 change: 1 addition & 0 deletions test/fixtures/allow/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
3 changes: 3 additions & 0 deletions test/fixtures/allow/expected/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Allow: allowed-1.html
Allow: allowed-2.html
1 change: 1 addition & 0 deletions test/fixtures/allow/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
1 change: 1 addition & 0 deletions test/fixtures/disallow/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
3 changes: 3 additions & 0 deletions test/fixtures/disallow/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Disallow: disallowed-1.html
Disallow: disallowed-2.html
1 change: 1 addition & 0 deletions test/fixtures/disallow/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
3 changes: 3 additions & 0 deletions test/fixtures/disallow/expected/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Disallow: disallowed-1.html
Disallow: disallowed-2.html
1 change: 1 addition & 0 deletions test/fixtures/disallow/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
1 change: 1 addition & 0 deletions test/fixtures/no-empty/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
1 change: 1 addition & 0 deletions test/fixtures/no-empty/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
1 change: 1 addition & 0 deletions test/fixtures/no-empty/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
1 change: 1 addition & 0 deletions test/fixtures/private/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index page
1 change: 1 addition & 0 deletions test/fixtures/private/build/nested/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nested page
3 changes: 3 additions & 0 deletions test/fixtures/private/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Disallow: index.html
Disallow: nested/index.html
1 change: 1 addition & 0 deletions test/fixtures/private/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index page
1 change: 1 addition & 0 deletions test/fixtures/private/expected/nested/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nested page
3 changes: 3 additions & 0 deletions test/fixtures/private/expected/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Disallow: index.html
Disallow: nested/index.html
4 changes: 4 additions & 0 deletions test/fixtures/private/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
private: true
---
Index page
4 changes: 4 additions & 0 deletions test/fixtures/private/src/nested/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
private: true
---
Nested page
1 change: 1 addition & 0 deletions test/fixtures/public/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index page
1 change: 1 addition & 0 deletions test/fixtures/public/build/nested/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nested page
3 changes: 3 additions & 0 deletions test/fixtures/public/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Allow: index.html
Allow: nested/index.html
1 change: 1 addition & 0 deletions test/fixtures/public/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index page
1 change: 1 addition & 0 deletions test/fixtures/public/expected/nested/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nested page
3 changes: 3 additions & 0 deletions test/fixtures/public/expected/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Allow: index.html
Allow: nested/index.html
4 changes: 4 additions & 0 deletions test/fixtures/public/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
public: true
---
Index page
4 changes: 4 additions & 0 deletions test/fixtures/public/src/nested/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
public: true
---
Nested page
1 change: 1 addition & 0 deletions test/fixtures/sitemap/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
2 changes: 2 additions & 0 deletions test/fixtures/sitemap/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Sitemap: https://www.site.com/sitemap.xml
1 change: 1 addition & 0 deletions test/fixtures/sitemap/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
2 changes: 2 additions & 0 deletions test/fixtures/sitemap/expected/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Sitemap: https://www.site.com/sitemap.xml
1 change: 1 addition & 0 deletions test/fixtures/sitemap/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
1 change: 1 addition & 0 deletions test/fixtures/useragent/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
2 changes: 2 additions & 0 deletions test/fixtures/useragent/build/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: googlebot
Sitemap: https://www.site.com/sitemap.xml
1 change: 1 addition & 0 deletions test/fixtures/useragent/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
2 changes: 2 additions & 0 deletions test/fixtures/useragent/expected/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: googlebot
Sitemap: https://www.site.com/sitemap.xml
1 change: 1 addition & 0 deletions test/fixtures/useragent/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index.html
Loading

0 comments on commit 299d1eb

Please sign in to comment.