Skip to content

Commit

Permalink
Added warning check for invalid range expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Nov 24, 2017
1 parent 477ee9b commit aad2f3b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,64 @@ Object {
}
`;
exports[`gatsby-remark-embed-snippet should error if an invalid range expression is specified 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"position": Position {
"end": Object {
"column": 23,
"line": 1,
"offset": 22,
},
"indent": Array [],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "html",
"value": "<div class=\\"gatsby-highlight\\">
<pre class=\\"language-jsx\\"><code>console<span class=\\"token punctuation\\">.</span><span class=\\"token function\\">log</span><span class=\\"token punctuation\\">(</span><span class=\\"token string\\">\\"oops!\\"</span><span class=\\"token punctuation\\">)</span><span class=\\"token punctuation\\">;</span>
</code></pre>
</div>",
},
],
"position": Position {
"end": Object {
"column": 23,
"line": 1,
"offset": 22,
},
"indent": Array [],
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "paragraph",
},
],
"position": Object {
"end": Object {
"column": 23,
"line": 1,
"offset": 22,
},
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;
exports[`gatsby-remark-embed-snippet should not modify non-embed inlineCode nodes 1`] = `
Object {
"children": Array [
Expand Down
22 changes: 22 additions & 0 deletions packages/gatsby-remark-embed-snippet/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ describe(`gatsby-remark-embed-snippet`, () => {
expect(transformed).toMatchSnapshot()
})

it(`should error if an invalid range expression is specified`, () => {
spyOn(console, `warn`)

fs.readFileSync.mockReturnValue(
`
// highlight-range 1
console.log("oops!");
`
.replace(/^ +/gm, ``)
.trim()
)

const markdownAST = remark.parse(`\`embed:hello-world.js\``)
const transformed = plugin({ markdownAST }, { directory: `examples` })

expect(transformed).toMatchSnapshot()

expect(console.warn).toHaveBeenCalledWith(
`Invalid match specified: "// highlight-range 1"`
)
})

describe(`CSS files`, () => {
it(`should extract the correct Prism language`, () => {
fs.readFileSync.mockReturnValue(`html { height: 100%; }`)
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby-remark-embed-snippet/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ module.exports = (
return false
} else if (line.includes(`highlight-range`)) {
const match = line.match(/highlight-range{([^}]+)}/)
if (!match) {
console.warn(`Invalid match specified: "${line.trim()}"`)
return false
}
const range = match[1]

// Highlight line numbers are 1-based but so are offsets.
Expand Down

0 comments on commit aad2f3b

Please sign in to comment.