From 04b67737342917af26eee6fcc933aa16e5795f70 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Sat, 28 Dec 2024 14:55:54 -0500 Subject: [PATCH 1/4] Apply vitesse-dark theme only for ansi code blocks --- .vuepress/config.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/.vuepress/config.js b/.vuepress/config.js index 0f851b8990..5927bc5926 100755 --- a/.vuepress/config.js +++ b/.vuepress/config.js @@ -192,8 +192,26 @@ export default defineUserConfig({ }, }), shikiPlugin({ - theme: 'dark-plus', + themes: { + dark: 'dark-plus', + vitessedark: 'vitesse-dark', // pre-load vitesse-dark for ansi code blocks + }, lineNumbers: 10, + transformers: [ + { + preprocess(code, options) { + if (options.lang == 'ansi') { + this.options.defaultColor = 'vitessedark'; + // this doesn't work at the top-level for some reason + this.options.colorReplacements = { + // make vitesse-dark background color the same as dark-plus + '#121212': '#1e1e1e', + }; + } + return code; + }, + }, + ], langs: [ 'csv', 'nushell', From db5dedb54ca809ae076f6b9e4cb6fff51b68179c Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Sat, 28 Dec 2024 16:16:21 -0500 Subject: [PATCH 2/4] Use nu-highlight for syntax highlighting, switch code blocks to one-dark-pro theme --- .vuepress/config.js | 38 ++++++++++++++++++++++++++++++++++---- book/custom_commands.md | 20 ++++++++++++++------ book/operators.md | 5 +++++ tools/highlight.nu | 15 +++++++++++++++ 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 tools/highlight.nu diff --git a/.vuepress/config.js b/.vuepress/config.js index 5927bc5926..713fd9ca40 100755 --- a/.vuepress/config.js +++ b/.vuepress/config.js @@ -1,4 +1,5 @@ import path from 'node:path'; +import { execFileSync } from 'node:child_process'; import { defineUserConfig } from '@vuepress/cli'; import { gitPlugin } from '@vuepress/plugin-git'; import { feedPlugin } from '@vuepress/plugin-feed'; @@ -10,6 +11,7 @@ import { copyCodePlugin } from '@vuepress/plugin-copy-code'; import { docsearchPlugin } from '@vuepress/plugin-docsearch'; import { backToTopPlugin } from '@vuepress/plugin-back-to-top'; import { mediumZoomPlugin } from '@vuepress/plugin-medium-zoom'; +import { transformerRenderWhitespace } from '@shikijs/transformers'; import { navbarDe, @@ -194,18 +196,46 @@ export default defineUserConfig({ shikiPlugin({ themes: { dark: 'dark-plus', - vitessedark: 'vitesse-dark', // pre-load vitesse-dark for ansi code blocks + onedarkpro: 'one-dark-pro', // pre-load one-dark-pro for ansi code blocks }, lineNumbers: 10, transformers: [ + transformerRenderWhitespace(), + { + // highlight nushell code blocks with nu-highlight + preprocess(code, options) { + if (this.options.lang != 'nushell' && this.options.lang != 'nu') { + return code; + } + + this.options.defaultColor = 'onedarkpro'; + // this doesn't work at the top-level for some reason + this.options.colorReplacements = { + // make one-dark-pro background color the same as dark-plus + '#282c34': '#1e1e1e', + // HACK: change color of comments, since nu-highlight can't highlight them + '#abb2bf': '#80858f', + }; + + // switch language to ansi, and highlight code blocks with nu-highlight + this.options.lang = 'ansi'; + let result = execFileSync('nu', ['--stdin', 'tools/highlight.nu'], { + input: code, + }); + return result.toString().trimEnd(); + }, + }, + // use one-dark-pro theme for ansi code blocks { preprocess(code, options) { if (options.lang == 'ansi') { - this.options.defaultColor = 'vitessedark'; + this.options.defaultColor = 'onedarkpro'; // this doesn't work at the top-level for some reason this.options.colorReplacements = { - // make vitesse-dark background color the same as dark-plus - '#121212': '#1e1e1e', + // make one-dark-pro background color the same as dark-plus + '#282c34': '#1e1e1e', + // HACK: change color of comments, since nu-highlight can't highlight them + '#abb2bf': '#80858f', }; } return code; diff --git a/book/custom_commands.md b/book/custom_commands.md index aa6360f54f..eb942e6e59 100644 --- a/book/custom_commands.md +++ b/book/custom_commands.md @@ -287,6 +287,10 @@ def "str mycommand" [] { Now we can call our custom command as if it were a built-in subcommand of [`str`](/commands/docs/str.md): ```nu +def "str mycommand" [] { + "hello" +} +# BEGIN EXAMPLE str mycommand ``` @@ -444,12 +448,16 @@ greet World If we try to run the above, Nushell will tell us that the types don't match: -```nu -error: Type Error - ┌─ shell:6:7 - │ -5 │ greet world - │ ^^^^^ Expected int +```ansi +Error: nu::parser::parse_mismatch + + × Parse mismatch during operation. + ╭─[entry #1:5:7] + 4 │ + 5 │ greet World + ·  ──┬── + · ╰── expected int + ╰──── ``` ::: tip Cool! diff --git a/book/operators.md b/book/operators.md index 14a1eb33ba..e249feda8a 100644 --- a/book/operators.md +++ b/book/operators.md @@ -167,6 +167,9 @@ let cats = ["Mr. Humphrey Montgomery", Kitten] The below code is an equivalent version using `append`: ```nushell +let dogs = [Spot, Teddy, Tommy] +let cats = ["Mr. Humphrey Montgomery", Kitten] +# BEGIN EXAMPLE $dogs | append Polly | 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 operator. You can use the spread multiple records inside a single record literal. ```nushell +let config = { path: /tmp, limit: 5 } +# BEGIN EXAMPLE { ...$config, users: [alice bob], diff --git a/tools/highlight.nu b/tools/highlight.nu new file mode 100644 index 0000000000..7a6f3d17aa --- /dev/null +++ b/tools/highlight.nu @@ -0,0 +1,15 @@ +const DELIMITER = "BEGIN EXAMPLE" + +def main [] { + let source = $in + let highlighted = $source | nu-highlight + if $DELIMITER in $source { + $highlighted + | lines + | skip while {|line| "BEGIN EXAMPLE" not-in $line} + | skip 1 # skip example line itself + | to text + } else { + $highlighted + } +} From 10951cdc5914b8d2d676439ad63f8b2f085e6193 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Sat, 28 Dec 2024 16:27:46 -0500 Subject: [PATCH 3/4] Fix highlighting in blocks with carets --- tools/highlight.nu | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/highlight.nu b/tools/highlight.nu index 7a6f3d17aa..95f164fbf9 100644 --- a/tools/highlight.nu +++ b/tools/highlight.nu @@ -1,7 +1,11 @@ const DELIMITER = "BEGIN EXAMPLE" def main [] { - let source = $in + let source = ( + $in + # prune leading caret + | str replace -r '^[>>]\s+' '' + ) let highlighted = $source | nu-highlight if $DELIMITER in $source { $highlighted From 31cc2034398c525d0b28e90cb60986d228a77fd7 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Sat, 28 Dec 2024 16:44:25 -0500 Subject: [PATCH 4/4] Add nushell to CI --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5cb9ab34fa..ec64a81a5a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,9 @@ jobs: steps: - uses: actions/checkout@v4.1.2 + - name: Install Nushell + run: cargo install nu --locked --no-default-features + - name: Setup Node uses: actions/setup-node@v4.0.2 with: