@static-pages/file-writer
v5.2.0
Published
Generic file writer implementation for @static-pages/core.
Readme
Static Pages / File writer
Writes contents of a page data to file.
Usage
import fileWriter from '@static-pages/file-writer';
const write = fileWriter({
outDir: 'dist',
outFile: d => d.url + '.html',
renderer: d => d.body,
onOverwrite: fileName => console.log(fileName),
onInvalidPath: fileName => console.log(fileName),
});
const pageData = {
url: 'folder/file',
body: '[file contents]',
};
write({ value: pageData });Docs
@static-pages/file-writer@5 and higher versions are compatible with @static-pages/core@6 and higher versions.
writer(options: Options): void
Options
options.outDir(default:dist) sets the output directory.options.outFileresolves the file name based on the provided page data.options.rendererrenders the file contents based on the provided page data.options.onOverwritecallback function that gets executed when a file name collision occurs. Defaults to log on the console.options.onInvalidPathcallback function that gets executed when a file name contains invalid characters. Defaults to log on the console.
outFile defaults
The default behaviour is to guess file path by a few possible properties of the data:
- if
data.urlis defined, append.htmland use that. - if
data.header.pathis defined, replace extension to.htmland use that. - if nothing matches call the
onInvalidPathhandler withundefinedfile name.
renderer defaults
The default behaviour is to retrieve file contents from data.body.
Tip: you can implement your own rendering logic in the
renderercallback, eg:renderer: d => myRenderingLibrary.render('template', d).
Where to use this?
This module can be used to generate static HTML pages. Read more at the Static Pages JS project page.
