Skip to content

v3 #108

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

Merged
merged 25 commits into from
Jul 10, 2020
Merged

v3 #108

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e8712bd
Add concept of gutter cells
andrewbranch May 11, 2020
00abb5a
Adjust rendering and styling
andrewbranch May 12, 2020
a926410
Finish layout, add test, make it work with line highlighting
andrewbranch May 16, 2020
d1f1e29
Fix copy and paste
andrewbranch May 16, 2020
0bf5f92
Merge pull request #95 from andrewbranch/gutters
andrewbranch May 17, 2020
ab2c700
Update alpha publishing to point to v3 branch
andrewbranch May 17, 2020
2d94be7
Update other v2 reference
andrewbranch May 17, 2020
cb774a2
Another 2
andrewbranch May 17, 2020
ffbfe2c
Add diff highlighting line transformer (#88)
janosh May 23, 2020
adb1ecd
Add GraphQL testing infrastructure
andrewbranch May 24, 2020
6e77d7b
Update workflows and CONTRIBUTING.md
andrewbranch May 24, 2020
326e278
Add missing proc.kill
andrewbranch May 24, 2020
b991180
Ignore tty message?
andrewbranch May 24, 2020
d9d7757
Remove meta from test query because apparently languageId isn’t stable
andrewbranch May 25, 2020
d954ccd
Totally ignore tty error
andrewbranch May 25, 2020
71b79db
Update snapshot
andrewbranch May 25, 2020
d22f6db
Shallow checkout vscode
andrewbranch May 25, 2020
844f98c
Merge pull request #98 from andrewbranch/graphql-testing
andrewbranch May 25, 2020
315ced0
Merge branch 'master' into v3
andrewbranch Jun 7, 2020
37eeb47
Pick a reasonable default for line highlight color based on theme bac…
andrewbranch Jul 6, 2020
2436e75
Officially deprecate `codeFenceNode`
andrewbranch Jul 10, 2020
482a929
Add line number docs
andrewbranch Jul 10, 2020
9feb604
Add diff highlighting docs
andrewbranch Jul 10, 2020
6aa753b
Remove old `colorTheme` option
andrewbranch Jul 10, 2020
952eba7
Update baselines
andrewbranch Jul 10, 2020
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
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ jobs:
with:
node-version: ${{ matrix.node }}
- name: install
run: npm ci
run: yarn install --frozen-lockfile
- name: test
run: npm test
run: yarn test
publish:
if: github.event_name == 'release'
needs: build_test
Expand All @@ -45,9 +45,9 @@ jobs:
node-version: '12'
registry-url: 'https://registry.npmjs.org'
- name: install
run: npm ci
run: yarn install --frozen-lockfile
- name: build
run: npm run build
run: yarn build
- name: publish production
run: npm publish
env:
Expand All @@ -64,9 +64,9 @@ jobs:
node-version: '12'
registry-url: 'https://registry.npmjs.org'
- name: install
run: npm ci
run: yarn install --frozen-lockfile
- name: build
run: npm run build
run: yarn build
- name: publish alpha
run: |
version=`node .github/workflows/getVersion.js 3.0.0`
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ lib/extensions
# Autogenerated content
lib/grammars
lib/themes
test/integration/viewer/report.html
test/html/viewer/report.html
18 changes: 10 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ PRs introducing new ideas are always welcome, but might be declined if opened wi

## Local development setup

Prerequisites: development is supported on the current [Node LTS](https://nodejs.org/).
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.

1. Fork and clone the repo.
2. Run `npm install`
2. Run `yarn`

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`.
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`.

## Tests

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`.
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`.

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`.
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`.

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.
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.

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.

## Debugging

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

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

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.
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.
14 changes: 13 additions & 1 deletion MIGRATING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Migration Guide

## v2 → v3

### Line highlighting is visible without custom CSS.

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing verb here: "...the plugin the luminance of ..."


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 🤷🏻‍♂️

### The `codeFenceNode` property of option callback arguments is deprecated.

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.

## v1 → v2

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

### Known issues

- Usage with [gatsby-plugin-mdx](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-mdx) requires [email protected] or later.
- Usage with [gatsby-plugin-mdx](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-mdx) requires [email protected] or later.
147 changes: 134 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ If you’re updating from v1.x.x to v2.x.x, see [MIGRATING.md](./MIGRATING.md).
- [Extra stuff](#extra-stuff)
- [Inline code highlighting](#inline-code-highlighting)
- [Line highlighting](#line-highlighting)
- [Line numbers](#line-numbers)
- [Diff highlighting](#diff-highlighting)
- [Using different themes for different code fences](#using-different-themes-for-different-code-fences)
- [Arbitrary code fence options](#arbitrary-code-fence-options)
- [Options reference](#options-reference)
Expand Down Expand Up @@ -199,7 +201,7 @@ The following languages and themes can be used without [installing third-party e

</details>

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.
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.

### Themes

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

`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):

<table>
<thead><tr><th>Markdown</th><th>Rendered result</th></thead>
<tbody>
<tr>
<td>

````md
```js{1,3-5}
this.isLine(1); // highlighted
Expand All @@ -367,8 +375,24 @@ this.isLine(5); // highlighted
```
````

</td>
<td>

![][line-highlighting-meta]

</td>
</tr>
</tbody>
</table>

Comment directives are also supported:

<table>
<thead><tr><th>Markdown</th><th>Rendered result</th></thead>
<tbody>
<tr>
<td>

````md
```js
function constant(value) {
Expand All @@ -387,24 +411,114 @@ const zero = [0, 1, 2, 3, 4, 5]
```
````

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:
</td>
<td>

![][line-highlighting-comment]

</td>
</tr>
</tbody>
</table>

You can customize the default background color and left border width and color for the highlighted lines by setting CSS variables:

```css
:root {
--grvsc-line-highlighted-background-color: rgba(255, 255, 255, 0.2); /* default: transparent */
--grvsc-line-highlighted-border-color: rgba(255, 255, 255, 0.5); /* default: transparent */
--grvsc-line-highlighted-border-width: 2px; /* default: 4px */
--grvsc-line-highlighted-background-color: rgba(255, 255, 255, 0.2);
--grvsc-line-highlighted-border-color: rgba(255, 255, 255, 0.5);
--grvsc-line-highlighted-border-width: 2px;
}
```

or by setting custom styles on the lines:
### Line numbers

```css
.grvsc-container .grvsc-line-highlighted {
background-color: rgba(255, 255, 255, 0.2);
box-shadow: inset 2px 0 0 0 rgba(255, 255, 255, 0.5);
With code fence info:

````md
```js {numberLines}
import * as React from 'react';

React.createElement('span', {});
```
````

![Rendered result of the example code above][line-numbering-with-code-fence-info]

With code fence info specifying a starting line:

````md
```js {numberLines: 21}
return 'blah';
```
````

![Rendered result of the example code above][line-numbering-starting-line]

With a comment:

````md
```ts
function getDefaultLineTransformers(pluginOptions, cache) {
return [
one, // L4
two,
three
];
}
```
````

![Rendered result of the example code above][line-numbering-with-a-comment]

With both:

````md
```ts {numberLines}
import * as React from 'react';

// ...

function SomeComponent(props) { // L29
return <div />;
}
```
````

![Rendered result of the example code above][line-numbering-with-both]

The line number cell’s styling can be overridden on the `.grvsc-line-number` class.

### Diff highlighting

You can combine syntax highlighting with diff highlighting:

<table>
<thead><tr><th>Markdown</th><th>Rendered result</th></thead>
<tbody>
<tr>
<td>

````md
```ts {diff}
function add(x, y) {
- return x + x;
+ return x + y;
}
```
````

</td>
<td>

![][diff-highlighting]

</td>
</tr>
</tbody>
</table>

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.

### Using different themes for different code fences

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

```js
{
theme: ({ parsedOptions, language, markdownNode, codeFenceNode }) => {
theme: ({ parsedOptions, language, markdownNode, node }) => {
// 'language' is 'jsx', in this case
// 'markdownNode' is the gatsby-transformer-remark GraphQL node
// 'codeFenceNode' is the Markdown AST node of the current code fence
// 'node' is the Markdown AST node of the current code fence
// 'parsedOptions' is your parsed object that looks like this:
// {
// theme: 'Monokai',
Expand All @@ -436,7 +550,7 @@ Line numbers and ranges aren’t the only things you can pass as options on your
// }
return parsedOptions.theme || 'Dark+ (default dark)';
},
wrapperClassName: ({ parsedOptions, language, markdownNode, codeFenceNode }) => '';
wrapperClassName: ({ parsedOptions, language, markdownNode, node }) => '';
}
```

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