Skip to content

Commit 374dfbc

Browse files
committed
Expose Full Line-Length Diff Highlighting
Enabled a config param to the remark-prism plugin that allows Diff code blocks to have the entire diff'd line be highlighted
1 parent ace4336 commit 374dfbc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/gatsby-remark-prismjs/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ plugins: [
7979
// Add additional HTML escapes by providing a mapping
8080
// of HTML entities and their escape value IE: { '}': '{' }
8181
escapeEntities: {},
82+
// Enables full-line highlighting on diff-code-blocks (see below)
83+
// Disabled by default
84+
// To use it, add the following line in gatsby-browser.js (after
85+
// the selected color scheme):
86+
// require("prismjs/plugins/diff-highlight/prism-diff-highlight.css")
87+
// Not currently enable-able on a per-block basis
88+
showDiffHighlight: false,
8289
},
8390
},
8491
],
@@ -392,12 +399,28 @@ unless explicitly overridden by the `promptUser` and `promptHost` options in the
392399

393400
### Diff code blocks
394401

395-
You can specify language for `diff` code blocks by using `diff-[language]` to enable syntax highlighting in diffs:
402+
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.
396403

397404
````
398405
```diff-javascript
406+
@@ -4,6 +4,5 @@
407+
const doTheThing = aParm => {
408+
+ console.log(`Parm is: ${aParm}`)
409+
- console.log('Hit this function')
410+
! console.log('Rubber duck debugging anybody?')
411+
return true
412+
}
413+
```
399414
````
400415

416+
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/
417+
418+
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):
419+
420+
```js
421+
require("prismjs/plugins/diff-highlight/prism-diff-highlight.css")
422+
```
423+
401424
### Line hiding
402425

403426
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/).
@@ -520,6 +543,10 @@ throw our overflow and background on `.gatsby-highlight`, and use
520543
`display:block` on `.gatsby-highlight-code-line` – all of this coming together
521544
to facilitate the desired line highlight behavior.
522545

546+
**NOTE:** This is _not_ the same as the Diff code-block full line-length
547+
highlighting for diff'd lines. Those "line highlights" work great beyond
548+
the above.
549+
523550
### Line numbering
524551

525552
Because [the line numbering PrismJS plugin][7] runs client-side, a few adaptations were required to make it work:

packages/gatsby-remark-prismjs/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = (
1717
noInlineHighlight = false,
1818
showLineNumbers: showLineNumbersGlobal = false,
1919
showInvisibles = false,
20+
showDiffHighlight = false,
2021
languageExtensions = [],
2122
prompt = {
2223
user: `root`,
@@ -78,7 +79,11 @@ module.exports = (
7879
//
7980
// @see https://github.com/gatsbyjs/gatsby/issues/1486
8081
const className = `${classPrefix}${languageName}${
81-
diffLanguage ? `-${diffLanguage}` : ``
82+
diffLanguage
83+
? showDiffHighlight
84+
? `-${diffLanguage} diff-highlight`
85+
: `-${diffLanguage}`
86+
: ``
8287
}`
8388

8489
// Replace the node with the markup we need to make

0 commit comments

Comments
 (0)