diff --git a/docs/docs/gatsby-style-guide.md b/docs/docs/gatsby-style-guide.md index a4752335aaaf2..5a627b4e4b00d 100644 --- a/docs/docs/gatsby-style-guide.md +++ b/docs/docs/gatsby-style-guide.md @@ -296,7 +296,9 @@ designed for scientific communication but might help avoid overspecialized wording. ## Best practices + ### Support software versions + When Gatsby commits to support a specific version of software (e.g. Node 6 and up), this is reflected in documentation. Gatsby documentation should be usable by all people on supported software, which means we don't introduce any commands or practices that can't be used by people on versions we've committed to support. In rare circumstances, we'll consider mentioning a newly introduced command or practice as side notes. For example, npm 5.2.0 (which comes with Node 8) introduced a command called `npx` that is not available for versions of Node below 8. Since Gatsby supports Node 6 and up, documentation should only introduce `npx` as an optional command in a note like so: @@ -304,9 +306,11 @@ For example, npm 5.2.0 (which comes with Node 8) introduced a command called `np > npm 5.2.0--bundled with Node 8--introduced a command called `npx`. Gatsby supports Node 6 and up, so we introduce `npx` here as an optional command for users of npm 5.2.0 or greater. ### Share best practices whenever possible + When there are multiple ways to complete a task, the docs should explain the following: + 1. The most fundamental way of completing the task -2. The most common way of completing a task +2. The most common way of completing a task 3. The best way to complete the task on the lowest supported versions of software 4. The best practice and why is it the best (if different than 3) 5. Any tips on how to pick an option @@ -320,6 +324,7 @@ The main tutorial at `/tutorial/` is optimized for users who are not experts in ## Tutorial ### Tutorial audience + Through research, it's clear that developers of all skill levels read the tutorial and go back to reference it later. The tutorial should prioritize helping users with the following attributes and goals. @@ -337,6 +342,7 @@ Looking for: - a way to start a site and/or app project that uses React ### Tutorial purpose + By following the steps in the tutorial, a user should: - Experience the value of Gatsby as quickly as possible. With Gatsby, a user typically values that it takes fewer steps (and is therefore easier) to: diff --git a/examples/using-remark/gatsby-config.js b/examples/using-remark/gatsby-config.js index 2782c68b8a677..3d39c5f5ed7b9 100644 --- a/examples/using-remark/gatsby-config.js +++ b/examples/using-remark/gatsby-config.js @@ -52,6 +52,13 @@ module.exports = { dashes: `oldschool`, }, }, + { + resolve: `gatsby-remark-embed-snippet`, + options: { + // Example code links are relative to this dir. + directory: `${__dirname}/src/code-examples/`, + }, + }, `gatsby-remark-prismjs`, `gatsby-remark-autolink-headers`, `gatsby-remark-katex`, diff --git a/examples/using-remark/package.json b/examples/using-remark/package.json index 848a472b0eda6..a671e7a115e2b 100644 --- a/examples/using-remark/package.json +++ b/examples/using-remark/package.json @@ -16,6 +16,7 @@ "gatsby-plugin-typography": "^2.2.0", "gatsby-remark-autolink-headers": "^2.0.5", "gatsby-remark-copy-linked-files": "^2.0.5", + "gatsby-remark-embed-snippet": "^3.1.2", "gatsby-remark-images": "^2.0.1", "gatsby-remark-katex": "^2.0.5", "gatsby-remark-prismjs": "^3.0.0", diff --git a/examples/using-remark/src/code-examples/hide-and-highlight-lines.js b/examples/using-remark/src/code-examples/hide-and-highlight-lines.js new file mode 100644 index 0000000000000..3911947b1ebf0 --- /dev/null +++ b/examples/using-remark/src/code-examples/hide-and-highlight-lines.js @@ -0,0 +1,16 @@ +/* hide-range{1-3} */ +import React from "react" +import ReactDOM from "react-dom" + +// highlight-next-line +const name = `Brian` + +// highlight-range{4-5} +// hide-next-line +ReactDOM.render( +
+

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) // hide-line +) // hide-line diff --git a/examples/using-remark/src/code-examples/hide-lines.js b/examples/using-remark/src/code-examples/hide-lines.js new file mode 100644 index 0000000000000..2c428b899d942 --- /dev/null +++ b/examples/using-remark/src/code-examples/hide-lines.js @@ -0,0 +1,14 @@ +/* hide-range{1-3} */ +import React from "react" +import ReactDOM from "react-dom" + +const name = `Brian` + +// hide-next-line +ReactDOM.render( +
+

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) // hide-line +) // hide-line diff --git a/examples/using-remark/src/code-examples/highlight-lines.js b/examples/using-remark/src/code-examples/highlight-lines.js new file mode 100644 index 0000000000000..8b458eb5d4cc0 --- /dev/null +++ b/examples/using-remark/src/code-examples/highlight-lines.js @@ -0,0 +1,13 @@ +import React from "react" +import ReactDOM from "react-dom" + +const name = `Brian` // highlight-line + +ReactDOM.render( +
+ {/* highlight-range{1-2} */} +

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) +) diff --git a/examples/using-remark/src/code-examples/plain.js b/examples/using-remark/src/code-examples/plain.js new file mode 100644 index 0000000000000..b0c7e7a9d3e02 --- /dev/null +++ b/examples/using-remark/src/code-examples/plain.js @@ -0,0 +1,12 @@ +import React from "react" +import ReactDOM from "react-dom" + +const name = `Brian` + +ReactDOM.render( +
+

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) +) diff --git a/examples/using-remark/src/pages/2017-04-05---embed-snippets/index.md b/examples/using-remark/src/pages/2017-04-05---embed-snippets/index.md new file mode 100644 index 0000000000000..51ded6872b525 --- /dev/null +++ b/examples/using-remark/src/pages/2017-04-05---embed-snippets/index.md @@ -0,0 +1,117 @@ +--- +title: "Embed file contents within PrismJS blocks" +date: "2017-04-05" +draft: false +author: Daisy Buchanan +tags: + - remark + - Code Highlighting + - embed snippets +--- + +You can embed the contents of existing files within your Markdown using the [`gatsby-remark-embed-snippet`][1] with [`gatsby-remark-prismjs`][2]. + +Let's start by displaying file contents as a highlighted code block, then see how to control line highlighting and visibility from within the source file. + +## Embedding files + +After installing the required plugins you can embed a file within your Markdown by using `embed` snippets. To embed the file `src/code-examples/plain.js` you could write the following: + +
`embed:plain.js`
+ +This will look like: + +`embed:plain.js` + +## Highlight lines + +You can highlight specific lines by adding special comments to the source file. Let's see how this works in `src/code-examples/highlight-lines.js`. + +Take a look at the `highlight-line` comment on line 4 and the `highlight-range` comment on line 8: + +```{numberLines: true} +import React from "react" +import ReactDOM from "react-dom" + +const name = `Brian` // highlight-line + +ReactDOM.render( +
+ {/* highlight-range{1-2} */} +

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) +) +``` + +When the file is embedded with: + +
`embed:highlight-lines.js`
+ +it will display like this: + +`embed:highlight-lines.js` + +## Hide lines + +You can use similar comments to hide specific lines. Here's another example, this time using `src/code-examples/hide-lines.js`. Check out the `hide-line` comments on lines 1, 7, 13 and 14: + +```{numberLines: true} +/* hide-range{1-3} */ +import React from "react" +import ReactDOM from "react-dom" + +const name = `Brian` + +// hide-next-line +ReactDOM.render( +
+

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) // hide-line +) // hide-line +``` + +Which will display like this: + +`embed:hide-lines.js` + +## All together now + +You can mix line highlighting and hiding in the same file. This is from `src/code-examples/hide-and-highlight-lines.js` + +``` +/* hide-range{1-3} */ +import React from "react" +import ReactDOM from "react-dom" + +// highlight-next-line +const name = `Brian` + +// highlight-range{4-5} +// hide-next-line +ReactDOM.render( +
+

