diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5cb9ab34fa3..ec64a81a5ad 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: diff --git a/.vuepress/config.js b/.vuepress/config.js index 0f851b89909..713fd9ca400 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, @@ -192,8 +194,54 @@ export default defineUserConfig({ }, }), shikiPlugin({ - theme: 'dark-plus', + themes: { + dark: 'dark-plus', + 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 = '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', + }; + } + return code; + }, + }, + ], langs: [ 'csv', 'nushell', diff --git a/book/custom_commands.md b/book/custom_commands.md index aa6360f54f0..eb942e6e597 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 14a1eb33baf..e249feda8a6 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 00000000000..95f164fbf96 --- /dev/null +++ b/tools/highlight.nu @@ -0,0 +1,19 @@ +const DELIMITER = "BEGIN EXAMPLE" + +def main [] { + let source = ( + $in + # prune leading caret + | str replace -r '^[>>]\s+' '' + ) + 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 + } +}