Skip to content

Commit 4ce88a1

Browse files
andrewbranchjanosh
andauthored
v3 (#108)
* Add concept of gutter cells * Adjust rendering and styling * Finish layout, add test, make it work with line highlighting * Fix copy and paste * Update alpha publishing to point to v3 branch * Update other v2 reference * Another 2 * Add diff highlighting line transformer (#88) * fix typo in CONTRIBUTING.md * fix formatting error preventing tests in src/utils.js * wip: diff line transformer draft implementation * add diff line transformer integration test * diffLineTransformer return new v3 gutterCells prop * readme fix typo * pick default red/green colors for diff line highlighting * fix diffLineTransformer.schemaExtension, avoid mutating input params * don't require diff lines to start with +/- * Update tests, fix type errors * Remove postfix space requirement Co-authored-by: Andrew Branch <[email protected]> * Add GraphQL testing infrastructure * Update workflows and CONTRIBUTING.md * Add missing proc.kill * Ignore tty message? * Remove meta from test query because apparently languageId isn’t stable * Totally ignore tty error * Update snapshot * Shallow checkout vscode * Pick a reasonable default for line highlight color based on theme background color (#106) * Detect dark themes by luminance * Fix dependencies * Update README * Update MIGRATING * Add test, adjust light default color * Ignore report.html * Don’t cache in GraphQL tests * Officially deprecate `codeFenceNode` * Add line number docs * Add diff highlighting docs * Remove old `colorTheme` option * Update baselines Co-authored-by: Janosh Riebesell <[email protected]>
1 parent 0bdaa18 commit 4ce88a1

File tree

99 files changed

+37294
-6250
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+37294
-6250
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ jobs:
3030
with:
3131
node-version: ${{ matrix.node }}
3232
- name: install
33-
run: npm ci
33+
run: yarn install --frozen-lockfile
3434
- name: test
35-
run: npm test
35+
run: yarn test
3636
publish:
3737
if: github.event_name == 'release'
3838
needs: build_test
@@ -45,9 +45,9 @@ jobs:
4545
node-version: '12'
4646
registry-url: 'https://registry.npmjs.org'
4747
- name: install
48-
run: npm ci
48+
run: yarn install --frozen-lockfile
4949
- name: build
50-
run: npm run build
50+
run: yarn build
5151
- name: publish production
5252
run: npm publish
5353
env:
@@ -64,9 +64,9 @@ jobs:
6464
node-version: '12'
6565
registry-url: 'https://registry.npmjs.org'
6666
- name: install
67-
run: npm ci
67+
run: yarn install --frozen-lockfile
6868
- name: build
69-
run: npm run build
69+
run: yarn build
7070
- name: publish alpha
7171
run: |
7272
version=`node .github/workflows/getVersion.js 3.0.0`

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ lib/extensions
7171
# Autogenerated content
7272
lib/grammars
7373
lib/themes
74-
test/integration/viewer/report.html
74+
test/html/viewer/report.html

CONTRIBUTING.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ PRs introducing new ideas are always welcome, but might be declined if opened wi
1010

1111
## Local development setup
1212

13-
Prerequisites: development is supported on the current [Node LTS](https://nodejs.org/).
13+
Prerequisites: development is supported on the current [Node LTS](https://nodejs.org/). Yarn is probably required to run all tests correctly, because npm fails to deduplicate certain dependencies which causes issues for running the GraphQL tests.
1414

1515
1. Fork and clone the repo.
16-
2. Run `npm install`
16+
2. Run `yarn`
1717

18-
Running `npm install` should automatically initialize or update the [vscode](./vscode) submodule, generate `src/graphql/schema.d.ts`, and populate [`lib/grammars`](lib/grammars) and [`lib/themes`](lib/themes). If any of these things are missing, you’ll errors when you try to run things later. You can rerun these steps with `npm run build`.
18+
Running `yarn` should automatically initialize or update the [vscode](./vscode) submodule, generate `src/graphql/schema.d.ts`, and populate [`lib/grammars`](lib/grammars) and [`lib/themes`](lib/themes). If any of these things are missing, you’ll get errors when you try to run things later. You can rerun these steps with `yarn build`.
1919

2020
## Tests
2121

22-
To run tests, run `npm test`. You can use Jest CLI options after a `--` separator. For example, to run just the test named “code-fence-meta”, use `npm test -- -t code-fence-meta`.
22+
To run tests, run `yarn test`. You can use any Jest CLI options. For example, to run just the test named “code-fence-meta”, use `yarn test -t code-fence-meta`.
2323

24-
Most tests are either Jest snapshot tests or HTML baseline tests against a known-good source ([`test/integration/cases/baselines`](test/integration/cases/baselines)). To update the snapshots and baselines, run `npm test -- -u`.
24+
Most tests are either Jest snapshot tests or HTML baseline tests against a known-good source ([`test/html/cases/baselines`](test/html/baselines)). To update the snapshots and baselines, run `yarn test -u`.
2525

26-
Most new tests can be integration tests. To write one, create a new folder in `test/integration/cases`, then put a Markdown file named `test.md` inside. If you want to run the plugin with custom options, place an `options.js` file whose `module.exports` is the options object to be passed to the plugin. Then, when you next run `npm test`, the baseline will be created and opened in a browser so you can view the resulting HTML. If it looks right, commit it and you’re done. If it looks wrong, you can overwrite the bad baseline by running `npm test -- -u -t name-of-test` after you’ve made changes to fix your code.
26+
Most new tests can be baseline tests. To write one, create a new folder in `test/cases`, then put a Markdown file named `test.md` inside. If you want to run the plugin with custom options, place an `options.js` file whose `module.exports` is the options object to be passed to the plugin. Then, when you next run `yarn test`, the baseline will be created and opened in a browser so you can view the resulting HTML. If it looks right, commit it and you’re done. If it looks wrong, you can overwrite the bad baseline by running `yarn test -u -t name-of-test` after you’ve made changes to fix your code.
27+
28+
Tests inside `test/cases` also generate a snapshot of a GraphQL query for the structured syntax highlighting data the plugin generates. The snapshot has a lot of data, so if you’re adding a new test case, don’t worry about manually inspecting every line of the added snapshot. They exist mostly to detect regressions, so if the snapshot looks plausibly correct, go ahead and commit it.
2729

2830
## Debugging
2931

@@ -34,7 +36,7 @@ VS Code launch scripts have been provided, so you can debug the tests or the [ex
3436
When you’ve made changes you’re happy with, ensure that you have
3537

3638
- Added a test if appropriate,
37-
- Formatted your changes through Prettier with `npm run format`,
39+
- Formatted your changes through Prettier with `yarn format`,
3840
- Maintained good JSDoc type annotations to the best of your ability.
3941

40-
This project is written in plain JavaScript, but uses TypeScript to type-check via JSDoc annotations. After tests, type checking runs (and can be run alone with `npm run check`). If types are new to you, or you have trouble getting type checking to pass, that’s ok! I’ll help you in your PR if necessary. Type checking is a great way to catch errors early, but I don’t want it to be an impediment for anyone’s contribution.
42+
This project is written in plain JavaScript, but uses TypeScript to type-check via JSDoc annotations. After tests, type checking runs (and can be run alone with `yarn check`). If types are new to you, or you have trouble getting type checking to pass, that’s ok! I’ll help you in your PR if necessary. Type checking is a great way to catch errors early, but I don’t want it to be an impediment for anyone’s contribution.

MIGRATING.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Migration Guide
22

3+
## v2 → v3
4+
5+
### Line highlighting is visible without custom CSS.
6+
7+
Previously, the default line highlight background color was `transparent`, which meant a color had to be set with a CSS variable by the user. The reason for this limitation was that there is no sensible default that works for both light themes and dark themes, as a light highlight would be invisible on white, and a dark highlight would be invisible on black. As of v3, the plugin the luminance of each theme’s background color and selects a translucent white or translucent black highlight color as a default.
8+
9+
This is unlikely to break existing sites, as the CSS variables continue to work as a customization. However, as part of upgrading to v3, you may be able to remove the variables you set if you’re happy with the new defaults. If, for some reason, you were relying on the old behavior of an invisible default, sorry, don’t do that 🤷🏻‍♂️
10+
11+
### The `codeFenceNode` property of option callback arguments is deprecated.
12+
13+
The `theme` and `wrapperClassName` plugin options accept a callback function that receives an object with a `codeFenceNode` containing the original Remark MDAST node for the code block currently being highlighted. That property has been renamed `node`. I’m like a million percent sure nobody uses this, but if I don’t say it here, someone will complain when I remove it in 4.0.
14+
315
## v1 → v2
416

517
### Extensions are no longer downloaded automatically.
@@ -49,4 +61,4 @@ The new `theme` option also supports some options that `colorTheme` did not; you
4961

5062
### Known issues
5163

52-
- Usage with [gatsby-plugin-mdx](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-mdx) requires [email protected] or later.
64+
- Usage with [gatsby-plugin-mdx](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-mdx) requires [email protected] or later.

README.md

Lines changed: 134 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ If you’re updating from v1.x.x to v2.x.x, see [MIGRATING.md](./MIGRATING.md).
2626
- [Extra stuff](#extra-stuff)
2727
- [Inline code highlighting](#inline-code-highlighting)
2828
- [Line highlighting](#line-highlighting)
29+
- [Line numbers](#line-numbers)
30+
- [Diff highlighting](#diff-highlighting)
2931
- [Using different themes for different code fences](#using-different-themes-for-different-code-fences)
3032
- [Arbitrary code fence options](#arbitrary-code-fence-options)
3133
- [Options reference](#options-reference)
@@ -199,7 +201,7 @@ The following languages and themes can be used without [installing third-party e
199201

200202
</details>
201203

202-
Language names are resolve case-insensitively by any aliases and file extensions listed in the grammar’s metadata. For example, a code fence with C++ code in it can use [any of these language codes](https://github.com/Microsoft/vscode/blob/da3c97f3668393ebfcb9f8208d7616018d6d1859/extensions/cpp/package.json#L20-L21). You could also check the [built-in grammar manifest](https://unpkg.com/[email protected]/lib/grammars/manifest.json) for an exact list of mappings.
204+
Language names are resolved case-insensitively by any aliases and file extensions listed in the grammar’s metadata. For example, a code fence with C++ code in it can use [any of these language codes](https://github.com/Microsoft/vscode/blob/da3c97f3668393ebfcb9f8208d7616018d6d1859/extensions/cpp/package.json#L20-L21). You could also check the [built-in grammar manifest](https://unpkg.com/[email protected]/lib/grammars/manifest.json) for an exact list of mappings.
203205

204206
### Themes
205207

@@ -357,6 +359,12 @@ See [`inlineCode`](#inlinecode) in the options reference for more API details.
357359

358360
`gatsby-remark-vscode` offers the same line-range-after-language-name strategy of highlighting or emphasizing lines as [gatsby-remark-prismjs](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-prismjs):
359361

362+
<table>
363+
<thead><tr><th>Markdown</th><th>Rendered result</th></thead>
364+
<tbody>
365+
<tr>
366+
<td>
367+
360368
````md
361369
```js{1,3-5}
362370
this.isLine(1); // highlighted
@@ -367,8 +375,24 @@ this.isLine(5); // highlighted
367375
```
368376
````
369377

378+
</td>
379+
<td>
380+
381+
![][line-highlighting-meta]
382+
383+
</td>
384+
</tr>
385+
</tbody>
386+
</table>
387+
370388
Comment directives are also supported:
371389

390+
<table>
391+
<thead><tr><th>Markdown</th><th>Rendered result</th></thead>
392+
<tbody>
393+
<tr>
394+
<td>
395+
372396
````md
373397
```js
374398
function constant(value) {
@@ -387,24 +411,114 @@ const zero = [0, 1, 2, 3, 4, 5]
387411
```
388412
````
389413

390-
You need to pick your own background color, and optionally a left border width and color, for the highlighted lines. This can be done by setting CSS variables:
414+
</td>
415+
<td>
416+
417+
![][line-highlighting-comment]
418+
419+
</td>
420+
</tr>
421+
</tbody>
422+
</table>
423+
424+
You can customize the default background color and left border width and color for the highlighted lines by setting CSS variables:
391425

392426
```css
393427
:root {
394-
--grvsc-line-highlighted-background-color: rgba(255, 255, 255, 0.2); /* default: transparent */
395-
--grvsc-line-highlighted-border-color: rgba(255, 255, 255, 0.5); /* default: transparent */
396-
--grvsc-line-highlighted-border-width: 2px; /* default: 4px */
428+
--grvsc-line-highlighted-background-color: rgba(255, 255, 255, 0.2);
429+
--grvsc-line-highlighted-border-color: rgba(255, 255, 255, 0.5);
430+
--grvsc-line-highlighted-border-width: 2px;
397431
}
398432
```
399433

400-
or by setting custom styles on the lines:
434+
### Line numbers
401435

402-
```css
403-
.grvsc-container .grvsc-line-highlighted {
404-
background-color: rgba(255, 255, 255, 0.2);
405-
box-shadow: inset 2px 0 0 0 rgba(255, 255, 255, 0.5);
436+
With code fence info:
437+
438+
````md
439+
```js {numberLines}
440+
import * as React from 'react';
441+
442+
React.createElement('span', {});
443+
```
444+
````
445+
446+
![Rendered result of the example code above][line-numbering-with-code-fence-info]
447+
448+
With code fence info specifying a starting line:
449+
450+
````md
451+
```js {numberLines: 21}
452+
return 'blah';
453+
```
454+
````
455+
456+
![Rendered result of the example code above][line-numbering-starting-line]
457+
458+
With a comment:
459+
460+
````md
461+
```ts
462+
function getDefaultLineTransformers(pluginOptions, cache) {
463+
return [
464+
one, // L4
465+
two,
466+
three
467+
];
406468
}
407469
```
470+
````
471+
472+
![Rendered result of the example code above][line-numbering-with-a-comment]
473+
474+
With both:
475+
476+
````md
477+
```ts {numberLines}
478+
import * as React from 'react';
479+
480+
// ...
481+
482+
function SomeComponent(props) { // L29
483+
return <div />;
484+
}
485+
```
486+
````
487+
488+
![Rendered result of the example code above][line-numbering-with-both]
489+
490+
The line number cell’s styling can be overridden on the `.grvsc-line-number` class.
491+
492+
### Diff highlighting
493+
494+
You can combine syntax highlighting with diff highlighting:
495+
496+
<table>
497+
<thead><tr><th>Markdown</th><th>Rendered result</th></thead>
498+
<tbody>
499+
<tr>
500+
<td>
501+
502+
````md
503+
```ts {diff}
504+
function add(x, y) {
505+
- return x + x;
506+
+ return x + y;
507+
}
508+
```
509+
````
510+
511+
</td>
512+
<td>
513+
514+
![][diff-highlighting]
515+
516+
</td>
517+
</tr>
518+
</tbody>
519+
</table>
520+
521+
The highlight color can be customized with the CSS variables `--grvsc-line-diff-add-background-color` and `--grvsc-line-diff-del-background-color`. The default color is static and might not be accessible with all syntax themes. Consider contrast ratios and choose appropriate colors when using this feature.
408522

409523
### Using different themes for different code fences
410524

@@ -424,10 +538,10 @@ Line numbers and ranges aren’t the only things you can pass as options on your
424538

425539
```js
426540
{
427-
theme: ({ parsedOptions, language, markdownNode, codeFenceNode }) => {
541+
theme: ({ parsedOptions, language, markdownNode, node }) => {
428542
// 'language' is 'jsx', in this case
429543
// 'markdownNode' is the gatsby-transformer-remark GraphQL node
430-
// 'codeFenceNode' is the Markdown AST node of the current code fence
544+
// 'node' is the Markdown AST node of the current code fence
431545
// 'parsedOptions' is your parsed object that looks like this:
432546
// {
433547
// theme: 'Monokai',
@@ -436,7 +550,7 @@ Line numbers and ranges aren’t the only things you can pass as options on your
436550
// }
437551
return parsedOptions.theme || 'Dark+ (default dark)';
438552
},
439-
wrapperClassName: ({ parsedOptions, language, markdownNode, codeFenceNode }) => '';
553+
wrapperClassName: ({ parsedOptions, language, markdownNode, node }) => '';
440554
}
441555
```
442556

@@ -550,3 +664,10 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for development instructions.
550664
[templates-own]: https://user-images.githubusercontent.com/3277153/56853802-5e847e00-68c8-11e9-8468-dedcd8bcab78.png
551665
[solidity-others]: https://user-images.githubusercontent.com/3277153/56853799-5e847e00-68c8-11e9-8895-535d9e0d555c.png
552666
[solidity-own]: https://user-images.githubusercontent.com/3277153/56853800-5e847e00-68c8-11e9-9c83-5e76146d5e46.png
667+
[line-highlighting-meta]: https://user-images.githubusercontent.com/3277153/86545712-6fc21500-bee5-11ea-8a83-71d04f595ef4.png
668+
[line-highlighting-comment]: https://user-images.githubusercontent.com/3277153/86545710-6e90e800-bee5-11ea-9f4d-33278d9312d7.png
669+
[line-numbering-with-a-comment]: https://user-images.githubusercontent.com/3277153/87123264-3ff37400-c23b-11ea-8ae6-80cbfcf6b6a0.png
670+
[line-numbering-with-code-fence-info]: https://user-images.githubusercontent.com/3277153/87122757-5ea53b00-c23a-11ea-8fbc-c85917433345.png
671+
[line-numbering-with-both]: https://user-images.githubusercontent.com/3277153/87122755-5ea53b00-c23a-11ea-8fe8-144aea7aa952.png
672+
[line-numbering-starting-line]: https://user-images.githubusercontent.com/3277153/87122747-5c42e100-c23a-11ea-9a06-923c699c0a0b.png
673+
[diff-highlighting]: https://user-images.githubusercontent.com/3277153/87123984-aa58e400-c23c-11ea-87b3-3f66afcd795d.png

0 commit comments

Comments
 (0)