Code Monkey home page Code Monkey logo

metadata's Introduction

Metalsmith

npm: version ci: build code coverage license: MIT Gitter chat

An extremely simple, pluggable static site generator for NodeJS.

In Metalsmith, all of the logic is handled by plugins. You simply chain them together.

Here's what the simplest blog looks like:

import { fileURLToPath } from 'node:url'
import { dirname } from 'path'
import Metalsmith from 'metalsmith'
import layouts from '@metalsmith/layouts'
import markdown from '@metalsmith/markdown'

const __dirname = dirname(fileURLToPath(import.meta.url))

Metalsmith(__dirname)
  .use(markdown())
  .use(
    layouts({
      pattern: '**/*.html'
    })
  )
  .build(function (err) {
    if (err) throw err
    console.log('Build finished!')
  })

Installation

NPM:

npm install metalsmith

Yarn:

yarn add metalsmith

Quickstart

What if you want to get fancier by hiding unfinished drafts, grouping posts in collections, and using custom permalinks? Just add plugins...

import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import Metalsmith from 'metalsmith'
import collections from '@metalsmith/collections'
import layouts from '@metalsmith/layouts'
import markdown from '@metalsmith/markdown'
import permalinks from '@metalsmith/permalinks'
import drafts from '@metalsmith/drafts'

const __dirname = dirname(fileURLToPath(import.meta.url))
const t1 = performance.now()
const devMode = process.env.NODE_ENV === 'development'

Metalsmith(__dirname) // parent directory of this file
  .source('./src') // source directory
  .destination('./build') // destination directory
  .clean(true) // clean destination before
  .env({
    // pass NODE_ENV & other environment variables
    DEBUG: process.env.DEBUG,
    NODE_ENV: process.env.NODE_ENV
  })
  .metadata({
    // add any variable you want & use them in layout-files
    sitename: 'My Static Site & Blog',
    siteurl: 'https://example.com/',
    description: "It's about saying »Hello« to the world.",
    generatorname: 'Metalsmith',
    generatorurl: 'https://metalsmith.io/'
  })
  .use(drafts(devMode)) // only include drafts when NODE_ENV === 'development'
  .use(
    collections({
      // group all blog posts by adding key
      posts: 'posts/*.md' // collections:'posts' to metalsmith.metadata()
    })
  ) // use `collections.posts` in layouts
  .use(
    markdown({
      // transpile all md file contents into html
      keys: ['description'], // and also file.description
      globalRefs: {
        // define links available to all markdown files
        home: 'https://example.com'
      }
    })
  )
  .use(permalinks()) // change URLs to permalink URLs
  .use(
    layouts({
      // wrap layouts around html
      pattern: '**/*.html'
    })
  )
  .build((err) => {
    // build process
    if (err) throw err // error handling is required
    console.log(`Build success in ${((performance.now() - t1) / 1000).toFixed(1)}s`)
  })

How does it work?

Metalsmith works in three simple steps:

  1. Read all the files in a source directory.
  2. Invoke a series of plugins that manipulate the files.
  3. Write the results to a destination directory!

Each plugin is invoked with the contents of the source directory, and each file can contain YAML front-matter that will be attached as metadata, so a simple file like...

---
title: A Catchy Title
date: 2024-01-01
---
An informative article.

...would be parsed into...

{
  'path/to/my-file.md': {
    title: 'A Catchy Title',
    date: new Date(2024, 1, 1),
    contents: Buffer.from('An informative article'),
    stats: fs.Stats
  }
}

...which any of the plugins can then manipulate however they want. Writing plugins is incredibly simple, just take a look at the example drafts plugin.

Of course they can get a lot more complicated too. That's what makes Metalsmith powerful; the plugins can do anything you want!

Plugins

A Metalsmith plugin is a function that is passed the file list, the metalsmith instance, and a done callback. It is often wrapped in a plugin initializer that accepts configuration options.

Check out the official plugin registry at: https://metalsmith.io/plugins.
Find all the core plugins at: https://github.com/search?q=org%3Ametalsmith+metalsmith-plugin
See the draft plugin for a simple plugin example.

API

Check out the full API reference at: https://metalsmith.io/api.

CLI

