Skip to content

feat(gatsby-remark-prismjs): Expose Full Line-Length Diff Highlighting #30939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion packages/gatsby-remark-prismjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ plugins: [
// Add additional HTML escapes by providing a mapping
// of HTML entities and their escape value IE: { '}': '{' }
escapeEntities: {},
// Enables full-line highlighting on diff-code-blocks (see below)
// Disabled by default
// To use it, add the following line in gatsby-browser.js (after
// the selected color scheme):
// require("prismjs/plugins/diff-highlight/prism-diff-highlight.css")
// Not currently enable-able on a per-block basis
showDiffHighlight: false,
},
},
],
Expand Down Expand Up @@ -392,12 +399,28 @@ unless explicitly overridden by the `promptUser` and `promptHost` options in the

### Diff code blocks

You can specify language for `diff` code blocks by using `diff-[language]` to enable syntax highlighting in diffs:
You can specify language for `diff` code blocks by using `diff-[language]` to enable syntax highlighting in diffs. Canonically, when writing code _diffs_, the code should be indented by two characters. If the code is being changed, the first character should be a `+`, `-`, or `!` otherwise left a space. The second character should always be a space. Git-style file-location coordinates need not be indented.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You and me both! I Knew the + - but Prism implements ! — not sure if that's a Prism specific thing, and depending on your Prism theme it can be pretty subtle (not exactly the 'exclamation mark' kind of effect I'd hope for) but it is there so I figured I should call it out 😀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can specify language for `diff` code blocks by using `diff-[language]` to enable syntax highlighting in diffs. Canonically, when writing code _diffs_, the code should be indented by two characters. If the code is being changed, the first character should be a `+`, `-`, or `!` otherwise left a space. The second character should always be a space. Git-style file-location coordinates need not be indented.
You can specify language for `diff` code blocks by using `diff-[language]` to enable syntax highlighting in diffs. Canonically, when writing code diffs, the code should be indented by two characters. If the code is being changed, the first character should be a `+`, `-`, or `!` otherwise left a space. The second character should always be a space. Git-style file-location coordinates need not be indented.


````
```diff-javascript
@@ -4,6 +4,5 @@
const doTheThing = aParm => {
+ console.log(`Parm is: ${aParm}`)
- console.log('Hit this function')
! console.log('Rubber duck debugging anybody?')
return true
}
```
````

The default rendering style for diff code blocks will be a colored `+` or `-` symbol and _not_ full line-length highlighting. This is depicted in the **first** example here: https://prismjs.com/plugins/diff-highlight/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The default rendering style for diff code blocks will be a colored `+` or `-` symbol and _not_ full line-length highlighting. This is depicted in the **first** example here: https://prismjs.com/plugins/diff-highlight/
The default rendering style for diff code blocks will be a colored `+` or `-` symbol and not full line-length highlighting. This is depicted in the first example here: https://prismjs.com/plugins/diff-highlight/

We try to use the formatting sparingly and this seems a bit too much :)


This plugin supports a configuration parameter, `showDiffHighlight`, which will add full line-length highlighting to _all_ Diff code blocks used by your Gatsby site with the format: `+` -> Highlight green, `-` -> highlight red, and `!` -> bolded line. Simply pass the configuration parameter with `true` and add the following to your `gatsby-browser.js` (_after_ the require for your chosen theme):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This plugin supports a configuration parameter, `showDiffHighlight`, which will add full line-length highlighting to _all_ Diff code blocks used by your Gatsby site with the format: `+` -> Highlight green, `-` -> highlight red, and `!` -> bolded line. Simply pass the configuration parameter with `true` and add the following to your `gatsby-browser.js` (_after_ the require for your chosen theme):
This plugin supports a configuration parameter, `showDiffHighlight`, which will add full line-length highlighting to all diff code blocks used by your Gatsby site with the format:
- `+` will highlight green
- `-` will highlight red
- `!` will bolded the line
Use `showDiffHighlight: true` inside `gatsby-config.js` and add the following to your `gatsby-browser.js` (after the require for your chosen theme):


```js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```js
```js:title=gatsby-browser.js
require("prismjs/themes/prism-solarizedlight.css") // Place the diff-highlight after your existing theme

require("prismjs/plugins/diff-highlight/prism-diff-highlight.css")
```

### Line hiding

As well as highlighting lines, it's possible to _hide_ lines from the rendered output. Often this is handy when using `gatsby-remark-prismjs` along with [`gatsby-remark-embed-snippet`](https://www.gatsbyjs.org/packages/gatsby-remark-embed-snippet/).
Expand Down Expand Up @@ -520,6 +543,10 @@ throw our overflow and background on `.gatsby-highlight`, and use
`display:block` on `.gatsby-highlight-code-line` – all of this coming together
to facilitate the desired line highlight behavior.

**NOTE:** This is _not_ the same as the Diff code-block full line-length
highlighting for diff'd lines. Those "line highlights" work great beyond
Comment on lines +546 to +547
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**NOTE:** This is _not_ the same as the Diff code-block full line-length
highlighting for diff'd lines. Those "line highlights" work great beyond
**Note:** This is not the same as the `showDiffHighlight` option for diff'd lines. Those "line highlights" work great beyond

the above.

### Line numbering

Because [the line numbering PrismJS plugin][7] runs client-side, a few adaptations were required to make it work:
Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby-remark-prismjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = (
noInlineHighlight = false,
showLineNumbers: showLineNumbersGlobal = false,
showInvisibles = false,
showDiffHighlight = false,
languageExtensions = [],
prompt = {
user: `root`,
Expand Down Expand Up @@ -78,7 +79,11 @@ module.exports = (
//
// @see https://github.com/gatsbyjs/gatsby/issues/1486
const className = `${classPrefix}${languageName}${
diffLanguage ? `-${diffLanguage}` : ``
diffLanguage
? showDiffHighlight
? `-${diffLanguage} diff-highlight`
: `-${diffLanguage}`
: ``
}`

// Replace the node with the markup we need to make
Expand Down