Skip to content

Commit d423920

Browse files
Kirill BolotskyPepe Cano
authored andcommitted
refactor: code blocks api update
- set a strict list of language available for highlight: bash, javascript, json, markup, diff, go, plain - adjusted styling - added language declaration guard in Code - updated md files accordingly
1 parent 4d11a1e commit d423920

File tree

160 files changed

+581
-548
lines changed

Some content is hidden

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

160 files changed

+581
-548
lines changed

CONTRIBUTING_FILE_FORMAT.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,22 @@ To be able to switch between different code tabs, we have to repeat the headerfu
296296

297297
### Notes on code blocks
298298

299+
#### Languages
300+
301+
There is a curated list of available languages that you should use when working with code blocks:
302+
303+
1. `bash` covers all terminal code
304+
2. `javascript` for blocks with JS code
305+
3. `json` for JSON structures
306+
4. `diff` for showing diffs
307+
5. `markup` covers html and xml
308+
6. `go` for Go code
309+
7. `plain` for everything else, plain unhighlighted text
310+
311+
Every other language, including languages from list but with grammar or case mistakes will be rendered as a `plain` by default.
312+
313+
Let us know if there is necessity to bring highlight support for another language.
314+
299315
#### Line numbers
300316

301317
'{"labels": ["Nice code!", "This one is better", "Oh my.."], "lineNumbers": [true, true, true]}'

src/components/shared/code/code.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,27 @@ import React, { useRef } from 'react';
77

88
import styles from './code.module.scss';
99

10+
const SUPPORTED_LANGS = [
11+
'bash',
12+
'javascript',
13+
'json',
14+
'markup',
15+
'go',
16+
'plain',
17+
'diff',
18+
];
1019
const DEFAULT_HEIGHT = `100%`;
1120
const MAX_HEIGHT = 400;
1221

22+
const getLanguageDeclaration = (str) => {
23+
if (!str) return 'plain';
24+
const lang = str.split('-')[1];
25+
if (SUPPORTED_LANGS.includes(lang)) {
26+
return lang;
27+
}
28+
return 'plain';
29+
};
30+
1331
const Code = ({ children, showLineNumbers, showHeightToggler }) => {
1432
if (!children) return null;
1533

@@ -43,7 +61,7 @@ const Code = ({ children, showLineNumbers, showHeightToggler }) => {
4361
<Highlight
4462
{...defaultProps}
4563
code={children.props.children}
46-
language={children.props.className?.split('-')?.[1] ?? 'plain'}
64+
language={getLanguageDeclaration(children?.props?.className)}
4765
>
4866
{({ className, tokens, getLineProps, getTokenProps }) => (
4967
<pre

src/components/shared/code/code.module.scss

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
color: #c3e88d;
5959
}
6060
}
61-
&[class*='language-html'] {
61+
&[class*='language-markup'] {
6262
.tag {
6363
color: #00cdff;
6464
}
65-
.attr-name {
65+
.attr-name,
66+
.prolog {
6667
color: rgb(195, 232, 141);
6768
}
6869
.attr-value {
@@ -104,20 +105,23 @@
104105
}
105106

106107
&.operator {
107-
background-color: transparent !important;
108-
color: rgb(181, 181, 181) !important;
108+
background-color: transparent;
109+
color: rgb(181, 181, 181);
109110
}
110111

111112
&.constant {
112-
color: $color-accent-secondary !important;
113+
color: $color-accent-secondary;
113114
}
114115

115116
&.function {
116117
color: rgb(255, 255, 255);
117118
}
118119

119120
&.punctuation {
120-
color: $color-tertiary !important;
121+
color: $color-tertiary;
122+
}
123+
&.regex {
124+
color: #dd4a68;
121125
}
122126
}
123127
}

src/data/markdown/docs/01 guides/01 Getting started/02 Installation.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: 'Installation'
66

77
### Debian/Ubuntu
88

9-
```shell
9+
```bash
1010
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
1111
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
1212
sudo apt-get update
@@ -19,13 +19,13 @@ sudo apt-get install k6
1919
> command due to firewalls or proxies blocking their requests. If you experience this issue, you may try this
2020
> alternative approach instead:
2121
>
22-
> ```shell
22+
> ```bash
2323
> wget -q -O - https://bintray.com/user/downloadSubjectPublicKey?username=bintray | sudo apt-key add -
2424
> ```
2525
2626
### Red Hat/CentOS
2727
28-
```shell
28+
```bash
2929
wget https://bintray.com/loadimpact/rpm/rpm -O bintray-loadimpact-rpm.repo
3030
sudo mv bintray-loadimpact-rpm.repo /etc/yum.repos.d/
3131
sudo yum install k6
@@ -35,7 +35,7 @@ sudo yum install k6
3535

3636
<CodeGroup labels={['Brew']}>
3737

38-
```shell
38+
```bash
3939
brew install k6
4040
```
4141

@@ -54,7 +54,7 @@ Install the binary in your `PATH` to run **k6** from any location.
5454

5555
<CodeGroup labels={['Docker']}>
5656

57-
```shell
57+
```bash
5858
docker pull loadimpact/k6
5959
```
6060

src/data/markdown/docs/01 guides/01 Getting started/03 Running k6.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ Then run k6 using this command:
2626

2727
<CodeGroup labels={["CLI", "Docker", "Docker in Win PowerShell"]}>
2828

29-
```shell
29+
```bash
3030
$ k6 run script.js
3131
```
3232

33-
```shell
33+
```bash
3434
# When using the `k6` docker image, you can't just give the script name since
3535
# the script file will not be available to the container as it runs. Instead
3636
# you must tell k6 to read `stdin` by passing the file name as `-`. Then you
@@ -40,7 +40,7 @@ $ k6 run script.js
4040
$ docker run -i loadimpact/k6 run - <script.js
4141
```
4242

43-
```shell
43+
```bash
4444
$ cat script.js | docker run -i loadimpact/k6 run -
4545
```
4646

@@ -52,15 +52,15 @@ Now we'll try running a load test with more than 1 virtual user and a slightly l
5252

5353
<CodeGroup labels={["CLI", "Docker", "Docker in Win PowerShell"]}>
5454

55-
```shell
55+
```bash
5656
k6 run --vus 10 --duration 30s script.js
5757
```
5858

59-
```shell
59+
```bash
6060
docker run -i loadimpact/k6 run --vus 10 --duration 30s - <script.js
6161
```
6262

63-
```shell
63+
```bash
6464
cat script.js | docker run -i loadimpact/k6 run --vus 10 --duration 30s -
6565
```
6666

@@ -95,7 +95,7 @@ running. Code _outside_ of it is called "init code", and is run only once per VU
9595

9696
<CodeGroup labels={[""]}>
9797

98-
```js
98+
```javascript
9999
// init code
100100

101101
export default function( {
@@ -138,15 +138,15 @@ Then you just run the script without those parameters on the command line:
138138
139139
<CodeGroup labels={["CLI", "Docker", "Docker in Win PowerShell"]}>
140140
141-
```shell
141+
```bash
142142
$ k6 run script.js
143143
```
144144
145-
```shell
145+
```bash
146146
$ docker run -i loadimpact/k6 run - <script.js
147147
```
148148
149-
```shell
149+
```bash
150150
C:\ cat script.js | docker run -i loadimpact/k6 run -
151151
```
152152
@@ -197,7 +197,7 @@ For running cloud tests from the CLI, you must first register a k6 Cloud account
197197
198198
<CodeGroup labels={["Running a cloud test"]}>
199199
200-
```shell
200+
```bash
201201
$ k6 cloud script.js
202202
```
203203

src/data/markdown/docs/01 guides/01 Getting started/04 Results Output.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ When k6 displays the results to `stdout`, it will show the k6 logo and the follo
1919

2020
<CodeGroup labels={[]}>
2121

22-
```shell
22+
```bash
2323
execution: local
2424
output: -
2525
script: script.js
@@ -48,7 +48,7 @@ The test summary provides a general overview of your test result. The summary pr
4848

4949
<CodeGroup labels={[]}>
5050

51-
```shell
51+
```bash
5252
data_received..............: 148 MB 2.5 MB/s
5353
data_sent..................: 1.0 MB 17 kB/s
5454
http_req_blocked...........: avg=1.92ms min=1µs med=5µs max=288.73ms p(90)=11µs p(95)=17µs
@@ -75,7 +75,7 @@ vus_max....................: 100 min=100 max=100
7575

7676
<CodeGroup labels={[]}>
7777

78-
```shell
78+
```bash
7979
http_req_duration..........: avg=143.14ms min=112.87ms med=136.03ms max=1.18s p(90)=164.2ms p(95)=177.75ms
8080
```
8181

@@ -85,7 +85,7 @@ You could use the [summary-trend-stats](/using-k6/options#summary-trend-stats) o
8585

8686
<CodeGroup labels={[]}>
8787

88-
```shell
88+
```bash
8989
$ k6 run --summary-trend-stats="avg,p(99)" script.js
9090
```
9191

@@ -115,7 +115,7 @@ You can simultaneously send metrics to several outputs by using the CLI `--out`
115115

116116
<CodeGroup labels={[]}>
117117

118-
```shell
118+
```bash
119119
$ k6 run \
120120
--out json=test.json \
121121
--out influxdb=http://localhost:8086/k6
@@ -131,7 +131,7 @@ This is useful to get the aggregated test results in a machine-readable format,
131131

132132
<CodeGroup labels={[]}>
133133

134-
```shell
134+
```bash
135135
$ k6 run --summary-export=export.json script.js
136136
```
137137

src/data/markdown/docs/01 guides/02 Using k6/01 HTTP requests.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ your system. A simple example that just performs a GET request looks like this:
1010

1111
<CodeGroup labels={["http_get.js"]} lineNumbers={[true]}>
1212

13-
```js
13+
```javascript
1414
import http from 'k6/http';
1515

1616
export default function () {
@@ -104,7 +104,7 @@ all reported using one single metric:
104104

105105
<CodeGroup labels={["grouping.js" ]} lineNumbers={[true]}>
106106

107-
```js
107+
```javascript
108108
for (var id = 1; id <= 100; id++) {
109109
http.get(`http://example.com/posts/${id}`);
110110
}
@@ -119,7 +119,7 @@ You can aggregate data from dynamic URLs by explicitly setting a name tag:
119119

120120
<CodeGroup labels={["explicit_tag.js"]} lineNumbers={[true]}>
121121

122-
```js
122+
```javascript
123123
for (var id = 1; id <= 100; id++) {
124124
http.get(`http://example.com/posts/${id}`, {
125125
tags: { name: 'PostsItemURL' },
@@ -180,7 +180,7 @@ Additionally, you can use the `http.url` (v0.16.0) wrapper to set the name tag w
180180

181181
<CodeGroup labels={[ ]} lineNumbers={[true]}>
182182

183-
```js
183+
```javascript
184184
for (var id = 1; id <= 100; id++) {
185185
http.get(http.url`http://example.com/posts/${id}`);
186186
}

src/data/markdown/docs/01 guides/02 Using k6/02 Metrics.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ If you want to access the timing information from an individual HTTP request, th
4747

4848
<CodeGroup labels={["timings.js"]} lineNumbers={[true]}>
4949

50-
```js
50+
```javascript
5151
import http from 'k6/http';
5252
export default function () {
5353
var res = http.get('http://httpbin.org');
@@ -79,7 +79,7 @@ You can also create your own metrics, that are reported at the end of a load tes
7979

8080
<CodeGroup labels={["trend.js"]} lineNumbers={[true]}>
8181

82-
```js
82+
```javascript
8383
import http from 'k6/http';
8484
import { Trend } from 'k6/metrics';
8585

@@ -115,7 +115,7 @@ All values added to a custom metric can optionally be [tagged](/using-k6/tags-an
115115

116116
<CodeGroup labels={["counter.js"]} lineNumbers={[true]}>
117117

118-
```js
118+
```javascript
119119
import { Counter } from 'k6/metrics';
120120

121121
let myCounter = new Counter('my_counter');
@@ -140,7 +140,7 @@ Note that there is currently no way of accessing the value of any custom metric
140140

141141
<CodeGroup labels={["gauge.js"]} lineNumbers={[true]}>
142142

143-
```js
143+
```javascript
144144
import { Gauge } from 'k6/metrics';
145145

146146
let myGauge = new Gauge('my_gauge');
@@ -163,7 +163,7 @@ The value of `my_gauge` will be 2 at the end of the test. As with the Counter me
163163

164164
<CodeGroup labels={["trend.js"]} lineNumbers={[true]}>
165165

166-
```js
166+
```javascript
167167
import { Trend } from 'k6/metrics';
168168

169169
let myTrend = new Trend('my_trend');
@@ -187,7 +187,7 @@ By default, k6 will print average, min, max, median, 90th percentile, and 95th p
187187

188188
<CodeGroup labels={["rate.js"]} lineNumbers={[true]}>
189189

190-
```js
190+
```javascript
191191
import { Rate } from 'k6/metrics';
192192

193193
let myRate = new Rate('my_rate');

src/data/markdown/docs/01 guides/02 Using k6/03 Checks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ for example:
1313

1414
<CodeGroup labels={["check.js"]} lineNumbers={[true]}>
1515

16-
```js
16+
```javascript
1717
import { check } from 'k6';
1818
import http from 'k6/http';
1919

@@ -39,7 +39,7 @@ You may also add multiple checks within a single check() statement, like this:
3939

4040
<CodeGroup labels={["checks.js"]} lineNumbers={[true]}>
4141

42-
```js
42+
```javascript
4343
import { check } from 'k6';
4444
import http from 'k6/http';
4545

@@ -68,7 +68,7 @@ get what you want:
6868

6969
<CodeGroup labels={["check_thresholds.js"]} lineNumbers={[true]}>
7070

71-
```js
71+
```javascript
7272
import http from 'k6/http';
7373
import { check } from 'k6';
7474
import { Rate } from 'k6/metrics';

0 commit comments

Comments
 (0)