Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nu-highlight for code blocks #1719

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ jobs:
steps:
- uses: actions/[email protected]

- name: Install Nushell
run: cargo install nu --locked --no-default-features

Comment on lines +10 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Install Nu by this way is quite slow, try this one: https://github.com/hustcer/setup-nu

- name: Setup Node
uses: actions/[email protected]
with:
Expand Down
50 changes: 49 additions & 1 deletion .vuepress/config.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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',
};
Comment on lines +211 to +218
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to extract these a few lines to a function like adjustTheme, so we can reuse it later


// 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',
Expand Down
20 changes: 14 additions & 6 deletions book/custom_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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!
Expand Down
5 changes: 5 additions & 0 deletions book/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\)" }) |
Expand Down Expand Up @@ -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],
Expand Down
19 changes: 19 additions & 0 deletions tools/highlight.nu
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading