kylenstone / kylethinks

professional blog
0 stars 0 forks source link

add robots.txt #22

Open kylenstone opened 6 years ago

kylenstone commented 6 years ago

see the rise of the smiths:

At it's core, a plugin is a function that takes some configuration parameters, and then produces another function. The signature of that function is like this:

function(files, metalsmith, done)

The files object is an object in which every key represents a path in a virtual file system, and its value an object that contains both metadata and the actual contents, as a Buffer, stored under the contents key.

So a simple example adding a robots.txt file would look like this:

function robots(opts) {
  return function(files, metalsmith, done) {
    fs.readFile(opts.source, function(err, data) {
      files['robots.txt'] = {
        contents: data
      };
      done();
    });
  };
}

Here's how you add that plugin:

metalsmith(__dirname)
  .use(markdown())
  .use(robots({
    source: 'sample-robots.txt'
  })
  .destination('./build');

In this particular example, I've added the markdown plugin (one that is installed with npm install -S metalsmith-markdown), and also added the example robots.txt plugin I created a minute ago. The destination(…) call makes sure all plugins run in order, and the result is written to de destination folder. Alternatively, you could also use the metalsmith-serve plugin to serve the web site directly.