Skip to content

Commit db5dedb

Browse files
committed
Use nu-highlight for syntax highlighting, switch code blocks to one-dark-pro theme
1 parent 04b6773 commit db5dedb

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

.vuepress/config.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { execFileSync } from 'node:child_process';
23
import { defineUserConfig } from '@vuepress/cli';
34
import { gitPlugin } from '@vuepress/plugin-git';
45
import { feedPlugin } from '@vuepress/plugin-feed';
@@ -10,6 +11,7 @@ import { copyCodePlugin } from '@vuepress/plugin-copy-code';
1011
import { docsearchPlugin } from '@vuepress/plugin-docsearch';
1112
import { backToTopPlugin } from '@vuepress/plugin-back-to-top';
1213
import { mediumZoomPlugin } from '@vuepress/plugin-medium-zoom';
14+
import { transformerRenderWhitespace } from '@shikijs/transformers';
1315

1416
import {
1517
navbarDe,
@@ -194,18 +196,46 @@ export default defineUserConfig({
194196
shikiPlugin({
195197
themes: {
196198
dark: 'dark-plus',
197-
vitessedark: 'vitesse-dark', // pre-load vitesse-dark for ansi code blocks
199+
onedarkpro: 'one-dark-pro', // pre-load one-dark-pro for ansi code blocks
198200
},
199201
lineNumbers: 10,
200202
transformers: [
203+
transformerRenderWhitespace(),
204+
{
205+
// highlight nushell code blocks with nu-highlight
206+
preprocess(code, options) {
207+
if (this.options.lang != 'nushell' && this.options.lang != 'nu') {
208+
return code;
209+
}
210+
211+
this.options.defaultColor = 'onedarkpro';
212+
// this doesn't work at the top-level for some reason
213+
this.options.colorReplacements = {
214+
// make one-dark-pro background color the same as dark-plus
215+
'#282c34': '#1e1e1e',
216+
// HACK: change color of comments, since nu-highlight can't highlight them
217+
'#abb2bf': '#80858f',
218+
};
219+
220+
// switch language to ansi, and highlight code blocks with nu-highlight
221+
this.options.lang = 'ansi';
222+
let result = execFileSync('nu', ['--stdin', 'tools/highlight.nu'], {
223+
input: code,
224+
});
225+
return result.toString().trimEnd();
226+
},
227+
},
228+
// use one-dark-pro theme for ansi code blocks
201229
{
202230
preprocess(code, options) {
203231
if (options.lang == 'ansi') {
204-
this.options.defaultColor = 'vitessedark';
232+
this.options.defaultColor = 'onedarkpro';
205233
// this doesn't work at the top-level for some reason
206234
this.options.colorReplacements = {
207-
// make vitesse-dark background color the same as dark-plus
208-
'#121212': '#1e1e1e',
235+
// make one-dark-pro background color the same as dark-plus
236+
'#282c34': '#1e1e1e',
237+
// HACK: change color of comments, since nu-highlight can't highlight them
238+
'#abb2bf': '#80858f',
209239
};
210240
}
211241
return code;

book/custom_commands.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ def "str mycommand" [] {
287287
Now we can call our custom command as if it were a built-in subcommand of [`str`](/commands/docs/str.md):
288288

289289
```nu
290+
def "str mycommand" [] {
291+
"hello"
292+
}
293+
# BEGIN EXAMPLE
290294
str mycommand
291295
```
292296

@@ -444,12 +448,16 @@ greet World
444448

445449
If we try to run the above, Nushell will tell us that the types don't match:
446450

447-
```nu
448-
error: Type Error
449-
┌─ shell:6:7
450-
451-
5 │ greet world
452-
│ ^^^^^ Expected int
451+
```ansi
452+
Error: nu::parser::parse_mismatch
453+
454+
 × Parse mismatch during operation.
455+
╭─[entry #1:5:7]
456+
4 │
457+
5 │ greet World
458+
·  ──┬──
459+
 · ╰── expected int
460+
 ╰────
453461
```
454462

455463
::: tip Cool!

book/operators.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ let cats = ["Mr. Humphrey Montgomery", Kitten]
167167

168168
The below code is an equivalent version using `append`:
169169
```nushell
170+
let dogs = [Spot, Teddy, Tommy]
171+
let cats = ["Mr. Humphrey Montgomery", Kitten]
172+
# BEGIN EXAMPLE
170173
$dogs |
171174
append Polly |
172175
append ($cats | each { |elt| $"($elt) \(cat\)" }) |
@@ -209,6 +212,8 @@ You can make a new record with all the fields of `$config` and some new addition
209212
operator. You can use the spread multiple records inside a single record literal.
210213

211214
```nushell
215+
let config = { path: /tmp, limit: 5 }
216+
# BEGIN EXAMPLE
212217
{
213218
...$config,
214219
users: [alice bob],

tools/highlight.nu

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const DELIMITER = "BEGIN EXAMPLE"
2+
3+
def main [] {
4+
let source = $in
5+
let highlighted = $source | nu-highlight
6+
if $DELIMITER in $source {
7+
$highlighted
8+
| lines
9+
| skip while {|line| "BEGIN EXAMPLE" not-in $line}
10+
| skip 1 # skip example line itself
11+
| to text
12+
} else {
13+
$highlighted
14+
}
15+
}

0 commit comments

Comments
 (0)