Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function _getFrontMatter (relPath) {
let frontMatter = {};
try {
let content = fs.readFileSync(`${CONFIG.docsDir}/${relPath}`, 'utf8');
if (fm.test(content)) {
Object.assign(frontMatter, fm(content).attributes);
}
} finally {
// nothing to do
}
return frontMatter;
}//_getFrontMatter()
module.exports = function (text, syntax, filename) {
var tree;
// Run `.toString()` to allow Buffers to be passed in
text = helpers.stripBom(text.toString());
// if we're skipping front matter do it here, fall back to just our text in case it fails
if (fm.test(text)) {
text = fm(text).body || text;
}
try {
tree = gonzales.parse(text, {
'syntax': syntax
});
}
catch (e) {
throw {
message: e.message,
file: filename,
line: e.line
};
}
var initFile = function(file) {
var data = file.data = parse(file.relative, {locales: options.locales});
// data.locale
data.locale || (data.locale = options.defaultLocale);
// data.document
if (~(options.dataExtensions || []).indexOf(data.extname)) {
data.document = 'data';
} else if (fm.test(file.contents.toString())) {
data.document = 'text';
} else {
data.document = false;
}
// data.filepaths
data.filepaths = [];
data.filepaths.push(data.locale === options.defaultLocale ? null : data.locale);
data.filepaths.push.apply(data.filepaths, data.dirnames);
if (data.document) {
data.filepaths.push(data.index ? null : data.slug);
data.filepaths.push('index.html');
} else {
data.filepaths.push(data.slug + '.' + data.extname);
}
data.filepaths = data.filepaths.filter(function(x) { return x });
item.title = item.title || name
item.slug = item.slug || slug
if (item.date) {
item.date = new Date(item.date)
}
} else {
item = {
title: name,
slug: slug,
content: fileContent,
path: null
}
}
if (frontMatter.test(fileContent)) {
var contentData = frontMatter(fileContent)
var dataAttributes = contentData.attributes
Object.keys(dataAttributes)
.filter(function (k) { // prevents attribute leaking
return dataAttributes.hasOwnProperty(k)
})
.forEach(function (k) { // merges into item object
item[k] = dataAttributes[k]
})
// Assign parsed content
item.content = contentData.body
// If date exists then parse it
if (dataAttributes.date) {
fs.readdirSync(path.resolve(__dirname, 'original')).forEach(function(file) {
var text = fs.readFileSync(path.resolve(__dirname, 'original', file), 'utf8');
if (path.extname(file) === '.md') {
if (fm.test(text)) {
text = fm(text);
text = '---\n' + text.frontmatter + '\ngfm: false\n---\n' + text.body;
} else {
text = '---\ngfm: false\n---\n' + text;
}
}
fs.writeFileSync(path.resolve(__dirname, 'compiled_tests', file), text);
});
function valid(text) {
return text && frontmatter.test(text.trim());
}
mdOpt.preprocess = function(parser, env, source) {
var fm = require("front-matter");
var _ = require("microdash");
if(fm.test(source)) {
var fmData = fm(source);
env = _.extend(env, fmData.attributes);
return fmData.body;
} else {
return source;
}
}
return mdOpt;
fs.readFile(`${CONFIG.docsDir}/${relPath}`, 'utf8', (err, data) => {
if (err) {
next(err);
} else {
const nextFile = {
path: relPath,
content: (fm.test(data) ? fm(data).body : data)
};
next(err, nextFile, ctx);
}
});
}//_readWithContext