In addition to a simple Javascript API, the Metalsmith CLI can read configuration from a metalsmith.json file, so that you can build static-site generators similar to Jekyll or Hexo easily. The example blog above would be configured like this:

metalsmith.json

{
  "source": "src",
  "destination": "build",
  "clean": true,
  "metadata": {
    "sitename": "My Static Site & Blog",
    "siteurl": "https://example.com/",
    "description": "It's about saying »Hello« to the world.",
    "generatorname": "Metalsmith",
    "generatorurl": "https://metalsmith.io/"
  },
  "plugins": [
    { "@metalsmith/drafts": true },
    { "@metalsmith/collections": { "posts": "posts/*.md" } },
    { "@metalsmith/markdown": true },
    { "@metalsmith/permalinks": "posts/:title" },
    { "@metalsmith/layouts": true }
  ]
}

Then run:

metalsmith

# Metalsmith · reading configuration from: /path/to/metalsmith.json
# Metalsmith · successfully built to: /path/to/build

Options recognised by metalsmith.json are source, destination, concurrency, metadata, clean and frontmatter. Checkout the static site, Jekyll examples to see the CLI in action.

Local plugins

If you want to use a custom plugin, but feel like it's too domain-specific to be published to the world, you can include plugins as local npm modules: (simply use a relative path from your root directory)

{
  "plugins": [{ "./lib/metalsmith/plugin.js": true }]
}

The secret...

We often refer to Metalsmith as a "static site generator", but it's a lot more than that. Since everything is a plugin, the core library is just an abstraction for manipulating a directory of files.

Which means you could just as easily use it to make...

Resources

Troubleshooting

