node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

onessg

onessg

Travis Coveralls branch npm npm

onessg (One Static Site Generator) is the Static Site Generator that does only one thing: compile your html and markdown. It won't minify your JS, concat your CSS, or optimize your images. Why? You most likely already have a favorite tool for doing that.

The Javascript pendulum has swung from restrictive, monolithic frameworks to modular, boilerplate-hindered libraries.

-- @ericclemmons

onessg changes that. We believe in the unix philosophy: do one thing and do it well.

We also believe in setting useful, but overridable defaults. Because of this, onessg requires no configuration files to get started.

Contents

Installation

npm install -D onessg

You will also need to install the jstransformer for your favorite template engine. For example, if you use EJS, you would run:

npm install -D jstransformer-ejs

You can read more about jstransformers here.

Note: We recommend installing onessg as a devDependency (with the -D flag) and running it via an npm script. If you choose to install onessg globally, you will also need to install the jstransformer globally as well.

Example

Assuming the following file/directory structure:

.
├── src/
|   ├── _defaults.yaml
|   └── page-one.md
├── layouts/
|   └── page.ejs
├── dist/
└── package.json

src/page-one.md:

---
title: "My first Page"
_layout: "page"
---
Hello World!

The front-matter (the part between the --- lines) is written in YAML (other languages are supported as well). All keys in the front-matter will be passed as locals to your templates.

Notice the underscore before layout. Anything prefixed with an underscore is reserved word for onessg. See the full list of underscore keys.


You can set defaults for your front-matter in _defaults.yaml (_defaults.json works too!). These defaults can be overridden in your front-matter.

src/_defaults.yaml:

title: "Hello World!" # This title will be used if none is specified 
author: "John Smith"

If you place a _defaults.yaml file in a subdirectory in src/, settings there will only apply to files in that subdirectory and its child subdirectories.


Layouts are written in the templating language of your choice. We are using EJS here, but you can use any template engine that has a jstransformer. You can also use multiple template engines in the same project! onessg will infer the correct template engine from the file extension.

layouts/page.ejs looks like this:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><%= title %></title>
    <meta name="author" content="<%= author %>">
  </head>
  <body>
    <%- _body -%>
  </body>
</html>

Notice the local _body. This is the local for outputing the contents of each file. For page-one.md, it is Hello World!.


Run:

onessg

onessg will compile all the html and markdown files in src/ (and subdirectories), and output them to dist/ (retaining the directory structure):

.
├── src/
|   ├── _defaults.yaml
|   └── page-one.md
├── layouts/
|   └── page.ejs
├── dist/
|   └── page-one.html
└── package.json

dist/page-one.html looks like this:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My first Page</title>
    <meta name="author" content="John Smith">
  </head>
  <body>
    <p>Hello World!</p>
  </body>
</html>

Success!!! 🎉

A few notes:

  • The title (My first Page) comes from the front-matter.
  • The author's name (John Smith) comes from the _defaults.yaml file.

For further reading, see the Tutorial.

CLI Usage & Options

onessg
onessg [-s <source_dir>] [-d <output_dir>] [-l <layout_dir>]
 
Options:
  -s, --src      Set the src directory                [string] [default: "src/"]
  -d, --dist     Set the dist directory              [string] [default: "dist/"]
  -l, --layouts  Set the layouts directory        [string] [default: "layouts/"]
  -c, --config   Set the directory that contains onessg.config.js       [string]
  --help         Show help                                             [boolean]
  --version      Show version number                                   [boolean]
 
Examples:
  onessg
  onessg -s posts/ -d output/ -l templates/
 

onessg.config.js

To pass options to your template engine, you will need to use a config file. Read more about it here.

Contributing

Contributions welcome; please read the Contributing Guidelines for more info.

Check the Roadmap to see what's on the horizon.

License

MIT