Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as React from "react";

import ClipboardJS from "clipboard";

import { extractTextFromTokens } from "./codeUtils";
import { extractTextFromTokens, stripShellPromptPrefix } from "./codeUtils";

import { Icon } from "../icons/Icon";
import { SnippetOptionallyScrollablePart } from "./SnippetOptionallyScrollablePart";
Expand Down Expand Up @@ -129,11 +129,11 @@ class SnippetContainer extends React.Component {

this.clipboard = new ClipboardJS(this.copyToClipboardNode, {
text: () => {
const { linesOfCode, tokensForClipboardProvider } = this.props;
const { lang, linesOfCode, tokensForClipboardProvider } = this.props;
this.setState({ displayCopied: true });
this.startRemoveFeedbackTimer();

return extractTextFromTokens(tokensToUse());
return stripShellPromptPrefix(extractTextFromTokens(tokensToUse()), lang);

function tokensToUse() {
if (tokensForClipboardProvider) {
Expand Down
20 changes: 20 additions & 0 deletions znai-reactjs/src/doc-elements/code-snippets/codeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ export function extractTextFromTokens(tokens) {
return tokens.map(t => tokenToText(t)).join('')
}

const shellLangs = new Set(['bash', 'sh', 'shell', 'shell-session', 'zsh', 'console'])

/**
* strips the optional "$ " prompt prefix from each line of a shell snippet,
* so that copied commands can be pasted into a terminal as-is
* @param text snippet text
* @param lang snippet language
* @return {String}
*/
export function stripShellPromptPrefix(text, lang) {
if (!lang || !shellLangs.has(lang.toLowerCase())) {
return text
}

return text
.split('\n')
.map(line => line.replace(/^(\s*)\$ /, '$1'))
.join('\n')
}

function tokenToText(token) {
if (typeof token === 'string') {
return token
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2026 znai maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, it, expect } from 'vitest';

import { stripShellPromptPrefix } from "./codeUtils";

describe("stripShellPromptPrefix", () => {
it("removes the $ prompt prefix from bash snippet lines", () => {
const text = '$ echo hello\n$ ls -la'
expect(stripShellPromptPrefix(text, 'bash')).toEqual('echo hello\nls -la')
})

it("keeps lines without a prompt untouched, e.g. line continuations and output", () => {
const text = '$ my-tool run \\\n' +
' -flag value\n' +
'output line\n' +
'\n' +
'$ another-command'
expect(stripShellPromptPrefix(text, 'sh')).toEqual(
'my-tool run \\\n' +
' -flag value\n' +
'output line\n' +
'\n' +
'another-command')
})

it("preserves indentation before the prompt", () => {
expect(stripShellPromptPrefix(' $ echo indented', 'bash')).toEqual(' echo indented')
})

it("does not remove $ that is part of a variable reference", () => {
expect(stripShellPromptPrefix('$HOME/bin/tool', 'bash')).toEqual('$HOME/bin/tool')
})

it("does not change snippets of other languages", () => {
expect(stripShellPromptPrefix('$ looks like prompt', 'java')).toEqual('$ looks like prompt')
expect(stripShellPromptPrefix('$ looks like prompt', undefined)).toEqual('$ looks like prompt')
})
})