Hello, ${name}!

+

Welcome to this example

+
, + document.getElementById(`root`) // hide-line +) // hide-line +``` + +It'll look like this: + +`embed:hide-and-highlight-lines.js` + +## Related info + +You've learnt how to embed the contents of existing files into your Markdown using [`gatsby-remark-embed-snippet`][1] with [`gatsby-remark-prismjs`][2]. Hurray! + +If you'd prefer to write your code blocks directly within your Markdown, take a look at the example [Code and Syntax Highlighting with PrismJS][4]. + +[1]: https://www.gatsbyjs.org/packages/gatsby-remark-embed-snippet/ +[2]: https://www.gatsbyjs.org/packages/gatsby-remark-prismjs/ +[3]: http://prismjs.com/ +[4]: /code-and-syntax-highlighting/ diff --git a/examples/using-remark/src/pages/excerpt-example.js b/examples/using-remark/src/pages/excerpt-example.js index aa9b6ac11813a..a0cab5a9007ba 100644 --- a/examples/using-remark/src/pages/excerpt-example.js +++ b/examples/using-remark/src/pages/excerpt-example.js @@ -1,5 +1,6 @@ import React from "react" import { Link, graphql } from "gatsby" +import Layout from "../layouts" import styles from "../styles" import presets from "../utils/presets" import { rhythm, scale } from "../utils/typography" @@ -9,7 +10,7 @@ class Index extends React.Component { const posts = this.props.data.allMarkdownRemark.edges return ( -
+

