NOTICE: SUPPORT FOR THIS PROJECT HAS ENDED
This projected was owned and maintained by Walmart. This project has reached its end of life and Walmart no longer supports this project.
We will no longer be monitoring the issues for this project or reviewing pull requests. You are free to continue using this project under the license terms or forks of this project at your own risk. This project is no longer subject to Walmart's bug bounty program or other security monitoring.
We recommend you take the following action:
- Review any configuration files used for build automation and make appropriate updates to remove or replace this project
- Notify other members of your team and/or organization of this change
- Notify your security team to help you evaluate alternative options
For security reasons, Walmart does not transfer the ownership of our primary repos on Github or other platforms to other individuals/organizations. Further, we do not transfer ownership of packages for public package management systems.
If you would like to fork this package and continue development, you should choose a new name for the project and create your own packages, build automation, etc.
Please review the licensing terms of this project, which continue to be in effect even after decommission.
Markdown and Handlebars static site generator. Transforms files with .hbs and .md with Handlebars and Markdown respectively.
npm install static
static source.hbs.html target.html
var static = require('./static');
static.transform('source.hbs.html', function(buffer) {
fs.writeFile('target.html', buffer.toString());
});
In your Gruntfile.js:
grunt.loadNpmTasks('static');
Then in your config, define "static" as a multi task:
grunt.initConfig({
static: {
mySite: {
// this file will be included before the
// build is run
require: ['helpers.js'],
build: {
// will treat source as a handlebars
// template and save at target
'target.html': 'source.hbs.html',
// process multiple files into a single file
'api.html': [
'header.hbs.html',
'README.md',
'footer.html'
],
// specify a specific context for the
// handlebars template to be run with
'target2.html': {
file: 'source.hbs.html',
context: {
key: 'value'
}
}
}
}
}
});
In this case, one could invoke: grunt static:mySite to run the build.
require accepts an array of files to require before static runs, each file must export a function that will recieve a static object:
module.exports = function(static) {
};
build accepts a hash of target file: source file pairs to process. If an array of source files is specified each one will be processed individually and concatenated. If compiling with handlebars an object may be passed that should be in this format:
{
file: 'source.hbs.html',
context: {
key: 'value'
}
}
In source.hbs.html {{key}} would be available.
A handlebars file similar to this could be used to generate documentation from a README.md file:
<ul class="toc">
{{#include "README.md" select="h3"}}
<li><a href="#{{id}}">{{innerHTML}}</a></li>
{{/include}}
</ul>
<div class="body">
{{include "README.md"}}
</div>
Include a file. If select is specified a block must be passed. The block will be called once for each selected node (with the context set to the node) from the file and the resulting HTML will be embedded.
Transforms a given file with Handlebars and Markdown if file extensions are present. Calls callback with a buffer containing the transformed file.
A reference to the handlebars object static is using. Useful to register new helpers on.
Just like Handlebars.registerHelper but async. callback recieves arguments to the helper (if any) followed by an options object, followed by a callback. Call the callback with your generated output instad of returning.
static.handlebars.registerAsyncHelper('toc', function(options, callback) {
static.transform('README.md', function(html) {
static.$(html, function($) {
var output = '<ul>';
$('h3', function() {
output += '<li>' + this.innerHTML + '</li>'
});
callback(output + '</ul>');
});
});
});
Create a DOM window and jQuery object from the specified HTML. callback recieves a $ cherrio instance. The select argument to include is implemented with this.
static.$(html, function($) {
$('a').each(...);
});
Similar to $, calls callback with a $ cherrio instance. $ can be modified within the callback. next will be called with the resulting HTML.
static.modifyDocumentFragment('<ul></ul>', function($) {
$('ul').append('<li></li>');
}, function(html) {
// html === '<ul><li></li></ul>'
});
Called anytime after transform transforms a markdown document. callback is called with the generated HTML and a next function that must be called with the modified HTML. Pairs well with modifyDocumentFragment.
static.onMarkdown(function(html, next) {
next(html);
});
Transform files passed to transform based on file extension. callback recieves:
buffer- the file buffercallback- to be called with the transformed buffercontext- context if transform was invoked from a handlebars helperdata- private handlebars data, also containsfilewhich is a reference to the current file
The html extension is a noop and is implemented as:
static.addTransform('html', function(buffer, next, context, data) {
next(buffer);
});
Defaults to:
{ addIdsToHeadings: true, //in markdown add ids to h[1-6] tags gfm: true, //github flavored markdown highlight: function(code, lang) { return require('highlight.js').highlight(lang || 'javascript', code).value; } }