Set metalsmith.env('DEBUG', '*metalsmith*') to debug your build. This will log debug logs for all plugins using the built-in metalsmith.debug debugger. For older plugins using debug directly, run your build with export DEBUG=metalsmith-*,@metalsmith/* (Linux) or set DEBUG=metalsmith-*,@metalsmith/* for Windows.

Node Version Requirements

Future Metalsmith releases will at least support the oldest supported Node LTS versions.

Metalsmith 2.6.x supports NodeJS versions 14.18.0 and higher.
Metalsmith 2.5.x supports NodeJS versions 12 and higher.
Metalsmith 2.4.x supports NodeJS versions 8 and higher.
Metalsmith 2.3.0 and below support NodeJS versions all the way back to 0.12.

Compatibility & support policy

Metalsmith is supported on all common operating systems (Windows, Linux, Mac). Metalsmith releases adhere to semver (semantic versioning) with 2 minor gray-area exceptions for what could be considered breaking changes:

  • Major Node version support for EOL (End of Life) versions can be dropped in minor releases
  • If a change represents a major improvement that is backwards-compatible with 99% of use cases (not considering outdated plugins), they will be considered eligible for inclusion in minor version updates.

Credits

Special thanks to Ian Storm Taylor, Andrew Meyer, Dominic Barnes, Andrew Goodricke, Ismay Wolff, Kevin Van Lierde and others for their contributions!

metadata's People

Contributors

deltamualpha avatar doingweb avatar fhemberger avatar ianstormtaylor avatar lambtron avatar robloach avatar thangngoc89 avatar webketje avatar wernerglinka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

metadata's Issues

package.json as metadata

I'm considering writing a separate plugin to import package.json as metadata. Would it be a better to just fork the repo and add it?

Possible to get example of use

I have the following, as shown in the readme:

.use(metadata({
templatedata: 'templatedata.yaml'
}))

and

templatedata.yaml in the 'src' directory (containing varname: value)

But varname when used in a template is not recognised

I think I just need to do more, but I can not see an example of use (and that is something that can help others too).

EDIT: tried also with .json and json data form, same result. No global metadata (at least not available to templates)

Make metadata file locations clearer

It would be good if the Readme made it clearer that the metadata files in question need to be loaded in the metalsmith 'tree', e.g. in your src directory if using default options. Took me a while to figure this out - had to look at the source.

As you invoke metalsmith on __dirname I think you could reasonably expect it to find metadata files relative to there (I did).

You might also expect absolute file paths to work (perhaps if invoking from some higher-level tool, gulp etc) and of course they don't for the same reason. I think this is reasonable, but should probably be noted as well.

Thanks!

Preprocess markdown in metadata?

It'd be great if there were an option to preprocess markdown in metadata. At the moment it's a difficult thing to do with metalsmith.

yaml not parsed if used with json

my metalsmith.json:

"metalsmith-metadata": {
    "site": "json/site.json",
    "author": "json/author.json",
    "photos": "yaml/photos.yaml",
    "works": "yaml/works.yaml"
}

site, author parse correctly and are being passed to Handlebars, but yaml files are not. i have checked all files (both .json and .yaml) with JSONLint and YAMLint, and they are all valid.

i even tried parsing only one yaml file, but it didn't work. any ideas?

using metadata in build script

Some metadata imported from a json file could be useful in the build script, for example as a parameter for some other modules.

Can we use metadata in build script ? If yes, how ?

In my case, I need this feature when I use the metalsmith-feed plugin :

  • I define the path of the rss feed in my json metadata file
  • I use this path in my handlebars template, to offer a link to my users in the web pages' footer
  • I have to redefine the path of the rss feed as a parameter of metalsmith-feed which is accepting a destination parameter that represent the file path to write the rendered XML feed

I want to declare the path once in the json file of metadata and use the var at both places :

  • the handlebars template of my footer (to give users a link to the rss feed)
  • the metalsmith-feed configuration file in my build script

Publish to NPM

I've been migrating the PlayCanvas Developer Site to the shiny new @metalsmith-prefixed NPM packages. Great to see these being updated! Is the metadata plugin going to be published to NPM as @metalsmith/metadata soon?

cannot find file?

i get the error:

  Metalsmith · file "./config.yml" not found

in my metalsmith.json file i point to the config file as

    "metalsmith-metadata": {
      "config": "./config.yml"
    },

I have the config.yml file in the root directory

Add built-in support for CSV/TSV

Use case:

Excel/Google sheet worksheets saved as CSV/TSV

  • Use https://www.npmjs.com/package/csv-parse as optional peerDependency (similar to toml implementation)
  • CSV can be parsed either as array of arrays or array of objects. A CSV is not guaranteed to have a header row so array of arrays is safer.
  • will require being able to specify options at some point (breaking change with current)

Using metadata in Handlebars helper functions

I was writing a question about this, but did some more research and figured it out. It wasn't obvious to me, so I'm assuming someone else might be struggle with it too.

If you want to use metadata (global or imported) in a Handlebars helper function, include options as your last parameter. Then options.data.root will be the global metadata object. If imported a key foo with metalsmith-metadata, you can access it as options.data.root.foo.

Assume foo.json is imported to foo

{
    "bar": "Hello, World!"
}

helper, registered as 'foobar'

module.exports = function (parm, options) {
    var foo = options.data.root.foo;
    return(foo.bar);

Template

{{foobar}}

will produce output

Hello, World!

You can also access any file-level metadata in options.data.root, for example if you have a title key in your document, options.data.root.title

You can get a sense of what's in options.data.root by registering the following helper and calling it in a template.

module.exports = function (filename, options) {
    var md = options.data.root;
    for (i in md) {
        console.log(i + " | " + md[i]);
    }
};

This will log keys and values. Note that some values will be objects, but they should be obvious (partials, knownHelpers, etc.).

Metalsmith team
I'm not sure exactly where this information should go. It's related to using metalsmith-metadata, but also general to metalsmith (global metadata). It's focused on Handlebars helpers, but may apply to other templating engines. Tell me where you want this documented and I'll try to make it happen.

Is this project being maintained?

cc: @ianstormtaylor
I just started using metalsmith (and I really like it), but it seems like some of the plugins aren't being maintained. Do you guys need any help? Seems like people using this are running into some of the same issues I did.

Incompability with metalsmith-watch

Hello,

I'm encountering an issue when using this plugin along with the metalsmith-watch plugin.

The problem is that the metalsmith-watch plugin calls the middleware stack and only passes the files that actually changed (which is the expected behavior). Therefore when metalsmith-metadata try to loads the desired metadata files, they do not exists anymore (unless they have been edited). And so it throws an error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.