Skip to content

Commit f789932

Browse files
authored
feat(build): improve code blocks and snippets (#875)
* refactor: don't hardcode language names * docs: fix typo * feat: support specifying language while importing code snippets * feat: support interpolation inside code blocks * docs: update v-pre escaping * fix: ignore starting `>` in case of shell commands fixes #861, fixes #471, fixes #884
1 parent d1a2c76 commit f789932

File tree

13 files changed

+100
-106
lines changed

13 files changed

+100
-106
lines changed

.github/contributing.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ You will need [pnpm](https://pnpm.io)
2727

2828
After cloning the repo, run:
2929

30-
```bash
30+
```sh
3131
# install the dependencies of the project
3232
$ pnpm install
3333
```
@@ -36,22 +36,22 @@ $ pnpm install
3636

3737
At first, execute the `pnpm run build` command.
3838

39-
```bash
39+
```sh
4040
$ pnpm run build
4141
```
4242

4343
You only need to do this once for your fresh project. It copies required files and makes sure everything is in place. After this, you only need to run `dev` related commands.
4444

4545
The easiest way to start testing out VitePress is to tweak the VitePress docs. You may run `pnpm run docs` to boot up VitePress documentation site locally, with live reloading of the source code.
4646

47-
```bash
47+
```sh
4848
$ pnpm run docs
4949
```
5050

5151
After executing the above command, visit http://localhost:3000 and try modifying the source code. You'll get live update.
5252

5353
If you don't need docs site up and running, you may start VitePress local dev environment with `pnpm run dev`.
5454

55-
```bash
55+
```sh
5656
$ pnpm run dev
5757
```

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/docs
22
/examples
3-
*.css
43
*.md
54
*.vue
65
dist

docs/guide/getting-started.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ VitePress is currently in `alpha` status. It is already suitable for out-of-the-
1010

1111
Create and change into a new directory.
1212

13-
```bash
13+
```sh
1414
$ mkdir vitepress-starter && cd vitepress-starter
1515
```
1616

1717
Then, initialize with your preferred package manager.
1818

19-
```bash
19+
```sh
2020
$ yarn init
2121
```
2222

2323
## Step. 2: Install VitePress
2424

2525
Add VitePress and Vue as dev dependencies for the project.
2626

27-
```bash
27+
```sh
2828
$ yarn add --dev vitepress vue
2929
```
3030

@@ -64,7 +64,7 @@ On PNPM, add this in your `package.json`:
6464

6565
Create your first document.
6666

67-
```bash
67+
```sh
6868
$ mkdir docs && echo '# Hello VitePress' > docs/index.md
6969
```
7070

@@ -86,7 +86,7 @@ Add some scripts to `package.json`.
8686

8787
Serve the documentation site in the local server.
8888

89-
```bash
89+
```sh
9090
$ yarn docs:dev
9191
```
9292

docs/guide/markdown.md

+12-17
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ In addition to a single line, you can also specify multiple single lines, ranges
279279
**Input**
280280

281281
````
282-
```js{1,4,6-7}
282+
```js{1,4,6-8}
283283
export default { // Highlighted
284284
data () {
285285
return {
@@ -296,7 +296,7 @@ export default { // Highlighted
296296

297297
**Output**
298298

299-
```js{1,4,6-7}
299+
```js{1,4,6-8}
300300
export default { // Highlighted
301301
data () {
302302
return {
@@ -346,20 +346,12 @@ It also supports [line highlighting](#line-highlighting-in-code-blocks):
346346

347347
**Code file**
348348

349-
<!--lint disable strong-marker-->
350-
351349
<<< @/snippets/snippet.js
352350

353-
<!--lint enable strong-marker-->
354-
355351
**Output**
356352

357-
<!--lint disable strong-marker-->
358-
359353
<<< @/snippets/snippet.js{2}
360354

361-
<!--lint enable strong-marker-->
362-
363355
::: tip
364356
The value of `@` corresponds to the source root. By default it's the VitePress project root, unless `srcDir` is configured.
365357
:::
@@ -374,19 +366,22 @@ You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/co
374366

375367
**Code file**
376368

377-
<!--lint disable strong-marker-->
378-
379369
<<< @/snippets/snippet-with-region.js
380370

381-
<!--lint enable strong-marker-->
382-
383371
**Output**
384372

385-
<!--lint disable strong-marker-->
386-
387373
<<< @/snippets/snippet-with-region.js#snippet{1}
388374

389-
<!--lint enable strong-marker-->
375+
You can also specify the language inside the braces (`{}`) like this:
376+
377+
```md
378+
<<< @/snippets/snippet.cs{c#}
379+
380+
<!-- with line highlighting: -->
381+
<<< @/snippets/snippet.cs{1,2,4-6 c#}
382+
```
383+
384+
This is helpful if source language cannot be inferred from your file extension.
390385

391386
## Markdown File Inclusion
392387

docs/guide/using-vue.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const { page } = useData()
6262

6363
## Escaping
6464

65-
By default, fenced code blocks are automatically wrapped with `v-pre`. To display raw mustaches or Vue-specific syntax inside inline code snippets or plain text, you need to wrap a paragraph with the `v-pre` custom container:
65+
By default, fenced code blocks are automatically wrapped with `v-pre`, unless you have set some language with `-vue` suffix like `js-vue` (in that case you can use Vue-style interpolation inside fences). To display raw mustaches or Vue-specific syntax inside inline code snippets or plain text, you need to wrap a paragraph with the `v-pre` custom container:
6666

6767
**Input**
6868

src/client/theme-default/composables/copy-code.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function useCopyCode() {
1111
nextTick(() => {
1212
document
1313
.querySelectorAll<HTMLSpanElement>(
14-
'.vp-doc div[class*="language-"]>span.copy'
14+
'.vp-doc div[class*="language-"] > span.copy'
1515
)
1616
.forEach(handleElement)
1717
})
@@ -67,19 +67,19 @@ async function copyToClipboard(text: string) {
6767
function handleElement(el: HTMLElement) {
6868
el.onclick = () => {
6969
const parent = el.parentElement
70-
71-
if (!parent) {
70+
const sibling = el.nextElementSibling as HTMLPreElement | null
71+
if (!parent || !sibling) {
7272
return
7373
}
7474

75-
const isShell =
76-
parent.classList.contains('language-sh') ||
77-
parent.classList.contains('language-bash')
75+
const isShell = /language-(shellscript|shell|bash|sh|zsh)/.test(
76+
parent.classList.toString()
77+
)
7878

79-
let { innerText: text = '' } = parent
79+
let { innerText: text = '' } = sibling
8080

8181
if (isShell) {
82-
text = text.replace(/^ *\$ /gm, '')
82+
text = text.replace(/^ *(\$|>) /gm, '')
8383
}
8484

8585
copyToClipboard(text).then(() => {

src/client/theme-default/styles/base.css

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ b {
7575
a,
7676
area,
7777
button,
78-
[role="button"],
78+
[role='button'],
7979
input,
8080
label,
8181
select,
@@ -142,13 +142,13 @@ textarea {
142142

143143
button {
144144
padding: 0;
145-
font-family: inherit;;
145+
font-family: inherit;
146146
background-color: transparent;
147147
background-image: none;
148148
}
149149

150150
button,
151-
[role="button"] {
151+
[role='button'] {
152152
cursor: pointer;
153153
}
154154

@@ -197,7 +197,7 @@ input::-webkit-inner-spin-button {
197197
margin: 0;
198198
}
199199

200-
input[type="number"] {
200+
input[type='number'] {
201201
-moz-appearance: textfield;
202202
}
203203

src/client/theme-default/styles/components/vp-doc.css

+6-36
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
color: inherit;
204204
font-weight: 600;
205205
text-decoration: underline;
206-
transition: opacity .25s;
206+
transition: opacity 0.25s;
207207
}
208208

209209
.vp-doc .custom-block a:hover {
@@ -381,7 +381,7 @@
381381
background-position: 50%;
382382
background-size: 20px;
383383
background-repeat: no-repeat;
384-
transition: opacity 0.25s;
384+
transition: opacity 0.4s;
385385
}
386386

387387
.vp-doc [class*='language-']:hover > span.copy {
@@ -414,54 +414,24 @@
414414
color: var(--vp-code-copy-code-active-text);
415415
background-color: var(--vp-code-copy-code-hover-bg);
416416
white-space: nowrap;
417-
content: "Copied";
417+
content: 'Copied';
418418
}
419419

420-
.vp-doc [class*='language-']:before {
420+
.vp-doc [class*='language-'] > span.lang {
421421
position: absolute;
422422
top: 6px;
423423
right: 12px;
424424
z-index: 2;
425425
font-size: 12px;
426426
font-weight: 500;
427427
color: var(--vp-c-text-dark-3);
428-
transition: color 0.5s, opacity 0.5s;
428+
transition: color 0.4s, opacity 0.4s;
429429
}
430430

431-
.vp-doc [class*='language-']:hover:before {
431+
.vp-doc [class*='language-']:hover > span.lang {
432432
opacity: 0;
433433
}
434434

435-
.vp-doc [class~='language-c']:before { content: 'c'; }
436-
.vp-doc [class~='language-css']:before { content: 'css'; }
437-
.vp-doc [class~='language-go']:before { content: 'go'; }
438-
.vp-doc [class~='language-html']:before { content: 'html'; }
439-
.vp-doc [class~='language-java']:before { content: 'java'; }
440-
.vp-doc [class~='language-javascript']:before { content: 'js'; }
441-
.vp-doc [class~='language-js']:before { content: 'js'; }
442-
.vp-doc [class~='language-json']:before { content: 'json'; }
443-
.vp-doc [class~='language-jsx']:before { content: 'jsx'; }
444-
.vp-doc [class~='language-less']:before { content: 'less'; }
445-
.vp-doc [class~='language-markdown']:before { content: 'md'; }
446-
.vp-doc [class~='language-md']:before { content: 'md' }
447-
.vp-doc [class~='language-php']:before { content: 'php'; }
448-
.vp-doc [class~='language-python']:before { content: 'py'; }
449-
.vp-doc [class~='language-py']:before { content: 'py'; }
450-
.vp-doc [class~='language-rb']:before { content: 'rb'; }
451-
.vp-doc [class~='language-ruby']:before { content: 'rb'; }
452-
.vp-doc [class~='language-rust']:before { content: 'rust'; }
453-
.vp-doc [class~='language-sass']:before { content: 'sass'; }
454-
.vp-doc [class~='language-scss']:before { content: 'scss'; }
455-
.vp-doc [class~='language-sh']:before { content: 'sh'; }
456-
.vp-doc [class~='language-bash']:before { content: 'sh'; }
457-
.vp-doc [class~='language-stylus']:before { content: 'styl'; }
458-
.vp-doc [class~='language-vue-html']:before { content: 'template'; }
459-
.vp-doc [class~='language-typescript']:before { content: 'ts'; }
460-
.vp-doc [class~='language-ts']:before { content: 'ts'; }
461-
.vp-doc [class~='language-tsx']:before { content: 'tsx'; }
462-
.vp-doc [class~='language-vue']:before { content: 'vue'; }
463-
.vp-doc [class~='language-yaml']:before { content: 'yaml'; }
464-
465435
/**
466436
* Component: Team
467437
* -------------------------------------------------------------------------- */

src/client/theme-default/styles/components/vp-sponsor.css

+40-15
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,63 @@
4646
gap: 4px;
4747
}
4848

49-
.vp-sponsor-grid.xmini .vp-sponsor-grid-link { height: 64px; }
50-
.vp-sponsor-grid.xmini .vp-sponsor-grid-image { max-width: 64px; max-height: 22px }
49+
.vp-sponsor-grid.xmini .vp-sponsor-grid-link {
50+
height: 64px;
51+
}
52+
.vp-sponsor-grid.xmini .vp-sponsor-grid-image {
53+
max-width: 64px;
54+
max-height: 22px;
55+
}
5156

52-
.vp-sponsor-grid.mini .vp-sponsor-grid-link { height: 72px; }
53-
.vp-sponsor-grid.mini .vp-sponsor-grid-image { max-width: 96px; max-height: 24px }
57+
.vp-sponsor-grid.mini .vp-sponsor-grid-link {
58+
height: 72px;
59+
}
60+
.vp-sponsor-grid.mini .vp-sponsor-grid-image {
61+
max-width: 96px;
62+
max-height: 24px;
63+
}
5464

55-
.vp-sponsor-grid.small .vp-sponsor-grid-link { height: 96px; }
56-
.vp-sponsor-grid.small .vp-sponsor-grid-image { max-width: 96px; max-height: 24px }
65+
.vp-sponsor-grid.small .vp-sponsor-grid-link {
66+
height: 96px;
67+
}
68+
.vp-sponsor-grid.small .vp-sponsor-grid-image {
69+
max-width: 96px;
70+
max-height: 24px;
71+
}
5772

58-
.vp-sponsor-grid.medium .vp-sponsor-grid-link { height: 112px; }
59-
.vp-sponsor-grid.medium .vp-sponsor-grid-image { max-width: 120px; max-height: 36px }
73+
.vp-sponsor-grid.medium .vp-sponsor-grid-link {
74+
height: 112px;
75+
}
76+
.vp-sponsor-grid.medium .vp-sponsor-grid-image {
77+
max-width: 120px;
78+
max-height: 36px;
79+
}
6080

61-
.vp-sponsor-grid.big .vp-sponsor-grid-link { height: 184px; }
62-
.vp-sponsor-grid.big .vp-sponsor-grid-image { max-width: 192px; max-height: 56px }
81+
.vp-sponsor-grid.big .vp-sponsor-grid-link {
82+
height: 184px;
83+
}
84+
.vp-sponsor-grid.big .vp-sponsor-grid-image {
85+
max-width: 192px;
86+
max-height: 56px;
87+
}
6388

64-
.vp-sponsor-grid[data-vp-grid="2"] .vp-sponsor-grid-item {
89+
.vp-sponsor-grid[data-vp-grid='2'] .vp-sponsor-grid-item {
6590
width: calc((100% - 4px) / 2);
6691
}
6792

68-
.vp-sponsor-grid[data-vp-grid="3"] .vp-sponsor-grid-item {
93+
.vp-sponsor-grid[data-vp-grid='3'] .vp-sponsor-grid-item {
6994
width: calc((100% - 4px * 2) / 3);
7095
}
7196

72-
.vp-sponsor-grid[data-vp-grid="4"] .vp-sponsor-grid-item {
97+
.vp-sponsor-grid[data-vp-grid='4'] .vp-sponsor-grid-item {
7398
width: calc((100% - 4px * 3) / 4);
7499
}
75100

76-
.vp-sponsor-grid[data-vp-grid="5"] .vp-sponsor-grid-item {
101+
.vp-sponsor-grid[data-vp-grid='5'] .vp-sponsor-grid-item {
77102
width: calc((100% - 4px * 4) / 5);
78103
}
79104

80-
.vp-sponsor-grid[data-vp-grid="6"] .vp-sponsor-grid-item {
105+
.vp-sponsor-grid[data-vp-grid='6'] .vp-sponsor-grid-item {
81106
width: calc((100% - 4px * 5) / 6);
82107
}
83108

0 commit comments

Comments
 (0)