Skip to content
Merged
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
6 changes: 2 additions & 4 deletions src/plugins/command-line/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ d-r-- 10/14/2015 5:06 PM Videos
data-continuation-str="\" >
```

```bash { .command-line data-filter-output="(out)" data-continuation-str="\" }
export MY_VAR=123
<pre class="command-line" data-filter-output="(out)" data-continuation-str="\"><code class="language-bash">export MY_VAR=123
echo "hello"
(out)hello
echo one \
Expand All @@ -174,8 +173,7 @@ three
(out)one two three
(out)
echo "goodbye"
(out)goodbye
```
(out)goodbye</code></pre>

## Line continuation with Output (PowerShell)

Expand Down
7 changes: 5 additions & 2 deletions src/plugins/command-line/command-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const Self = {
existingPrompt.remove();
}

const codeLines = env.code.split('\n');
// Browsers draw no line after a final line break, which every Markdown code fence ends with,
// so it gets no prompt
const finalLineBreak = env.code.endsWith('\n') ? '\n' : '';
const codeLines = env.code.replace(/\n$/, '').split('\n');

commandLine.numberOfLines = codeLines.length;
const outputLines = (commandLine.outputLines = []);
Expand Down Expand Up @@ -104,7 +107,7 @@ const Self = {
}
}

env.code = codeLines.join('\n');
env.code = codeLines.join('\n') + finalLineBreak;
},
'before-insert': env => {
const commandLine = (env.commandLine ??= {});
Expand Down
50 changes: 50 additions & 0 deletions tests/plugins/command-line/basic-functionality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { assert } from 'chai';
import { createTestSuite } from '../../helper/prism-dom-util.js';

describe('Command Line', () => {
const { it } = createTestSuite({
languages: 'bash',
plugins: 'command-line',
});

/**
* @param {import('../../types.d.ts').PrismDOM<{}>} dom
* @param {number} expectedPrompts
* @param {string} code
*/
function test ({ document, Prism }, expectedPrompts, code) {
document.body.innerHTML = `<pre class="command-line language-bash"><code>${code}</code></pre>`;
Prism.highlightAll();

assert.strictEqual(
document.querySelectorAll('.command-line-prompt > span').length,
expectedPrompts
);
}

it('should prompt every line', dom => {
test(dom, 2, 'cd ~/.vim\nvim vimrc');
test(dom, 3, 'cd ~/.vim\n\nvim vimrc');
});

// Markdown code fences end with one
it('should not prompt after a final line break', dom => {
test(dom, 2, 'cd ~/.vim\nvim vimrc\n');
test(dom, 3, 'cd ~/.vim\nvim vimrc\n\n');
});

// Copy to Clipboard copies the element's text
it('should keep the final line break in the code', ({ document, Prism }) => {
document.body.innerHTML = `<pre class="command-line language-bash"><code>ls\nfile.txt\n</code></pre>`;
Prism.highlightAll();

assert.strictEqual(document.querySelector('code')?.textContent, 'ls\nfile.txt\n');
});

it('should keep output on the last line', ({ document, Prism }) => {
document.body.innerHTML = `<pre class="command-line language-bash" data-output="2"><code>ls\nfile.txt\n</code></pre>`;
Prism.highlightAll();

assert.strictEqual(document.querySelector('.token.output')?.textContent, 'file.txt');
});
});
Loading