-
+ ) } } diff --git a/examples/using-remark/src/pages/index.js b/examples/using-remark/src/pages/index.js index 46ec6418571ce..a858b18e3fa60 100644 --- a/examples/using-remark/src/pages/index.js +++ b/examples/using-remark/src/pages/index.js @@ -78,7 +78,7 @@ export const pageQuery = graphql` query { allMarkdownRemark( limit: 2000 - sort: { fields: [frontmatter___date], order: DESC } + sort: { fields: [frontmatter___date], order: ASC } filter: { frontmatter: { draft: { ne: true }, example: { ne: true } } } ) { edges { diff --git a/packages/gatsby-remark-embed-snippet/README.md b/packages/gatsby-remark-embed-snippet/README.md index f047507039a37..17276d29aa921 100644 --- a/packages/gatsby-remark-embed-snippet/README.md +++ b/packages/gatsby-remark-embed-snippet/README.md @@ -110,6 +110,53 @@ baz: "highlighted" quz: "highlighted" ``` +### Hide Lines + +It's also possible to specify a range of lines to be hidden. + +#### JavaScript example + +```js +// hide-range{1-2} +import React from "react" +import ReactDOM from "react-dom" + +function App() { + return ( +
+ +
+ ) +} + +// hide-range{1-2} +const rootElement = document.getElementById("root") +ReactDOM.render(, rootElement) +``` + +Will produce something like this: + +```js +function App() { + return ( +
+ +
+ ) +} +``` + ## Installation `npm install --save gatsby-remark-embed-snippet` @@ -118,7 +165,7 @@ quz: "highlighted" **Important**: This module must appear before `gatsby-remark-prismjs` in your plugins array, or the markup will have already been transformed into a code block and this plugin will fail to detect it and inline the file. -```javascript +```js // In your gatsby-config.js module.exports = { plugins: [ diff --git a/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap index 471f54dd08c9d..dd74aa0c8cd86 100644 --- a/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`gatsby-remark-embed-snippet CSS files should extract the correct Prism language 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the correct Prism language for CSS files 1`] = ` Object { "children": Array [ Object { @@ -57,785 +57,7 @@ Object { } `; -exports[`gatsby-remark-embed-snippet CSS files should support highlight-line and highlight-next-line markers 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 24, - "line": 1, - "offset": 23, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
html {
-height: 100%;}
-
-* {
-box-sizing: border-box;}
-
", - }, - ], - "position": Position { - "end": Object { - "column": 24, - "line": 1, - "offset": 23, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 24, - "line": 1, - "offset": 23, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; - -exports[`gatsby-remark-embed-snippet CSS files should support highlight-range markers 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 24, - "line": 1, - "offset": 23, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
html {
-height: 100%;width: 100%;}
-
", - }, - ], - "position": Position { - "end": Object { - "column": 24, - "line": 1, - "offset": 23, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 24, - "line": 1, - "offset": 23, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; - -exports[`gatsby-remark-embed-snippet HTML files should extract the correct Prism language 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
<html></html>
-
", - }, - ], - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; - -exports[`gatsby-remark-embed-snippet HTML files should support highlight-line and highlight-next-line markers 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
<html>
-<body>
-<h1>highlighted</h1><p>
-highlighted</p>
-</body>
-</html>
-
", - }, - ], - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; - -exports[`gatsby-remark-embed-snippet HTML files should support highlight-range markers 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
<html>
-<body>
-<p>
-highlighted</p>
-</body>
-</html>
-
", - }, - ], - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; - -exports[`gatsby-remark-embed-snippet JavaScript files should extract the correct Prism language 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": "
-
const foo = \\"bar\\";
-
", - }, - ], - "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 JavaScript files should support JSX line highlight comments 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>
-<button>Add Item</button> 
-<ReactCSSTransitionGroup
-transitionName=\\"example\\"
-transitionEnterTimeout={500}
-transitionLeaveTimeout={300}>
-{items}</ReactCSSTransitionGroup>
-</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 JavaScript files should support highlight-line and highlight-next-line markers 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": "
-
import React from 'react';
-import ReactDOM from 'react-dom';
-
-ReactDOM.render(<h1>Hello, world!</h1>,
-document.getElementById('root')
-);
-
", - }, - ], - "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 JavaScript files should support highlight-range markers 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": "
-
ReactDOM.render(
-<h1>Hello, world!</h1>,document.getElementById('root'));
-
", - }, - ], - "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 JavaScript files should support highlighting a range via JSX comments 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": "
-
<ul>
-<li>Not highlighted</li>
-<li>Highlighted</li><li>Highlighted</li><li>Highlighted</li><li>Not highlighted</li>
-</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 JavaScript files should support multiple highlight directives within a single file 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": "
-
let notHighlighted;
-let highlighted;
-notHighlighted = 1;
-
-highlighted = 2;
-notHighlighted = 3;
-highlighted = 4;
-
", - }, - ], - "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 Markdown files should extract the correct Prism language 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": "
-
# Hi
-
", - }, - ], - "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 YAML files should extract the correct Prism language 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
name: Brian Vaughn
-
", - }, - ], - "position": Position { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 25, - "line": 1, - "offset": 24, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; - -exports[`gatsby-remark-embed-snippet YAML files should support highlight-line and highlight-next-line markers 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the correct Prism language for HTML files 1`] = ` Object { "children": Array [ Object { @@ -856,8 +78,7 @@ Object { }, "type": "html", "value": "
-
foo: \\"highlighted\\"bar: \\"not highlighted\\"
-baz: \\"highlighted\\"qux: \\"not highlighted\\"
+
<html></html>
", }, ], @@ -893,7 +114,7 @@ Object { } `; -exports[`gatsby-remark-embed-snippet YAML files should support highlight-range markers 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the correct Prism language for JavaScript files 1`] = ` Object { "children": Array [ Object { @@ -901,9 +122,9 @@ Object { Object { "position": Position { "end": Object { - "column": 25, + "column": 23, "line": 1, - "offset": 24, + "offset": 22, }, "indent": Array [], "start": Object { @@ -914,16 +135,15 @@ Object { }, "type": "html", "value": "
-
foo: \\"highlighted\\"bar: \\"not highlighted\\"
-baz: \\"highlighted\\"
+
const foo = \\"bar\\";
", }, ], "position": Position { "end": Object { - "column": 25, + "column": 23, "line": 1, - "offset": 24, + "offset": 22, }, "indent": Array [], "start": Object { @@ -937,9 +157,9 @@ Object { ], "position": Object { "end": Object { - "column": 25, + "column": 23, "line": 1, - "offset": 24, + "offset": 22, }, "start": Object { "column": 1, @@ -951,7 +171,7 @@ Object { } `; -exports[`gatsby-remark-embed-snippet shell scripts should extract the correct Prism language 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the correct Prism language for Markdown files 1`] = ` Object { "children": Array [ Object { @@ -972,7 +192,7 @@ Object { }, "type": "html", "value": "
-
pwd
+
# Hi
", }, ], @@ -1008,7 +228,7 @@ Object { } `; -exports[`gatsby-remark-embed-snippet shell scripts should support highlight-line and highlight-next-line markers 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the correct Prism language for YAML files 1`] = ` Object { "children": Array [ Object { @@ -1016,9 +236,9 @@ Object { Object { "position": Position { "end": Object { - "column": 23, + "column": 25, "line": 1, - "offset": 22, + "offset": 24, }, "indent": Array [], "start": Object { @@ -1029,20 +249,15 @@ Object { }, "type": "html", "value": "
-
# Yarn
-yarn init
-yarn add react react-dom
-# NPM
-npm init
-npm install --save react react-dom
+
name: Brian Vaughn
", }, ], "position": Position { "end": Object { - "column": 23, + "column": 25, "line": 1, - "offset": 22, + "offset": 24, }, "indent": Array [], "start": Object { @@ -1056,9 +271,9 @@ yarn init ], "position": Object { "end": Object { - "column": 23, + "column": 25, "line": 1, - "offset": 22, + "offset": 24, }, "start": Object { "column": 1, @@ -1070,7 +285,7 @@ yarn init } `; -exports[`gatsby-remark-embed-snippet shell scripts should support highlight-range markers 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the correct Prism language for shell scripts 1`] = ` Object { "children": Array [ Object { @@ -1091,8 +306,7 @@ Object { }, "type": "html", "value": "
-
echo \\"not highlighted\\"
-echo \\"highlighted\\"echo \\"highlighted\\"echo \\"not highlighted\\"
+
pwd
", }, ], @@ -1128,7 +342,7 @@ Object { } `; -exports[`gatsby-remark-embed-snippet should convert embed inlineCode nodes to Prism code blocks 1`] = ` +exports[`gatsby-remark-embed-snippet Language detection should set the default Prism language for unknown file extensions 1`] = ` Object { "children": Array [ Object { @@ -1136,9 +350,9 @@ Object { Object { "position": Position { "end": Object { - "column": 23, + "column": 20, "line": 1, - "offset": 22, + "offset": 19, }, "indent": Array [], "start": Object { @@ -1149,15 +363,15 @@ Object { }, "type": "html", "value": "
-
const foo = \\"bar\\";
+
const foo = \\"bar\\";
", }, ], "position": Position { "end": Object { - "column": 23, + "column": 20, "line": 1, - "offset": 22, + "offset": 19, }, "indent": Array [], "start": Object { @@ -1171,9 +385,9 @@ Object { ], "position": Object { "end": Object { - "column": 23, + "column": 20, "line": 1, - "offset": 22, + "offset": 19, }, "start": Object { "column": 1, @@ -1185,7 +399,7 @@ Object { } `; -exports[`gatsby-remark-embed-snippet should error if an invalid range expression is specified 1`] = ` +exports[`gatsby-remark-embed-snippet should convert embed inlineCode nodes to Prism code blocks 1`] = ` Object { "children": Array [ Object { @@ -1206,7 +420,7 @@ Object { }, "type": "html", "value": "
-
console.log(\\"oops!\\");
+
const foo = \\"bar\\";
", }, ], @@ -1296,60 +510,3 @@ Object { "type": "root", } `; - -exports[`gatsby-remark-embed-snippet unknown file extensions should set the correct default Prism language 1`] = ` -Object { - "children": Array [ - Object { - "children": Array [ - Object { - "position": Position { - "end": Object { - "column": 20, - "line": 1, - "offset": 19, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "html", - "value": "
-
const foo = \\"bar\\";
-
", - }, - ], - "position": Position { - "end": Object { - "column": 20, - "line": 1, - "offset": 19, - }, - "indent": Array [], - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "paragraph", - }, - ], - "position": Object { - "end": Object { - "column": 20, - "line": 1, - "offset": 19, - }, - "start": Object { - "column": 1, - "line": 1, - "offset": 0, - }, - }, - "type": "root", -} -`; diff --git a/packages/gatsby-remark-embed-snippet/src/__tests__/index.js b/packages/gatsby-remark-embed-snippet/src/__tests__/index.js index bd86491488dd3..a056f00828ec9 100644 --- a/packages/gatsby-remark-embed-snippet/src/__tests__/index.js +++ b/packages/gatsby-remark-embed-snippet/src/__tests__/index.js @@ -62,30 +62,8 @@ 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`, () => { + describe(`Language detection`, () => { + it(`should set the correct Prism language for CSS files`, () => { fs.readFileSync.mockReturnValue(`html { height: 100%; }`) const markdownAST = remark.parse(`\`embed:hello-world.css\``) @@ -99,60 +77,7 @@ describe(`gatsby-remark-embed-snippet`, () => { expect(transformed).toMatchSnapshot() }) - it(`should support highlight-line and highlight-next-line markers`, () => { - fs.readFileSync.mockReturnValue( - ` - html { - /* highlight-next-line */ - height: 100%; - } - - * { - box-sizing: border-box; /* highlight-line */ - } - ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.css\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - - it(`should support highlight-range markers`, () => { - fs.readFileSync.mockReturnValue( - ` - html { - /* highlight-range{1,2} */ - height: 100%; - width: 100%; - } - ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.css\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - }) - - describe(`HTML files`, () => { - it(`should extract the correct Prism language`, () => { + it(`should set the correct Prism language for HTML files`, () => { fs.readFileSync.mockReturnValue(``) const markdownAST = remark.parse(`\`embed:hello-world.html\``) @@ -166,64 +91,7 @@ describe(`gatsby-remark-embed-snippet`, () => { expect(transformed).toMatchSnapshot() }) - it(`should support highlight-line and highlight-next-line markers`, () => { - fs.readFileSync.mockReturnValue( - ` - - -

highlighted

-

- - highlighted -

- - - ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.html\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - - it(`should support highlight-range markers`, () => { - fs.readFileSync.mockReturnValue( - ` - - - -

- highlighted -

- - - ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.html\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - }) - - describe(`JavaScript files`, () => { - it(`should extract the correct Prism language`, () => { + it(`should set the correct Prism language for JavaScript files`, () => { fs.readFileSync.mockReturnValue(`const foo = "bar";`) const markdownAST = remark.parse(`\`embed:hello-world.js\``) @@ -237,148 +105,7 @@ describe(`gatsby-remark-embed-snippet`, () => { expect(transformed).toMatchSnapshot() }) - it(`should support highlight-line and highlight-next-line markers`, () => { - fs.readFileSync.mockReturnValue( - ` - import React from 'react'; - import ReactDOM from 'react-dom'; - - // highlight-next-line - ReactDOM.render( -

Hello, world!

, - document.getElementById('root') - ); // highlight-line - ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.js\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - - it(`should support highlight-range markers`, () => { - fs.readFileSync.mockReturnValue( - ` - // highlight-range{2,3} - ReactDOM.render( -

Hello, world!

, - document.getElementById('root') - ); - ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.js\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - - it(`should support JSX line highlight comments`, () => { - fs.readFileSync.mockReturnValue( - ` -
- {/* highlight-line */} - - - {/* highlight-next-line */} - {items} - -
- ` - .replace(/^ +/gm, ``) - .trim() - ) - - const markdownAST = remark.parse(`\`embed:hello-world.js\``) - const transformed = plugin( - { markdownAST }, - { - directory: `examples`, - } - ) - - expect(transformed).toMatchSnapshot() - }) - - it(`should support highlighting a range via JSX comments`, () => { - fs.readFileSync.mockReturnValue( - ` -