-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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, | ||||||||||||||||||
}, | ||||||||||||||||||
}, | ||||||||||||||||||
], | ||||||||||||||||||
|
@@ -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. | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
```` | ||||||||||||||||||
```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/ | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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): | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
```js | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
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/). | ||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
the above. | ||||||||||||||||||
|
||||||||||||||||||
### Line numbering | ||||||||||||||||||
|
||||||||||||||||||
Because [the line numbering PrismJS plugin][7] runs client-side, a few adaptations were required to make it work: | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL!
There was a problem hiding this comment.
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 😀