-
Notifications
You must be signed in to change notification settings - Fork 462
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
132ikl
wants to merge
4
commits into
nushell:main
Choose a base branch
from
132ikl:nu-highlight
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ jobs: | |
steps: | ||
- uses: actions/[email protected] | ||
|
||
- name: Install Nushell | ||
run: cargo install nu --locked --no-default-features | ||
|
||
- name: Setup Node | ||
uses: actions/[email protected] | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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', | ||
}; | ||
Comment on lines
+211
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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