Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gatsby-transformer-javascript-frontmatter

Package Overview
Dependencies
Maintainers
1
Versions
310
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gatsby-transformer-javascript-frontmatter - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0-alpha.067e672c

246

gatsby-node.js

@@ -1,163 +0,127 @@

"use strict";
var _asyncToGenerator = require("@babel/runtime/helpers/asyncToGenerator");
var _regenerator = require("babel-runtime/regenerator");
const _ = require(`lodash`);
var _regenerator2 = _interopRequireDefault(_regenerator);
const crypto = require(`crypto`);
var _extends2 = require("babel-runtime/helpers/extends");
const babylon = require(`babylon`);
var _extends3 = _interopRequireDefault(_extends2);
const traverse = require(`babel-traverse`).default;
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
function onCreateNode(_x) {
return _onCreateNode.apply(this, arguments);
}
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _onCreateNode() {
_onCreateNode = _asyncToGenerator(function* ({
node,
getNode,
boundActionCreators,
loadNodeContent
}) {
const createNode = boundActionCreators.createNode,
createParentChildLink = boundActionCreators.createParentChildLink; // This only processes javascript files.
var onCreateNode = function () {
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(_ref) {
var node = _ref.node,
getNode = _ref.getNode,
boundActionCreators = _ref.boundActionCreators,
loadNodeContent = _ref.loadNodeContent;
var createNode, createParentChildLink, code, options, exportsData, frontmatter, ast, parseData, objStr, contentDigest, nodeData;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
createNode = boundActionCreators.createNode, createParentChildLink = boundActionCreators.createParentChildLink;
if (node.internal.mediaType !== `application/javascript`) {
return;
}
// This only processes javascript files.
const code = yield loadNodeContent(node);
const options = {
sourceType: `module`,
allowImportExportEverywhere: true,
plugins: [`jsx`, `doExpressions`, `objectRestSpread`, `decorators`, `classProperties`, `exportExtensions`, `asyncGenerators`, `functionBind`, `functionSent`, `dynamicImport`, `flow`]
};
let exportsData, frontmatter;
if (!(node.internal.mediaType !== `application/javascript`)) {
_context.next = 3;
break;
}
try {
const ast = babylon.parse(code, options);
return _context.abrupt("return");
const parseData = function parseData(node) {
let value;
case 3:
_context.next = 5;
return loadNodeContent(node);
if (node.type === `TemplateLiteral`) {
// Experimental basic support for template literals:
// Extract and join any text content; ignore interpolations
value = node.quasis.map(quasi => quasi.value.cooked).join(``);
} else if (node.type === `ObjectExpression`) {
value = {};
node.properties.forEach(elem => {
value[elem.key.name] = parseData(elem.value);
});
} else if (node.type === `ArrayExpression`) {
value = node.elements.map(elem => parseData(elem));
} else {
value = node.value;
}
case 5:
code = _context.sent;
options = {
sourceType: `module`,
allowImportExportEverywhere: true,
plugins: [`jsx`, `doExpressions`, `objectRestSpread`, `decorators`, `classProperties`, `exportExtensions`, `asyncGenerators`, `functionBind`, `functionSent`, `dynamicImport`, `flow`]
};
exportsData = void 0, frontmatter = void 0;
return value;
};
try {
ast = babylon.parse(code, options);
frontmatter = {};
traverse(ast, {
AssignmentExpression: function AssignmentExpression(astPath) {
if (astPath.node.left.type === `MemberExpression` && astPath.node.left.property.name === `frontmatter`) {
astPath.node.right.properties.forEach(node => {
frontmatter[node.key.name] = parseData(node.value);
});
}
},
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
const declaration = astPath.node.declaration;
parseData = function parseData(node) {
var value = void 0;
if (declaration && declaration.type === `VariableDeclaration`) {
const dataVariableDeclarator = _.find(declaration.declarations, d => d.id.name === `frontmatter`);
if (node.type === `TemplateLiteral`) {
// Experimental basic support for template literals:
// Extract and join any text content; ignore interpolations
value = node.quasis.map(function (quasi) {
return quasi.value.cooked;
}).join(``);
} else if (node.type === `ObjectExpression`) {
value = {};
node.properties.forEach(function (elem) {
value[elem.key.name] = parseData(elem.value);
});
} else if (node.type === `ArrayExpression`) {
value = node.elements.map(function (elem) {
return parseData(elem);
});
} else {
value = node.value;
}
return value;
};
frontmatter = {};
traverse(ast, {
AssignmentExpression: function AssignmentExpression(astPath) {
if (astPath.node.left.type === `MemberExpression` && astPath.node.left.property.name === `frontmatter`) {
astPath.node.right.properties.forEach(function (node) {
frontmatter[node.key.name] = parseData(node.value);
});
}
},
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
var declaration = astPath.node.declaration;
if (declaration && declaration.type === `VariableDeclaration`) {
var dataVariableDeclarator = _.find(declaration.declarations, function (d) {
return d.id.name === `frontmatter`;
});
if (dataVariableDeclarator && dataVariableDeclarator.init) {
dataVariableDeclarator.init.properties.forEach(function (node) {
frontmatter[node.key.name] = parseData(node.value);
});
}
}
}
if (dataVariableDeclarator && dataVariableDeclarator.init) {
dataVariableDeclarator.init.properties.forEach(node => {
frontmatter[node.key.name] = parseData(node.value);
});
exportsData = (0, _extends3.default)({}, frontmatter, {
error: false
});
} catch (e) {
// stick the error on the query so the user can
// react to an error as they see fit
exportsData = (0, _extends3.default)({}, frontmatter, {
error: {
err: true,
code: e.code,
message: e.message,
stack: e.stack
}
});
} finally {
objStr = JSON.stringify(node);
contentDigest = crypto.createHash(`md5`).update(objStr).digest(`hex`);
nodeData = {
id: `${node.id} >>> JavascriptFrontmatter`,
children: [],
parent: node.id,
node: (0, _extends3.default)({}, node),
internal: {
contentDigest,
type: `JavascriptFrontmatter`
}
};
nodeData.frontmatter = (0, _extends3.default)({}, exportsData);
if (node.internal.type === `File`) {
nodeData.fileAbsolutePath = node.absolutePath;
}
createNode(nodeData);
createParentChildLink({ parent: node, child: nodeData });
}
}
}
});
exportsData = Object.assign({}, frontmatter, {
error: false
});
} catch (e) {
// stick the error on the query so the user can
// react to an error as they see fit
exportsData = Object.assign({}, frontmatter, {
error: {
err: true,
code: e.code,
message: e.message,
stack: e.stack
}
});
} finally {
const objStr = JSON.stringify(node);
const contentDigest = crypto.createHash(`md5`).update(objStr).digest(`hex`);
const nodeData = {
id: `${node.id} >>> JavascriptFrontmatter`,
children: [],
parent: node.id,
node: Object.assign({}, node),
internal: {
contentDigest,
type: `JavascriptFrontmatter`
}
};
nodeData.frontmatter = Object.assign({}, exportsData);
case 9:
case "end":
return _context.stop();
}
if (node.internal.type === `File`) {
nodeData.fileAbsolutePath = node.absolutePath;
}
}, _callee, this);
}));
return function onCreateNode(_x) {
return _ref2.apply(this, arguments);
};
}();
createNode(nodeData);
createParentChildLink({
parent: node,
child: nodeData
});
}
});
return _onCreateNode.apply(this, arguments);
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _ = require(`lodash`);
var crypto = require(`crypto`);
var babylon = require(`babylon`);
var traverse = require(`babel-traverse`).default;
exports.onCreateNode = onCreateNode;
{
"name": "gatsby-transformer-javascript-frontmatter",
"version": "1.0.1",
"description": "Gatsby transformer plugin for JavaScript to extract exports.frontmatter statically.",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__",
"prepublish": "cross-env NODE_ENV=production npm run build"
"version": "1.1.0-alpha.067e672c",
"author": "Jacob Bolda <me@jacobbolda.com>",
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"keywords": [
"gatsby",
"gatsby-plugin",
"js"
],
"author": "Jacob Bolda <me@jacobbolda.com>",
"license": "MIT",
"dependencies": {

@@ -24,4 +16,22 @@ "babel-runtime": "^6.26.0",

"devDependencies": {
"@babel/cli": "^7.0.0-beta.38",
"@babel/core": "^7.0.0-beta.38",
"cross-env": "^5.0.5"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-javascript-frontmatter#readme",
"keywords": [
"gatsby",
"gatsby-plugin",
"js"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby.git"
},
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"prepublish": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore __tests__"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc