Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gulp-ssg

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-ssg

Generate a static site with gulpjs

  • 0.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

gulp-ssg NPM version Dependency Status

A gulp plugin to generate a static site.

Usage

var ssg = require('gulp-ssg');
var site = {
    title: 'My site'
};

gulp.task('html', function() {
    return gulp.src('content/**/*.md')
        .pipe(ssg(site))
        .pipe(gulp.dest('public/'));
});

This will rename the files so they have pretty URLs e.g.

content/index.md        -> public/index.html
content/foo.md          -> public/foo/index.html
content/bar/index.md    -> public/bar/index.html
content/bar/hello.md    -> public/bar/hello/index.html

It will also add properties to a meta object of each file:

  • file.meta.url
  • file.meta.isHome
  • file.meta.isIndex
  • file.meta.sectionUrl
  • file.meta.section

Finally, it will add an index property to the passed in site object which is a tree of all the content. The above example would look like:


    {
        name: 'root',
        url: '/',
        files: [<index.html>, <foo/index.html> ] // All files in this section
        sections: [
            {
                name: 'bar',
                url: 'bar',
                files: [<bar/index.html>, <bar/foo/index.html>]
            }
        ]
    }

As implied above each file has a reference back to it's section in this tree.

Example

It gets more interesting when combined with other pipes. For example:

var ssg = require('gulp-ssg');
var frontmatter = require('gulp-front-matter');
var marked = require('gulp-marked');
var site = {
    title: 'My site'
};

gulp.task('html', function() {
    return gulp.src('content/**/*.md')
        .pipe(frontmatter({
            property: 'meta'
        }))
        .pipe(marked())
        .pipe(ssg(site, {
            property: 'meta'
        }))
        .pipe(gulp.dest('public/'));
});

This will extract any YAML front-matter, convert the content of each file from markdown to HTML, then run the ssg. The data extracted from the front-matter will be combined with the data extracted by the ssg in the meta property.

## Templates

A common requirement of static sites is to pass the content through some template engine. There is nothing built into gulp-ssg to do this, but it's very easy to add with another pipe.

After the step above you will have created a bunch of HTML files. Now you can run them through a templating pipe. All the files are processed before the next pipe, so the template will have access to the complete site index for things like generating global navigation, or a list of sub-pages in the current section.

So to add this to the above example:

var ssg = require('gulp-ssg');
var frontmatter = require('gulp-front-matter');
var marked = require('gulp-marked');
var fs = require('fs');
var es = require('event-stream');
var mustache = require('mustache');
var site = {
    title: 'My site'
};

gulp.task('html', function() {

    var template = String(fs.readFileSync('templates/page.html'));

    return gulp.src('content/**/*.md')
        .pipe(frontmatter({
            property: 'meta'
        }))
        .pipe(marked())
        .pipe(ssg(site, {
            property: 'meta'
        }))
        .pipe(es.map(function(file, cb) {
            var html = mustache.render(template, {
                page: file.meta,
                site: site,
                content: String(file.contents)
            });
            file.contents = new Buffer(html);
            cb(null, file);
        }))
        .pipe(gulp.dest('public/'));
});

This uses es.map to modify the stream directly, but if you have a common way of rendering many sites it might be worth writing a little plug-in with a bit more error handling etc.

Caveats

  • Each directory must contain a file with a base name of index (e.g. index.md) to have the site index fully traversed.

Options

baseUrl string

The base URL of the site, defaults to '/'. This should be the path to where your site will eventually be deployed.

sort string

A property to sort pages by in the index, defaults to url. For example, this could be a property like order extracted from the YAML front-matter, giving content editors full control over the order of pages.

property string

The name of the property to attach data to, defaults to meta.

sectionProperties array

A list of properties to extract from index pages to add to the section, defaults to an empty list. For example, you could add a sectionTitle to front-matter in your index.md files, then use this it for link text in your global navigation.

Previewing Your Website

Add a watch task to run a server for previewing your website:

var http = require('http'),
	ecstatic = require('ecstatic');

gulp.task('watch', function() {
	http.createServer(
        ecstatic({ root: __dirname + '/public'  })
    ).listen(8745);
    console.log('Preview at http://localhost:8745');

    gulp.watch('content/', ['html']);
    gulp.watch('templates/', ['default']);
});

Keywords

FAQs

Package last updated on 26 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc