Skip to content

Commit 32d7e9f

Browse files
authored
Add codeblocks (#509)
* Add codeblocks * Add copy button and fine-tune typography * fine-tuning * Fix code
1 parent 6db3204 commit 32d7e9f

File tree

13 files changed

+639
-36
lines changed

13 files changed

+639
-36
lines changed

docs/contribute/move.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ When you move a source file or folder, you must also update all inbound and outb
66

77
Move a file or folder from one location to another and update all links in the documentation. For example:
88

9-
```
9+
```bash
1010
docs-builder mv ./old-location/ia.md ./new-location/ia.md
1111
```
1212

@@ -16,7 +16,7 @@ The `docset.yml` and `toc.yml` files are not automatically updated when using th
1616

1717
## `docs-builder mv --help`
1818

19-
```
19+
```bash
2020
Usage: mv [arguments...] [options...] [-h|--help] [--version]
2121

2222
Move a file or folder from one location to another and update all links in the documentation
@@ -28,4 +28,4 @@ Arguments:
2828
Options:
2929
--dry-run <bool?> Dry run the move operation (Default: null)
3030
-p|--path <string?> Defaults to the`{pwd}` folder (Default: null)
31-
```
31+
```

docs/migration/guide/how-to-set-up-docs-previews.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ If the `path-pattern` input does not match any changes in the PR, the workflow w
1616
This way you only build and deploy the docs when there are changes to the docs and you can still set it as a required status check.
1717

1818

19-
::::{dropdown} .github/workflows/docs-build.yml
20-
:open:
19+
::::{tab-set}
20+
21+
:::{tab-item} .github/workflows/docs-build.yml
2122

2223
```yaml
2324
---
@@ -45,6 +46,8 @@ jobs:
4546
2. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-build.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-build.yml)
4647
3. his should be the path to your docs folder.
4748

49+
:::
50+
4851
::::
4952

5053

@@ -57,8 +60,10 @@ We are aware of the security implications of using `pull_request_target` as desc
5760
The workflow never checks out the code and doesn't use any user modifiable inputs (e.g. PR title).
5861
:::
5962

60-
::::{dropdown} .github/workflows/docs-cleanup.yml
61-
:open:
63+
::::{tab-set}
64+
65+
:::{tab-item} .github/workflows/docs-cleanup.yml
66+
6267
```yaml
6368
---
6469
name: docs-cleanup
@@ -80,6 +85,8 @@ jobs:
8085
1. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-cleanup.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-cleanup.yml)
8186
2. No permissions to read content
8287

88+
:::
89+
8390
::::
8491

8592
## Required Status Checks

docs/syntax/code.md

Lines changed: 121 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1-
# Code blocks
1+
# Code
22

33
Code blocks can be used to display multiple lines of code. They preserve formatting and provide syntax highlighting when possible.
44

5-
## Syntax
5+
## Code block
66

7-
Start and end a code block with a code fence. A code fence is a sequence of at least three consecutive backtick characters (~```~). You can optionally add a language identifier to enable syntax highlighting.
7+
Start and end a code block with a code fence. A code fence is a sequence of at least three consecutive backtick characters `` ``` ``. You can optionally add a language identifier to enable syntax highlighting.
8+
9+
10+
::::{tab-set}
11+
12+
:::{tab-item} Output
813

9-
````markdown
1014
```yaml
1115
project:
1216
title: MyST Markdown
1317
github: https://github.com/jupyter-book/mystmd
1418
```
15-
````
1619
20+
:::
21+
22+
:::{tab-item} Markdown
23+
24+
````markdown
1725
```yaml
1826
project:
1927
title: MyST Markdown
2028
github: https://github.com/jupyter-book/mystmd
2129
```
30+
````
31+
32+
:::
33+
34+
::::
2235
2336
### Code callouts
2437
@@ -30,59 +43,97 @@ Add `<\d+>` to the end of a line to explicitly create a code callout.
3043

3144
An ordered list with the same number of items as callouts must follow the code block. If the number of list items doesn’t match the callouts, docs-builder will throw an error.
3245

33-
````markdown
46+
47+
::::{tab-set}
48+
49+
:::{tab-item} Output
50+
3451
```yaml
3552
project:
3653
license:
3754
content: CC-BY-4.0 <1>
3855
```
3956

4057
1. The license
41-
````
4258

59+
:::
60+
61+
62+
:::{tab-item} Markdown
63+
64+
````markdown
4365
```yaml
4466
project:
4567
license:
4668
content: CC-BY-4.0 <1>
4769
```
4870

4971
1. The license
72+
````
73+
74+
:::
75+
76+
::::
5077

5178

5279
#### Magic Callouts
5380

5481
If a code block contains code comments in the form of `//` or `#`, callouts will be magically created 🪄.
5582

56-
````markdown
83+
84+
::::{tab-set}
85+
86+
:::{tab-item} Output
87+
5788
```csharp
5889
var apiKey = new ApiKey("<API_KEY>"); // Set up the api key
5990
var client = new ElasticsearchClient("<CLOUD_ID>", apiKey);
6091
```
61-
````
6292

93+
:::
94+
95+
:::{tab-item} Markdown
96+
97+
````markdown
6398
```csharp
6499
var apiKey = new ApiKey("<API_KEY>"); // Set up the api key
65100
var client = new ElasticsearchClient("<CLOUD_ID>", apiKey);
66101
```
102+
````
103+
:::
104+
105+
106+
::::
67107

68108
Code comments must follow code to be hoisted as a callout. For example:
69109

70-
````markdown
110+
::::{tab-set}
111+
112+
:::{tab-item} Output
113+
71114
```csharp
72115
// THIS IS NOT A CALLOUT
73116
var apiKey = new ApiKey("<API_KEY>"); // This is a callout
74117
var client = new ElasticsearchClient("<CLOUD_ID>", apiKey);
75118
```
76-
````
77119

120+
:::
121+
122+
:::{tab-item} Markdown
123+
124+
````markdown
78125
```csharp
79126
// THIS IS NOT A CALLOUT
80127
var apiKey = new ApiKey("<API_KEY>"); // This is a callout
81128
var client = new ElasticsearchClient("<CLOUD_ID>", apiKey);
82129
```
130+
````
131+
132+
:::
83133

134+
::::
84135

85-
## Console code blocks
136+
### Console code blocks
86137

87138
:::{note}
88139
This feature is still being developed.
@@ -92,7 +143,10 @@ We document a lot of API endpoints at Elastic. For these endpoints, we support `
92143

93144
In a console code block, the first line is highlighted as a dev console string and the remainder as json:
94145

95-
````markdown
146+
::::{tab-set}
147+
148+
:::{tab-item} Output
149+
96150
```console
97151
GET /mydocuments/_search
98152
{
@@ -102,8 +156,12 @@ GET /mydocuments/_search
102156
}
103157
}
104158
```
105-
````
106159

160+
:::
161+
162+
:::{tab-item} Markdown
163+
164+
````markdown
107165
```console
108166
GET /mydocuments/_search
109167
{
@@ -113,3 +171,52 @@ GET /mydocuments/_search
113171
}
114172
}
115173
```
174+
````
175+
176+
:::
177+
178+
::::
179+
180+
## Inline code
181+
182+
Use backticks to create an inline code span.
183+
Inline code spans are useful for short code snippets or variable names.
184+
185+
186+
### Inline code in a paragraph
187+
188+
::::{tab-set}
189+
190+
:::{tab-item} Output
191+
192+
This `code` is inline.
193+
194+
:::
195+
196+
:::{tab-item} Markdown
197+
198+
````markdown
199+
This `code` is inline.
200+
````
201+
:::
202+
203+
::::
204+
205+
### Inline code in a heading
206+
207+
::::{tab-set}
208+
209+
:::{tab-item} Output
210+
211+
## This `code` is in a heading.
212+
213+
:::
214+
215+
:::{tab-item} Markdown
216+
217+
````markdown
218+
## This `code` is in a heading.
219+
````
220+
:::
221+
222+
::::
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copy buttons */
2+
button.copybtn {
3+
position: absolute;
4+
display: flex;
5+
top: .3em;
6+
right: .3em;
7+
width: 1.7em;
8+
height: 1.7em;
9+
opacity: 0;
10+
transition: opacity 0.3s, border .3s, background-color .3s;
11+
user-select: none;
12+
cursor: pointer;
13+
padding: 0;
14+
border: none;
15+
outline: none;
16+
border-radius: 0.4em;
17+
/* The colors that GitHub uses */
18+
border: #1b1f2426 1px solid;
19+
/*background-color: #f6f8fa;*/
20+
color: var(--color-gray-400);
21+
}
22+
23+
button.copybtn.success {
24+
/*border-color: #22863a;*/
25+
color: #22863a;
26+
}
27+
28+
button.copybtn svg {
29+
stroke: currentColor;
30+
width: 1.5em;
31+
height: 1.5em;
32+
padding: 0.1em;
33+
}
34+
35+
div.highlight {
36+
position: relative;
37+
}
38+
39+
/* Show the copybutton */
40+
.highlight:hover button.copybtn, button.copybtn.success {
41+
opacity: 1;
42+
}
43+
44+
.highlight button.copybtn:hover {
45+
/*background-color: rgb(235, 235, 235);*/
46+
}
47+
48+
.highlight button.copybtn:active {
49+
/*background-color: rgb(187, 187, 187);*/
50+
}
51+
52+
/**
53+
* A minimal CSS-only tooltip copied from:
54+
* https://codepen.io/mildrenben/pen/rVBrpK
55+
*
56+
* To use, write HTML like the following:
57+
*
58+
* <p class="o-tooltip--left" data-tooltip="Hey">Short</p>
59+
*/
60+
.o-tooltip--left {
61+
position: relative;
62+
}
63+
64+
.o-tooltip--left.success:after {
65+
}
66+
67+
.o-tooltip--left:after {
68+
opacity: 0;
69+
visibility: hidden;
70+
position: absolute;
71+
content: attr(data-tooltip);
72+
padding: .2em;
73+
font-size: .8em;
74+
left: -.2em;
75+
/*background: grey;*/
76+
color: var(--color-gray-400);
77+
white-space: nowrap;
78+
z-index: 2;
79+
border-radius: 2px;
80+
transform: translateX(-102%) translateY(0);
81+
transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1);
82+
}
83+
84+
.o-tooltip--left:hover:after {
85+
display: block;
86+
opacity: 1;
87+
visibility: visible;
88+
transform: translateX(-100%) translateY(0);
89+
transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1);
90+
transition-delay: .5s;
91+
}
92+
93+
/* By default the copy button shouldn't show up when printing a page */
94+
@media print {
95+
button.copybtn {
96+
display: none;
97+
}
98+
}

0 commit comments

Comments
 (0)