work.suroh.tk/node_modules/gray-matter/lib/engines.js

55 lines
1023 B
JavaScript

'use strict';
const yaml = require('js-yaml');
/**
* Default engines
*/
const engines = exports = module.exports;
/**
* YAML
*/
engines.yaml = {
parse: yaml.safeLoad.bind(yaml),
stringify: yaml.safeDump.bind(yaml)
};
/**
* JSON
*/
engines.json = {
parse: JSON.parse.bind(JSON),
stringify: function(obj, options) {
const opts = Object.assign({replacer: null, space: 2}, options);
return JSON.stringify(obj, opts.replacer, opts.space);
}
};
/**
* JavaScript
*/
engines.javascript = {
parse: function parse(str, options, wrap) {
/* eslint no-eval: 0 */
try {
if (wrap !== false) {
str = '(function() {\nreturn ' + str.trim() + ';\n}());';
}
return eval(str) || {};
} catch (err) {
if (wrap !== false && /(unexpected|identifier)/i.test(err.message)) {
return parse(str, options, false);
}
throw new SyntaxError(err);
}
},
stringify: function() {
throw new Error('stringifying JavaScript is not supported');
}
};