Documark helper functions for working with cache files.
When running var cache = require('documark-cache')(document);
the following happens:
- The cache helper will use given
document
for resolving paths. - A cache folder (
.documark/
) will be created in your document directory. - This directory will also be set as wkhtmltopdf its
--cache-dir
.
-
Load cache helper:
var cache = require('documark-cache')(document);
-
Use the cache helper in your plugins:
var fs = require('fs'); module.exports = function dmpPluginNameHere ($, document, done) { var cache = require('documark-cache')(document); // Folder path: var folderPath = cache.folderPath(); // Path to <document root>/.documark/ // File path: var filePath = cache.filePath('myfile.json'); // Path to <document root>/.documark/myfile.json fs.writeFileSync(filePath, 'some content'); // Read file: var file = cache.fileReadStream('my-cache-file.json'); file.on('data', function (chunk) { ... }); file.on('end', function () { ... }); file.resume(); // Or pipe to other stream: file.pipe(other); // Write to file: var file = cache.fileWriteStream('my-cache-file.json'); file.end(JSON.stringify({ hello: 'world!' }); // file.path contains the stream it's file path string done(); };
Note: Don't worry about unsafe file names, they will be sanitized automatically.