@dww/remark-plugin-graphql
Advanced tools
Comparing version 1.0.2 to 1.0.3
123
index.js
@@ -1,40 +0,95 @@ | ||
const RemarkPlugin = require('@dww/remark-plugin-graphql'); | ||
const visit = require('unist-util-visit'); | ||
const graphql = require('graphql'); | ||
const fs = require('fs'); | ||
// From https://github.com/jxson/front-matter/blob/master/index.js | ||
var optionalByteOrderMark = '\\ufeff?'; | ||
var platform = typeof process !== 'undefined' ? process.platform : ''; | ||
var pattern = '^(' + | ||
optionalByteOrderMark + | ||
'(= yaml =|---)' + | ||
'$([\\s\\S]*?)' + | ||
'^(?:\\2|\\.\\.\\.)\\s*' + | ||
'$' + | ||
(platform === 'win32' ? '\\r?' : '') + | ||
'(?:\\n)?)'; | ||
// NOTE: If this pattern uses the 'g' flag the `regex` variable definition will | ||
// need to be moved down into the functions that use it. | ||
var frontmatterRegex = new RegExp(pattern, 'm'); | ||
module.exports = attacher; | ||
function markdownOffset({markdownAST, markdownNode}) { | ||
const match = frontmatterRegex.exec(markdownNode.rawBody || ''); | ||
if (!match) { | ||
return 0; | ||
/** | ||
* Render a helpful description of the location of the error in the GraphQL | ||
* Source document. | ||
*/ | ||
function highlightSourceAtLocation(source, location, message) { | ||
const firstLineColumnOffset = source.locationOffset.column - 1; | ||
const body = whitespace(firstLineColumnOffset) + source.body; | ||
const lineIndex = location.line - 1; | ||
const lineOffset = source.locationOffset.line - 1; | ||
const lineNum = location.line + lineOffset; | ||
const columnOffset = location.line === 1 ? firstLineColumnOffset : 0; | ||
const columnNum = location.column + columnOffset; | ||
const lines = body.split(/\r\n|[\n\r]/g); | ||
return `${source.name} (${lineNum}:${columnNum})\n` + printPrefixedLines([ | ||
// Lines specified like this: ["prefix", "string"], | ||
[`${lineNum - 1}: `, lines[lineIndex - 1]], | ||
[`${lineNum}: `, lines[lineIndex]], | ||
['', whitespace(columnNum - 1) + '^' + whitespace(1) + (message || '')], | ||
[`${lineNum + 1}: `, lines[lineIndex + 1]], | ||
]); | ||
} | ||
function printPrefixedLines(lines) { | ||
const existingLines = lines.filter(([_, line]) => line !== undefined); | ||
let padLen = 0; | ||
for (const [prefix] of existingLines) { | ||
padLen = Math.max(padLen, prefix.length); | ||
} | ||
const lines = match[match.length - 1].split(/\r\n|[\n\r]/g); | ||
return lines.length; | ||
return existingLines | ||
.map(([prefix, line]) => lpad(padLen, prefix) + line) | ||
.join('\n'); | ||
} | ||
module.exports = ( | ||
{markdownAST, markdownNode, reporter, ...gatsbyOpts}, | ||
opts, | ||
) => { | ||
const transformer = RemarkPlugin({ | ||
...opts, | ||
markdownOffset: markdownOffset({markdownAST, markdownNode}), | ||
}); | ||
try { | ||
return transformer(markdownAST); | ||
} catch (e) { | ||
reporter.error(e); | ||
function whitespace(len) { | ||
return Array(len + 1).join(' '); | ||
} | ||
function lpad(len, str) { | ||
return whitespace(len - str.length) + str; | ||
} | ||
function attacher(opts = {}) { | ||
const {schemaPath, markdownOffset = 0} = opts; | ||
if (!schemaPath) { | ||
throw new Error('Missing required field `schemaPath` in config'); | ||
} | ||
}; | ||
const schema = graphql.buildSchema(fs.readFileSync(schemaPath).toString()); | ||
return transformer; | ||
function transformer(ast, file) { | ||
visit(ast, 'code', visitor); | ||
function visitor(node) { | ||
if (node.lang !== 'gql' && node.lang !== 'graphql') { | ||
return; | ||
} | ||
const query = node.value; | ||
try { | ||
const parsed = graphql.parse(query); | ||
const validationErrors = graphql.validate(schema, parsed); | ||
if (validationErrors.length) { | ||
throw validationErrors[0]; | ||
} | ||
} catch (err) { | ||
// TODO: check that it's a GraphQLError | ||
const start = node.position.start; | ||
throw new Error( | ||
highlightSourceAtLocation( | ||
{ | ||
name: 'Invalid GraphQL query', | ||
locationOffset: {...start, line: start.line + 1 + markdownOffset}, | ||
body: query, | ||
}, | ||
err.locations[0], | ||
err.message, | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "@dww/remark-plugin-graphql", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4881564
93
2