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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
out
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ It adds a CodeLens action to run shell snippets in any markdown file.

<img width="478" alt="screenshot" src="https://user-images.githubusercontent.com/1406778/64465935-20934280-d163-11e9-8e0e-8dfd0f86fad3.png">

### How does it work?
## How does it work?

`markdown-command-runner` detects `` ``` ``, `` ```sh `` and `` ```bash `` snippets in markdown files an adds
a `Run command in terminal` action at the top.

When clicked the associated snippet is run the current active terminal or a new one if it is busy.

### Point of execution

You can config wether it should change the directory to the one where the README
is by adding this in your .vscode/settings.json:

```json
{
"markdown-command-runner": {
"change-directory": "true"
}
}
```

*This extension is not actively maintained but contributions are welcome!*
7 changes: 6 additions & 1 deletion src/commandCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import vscode = require('vscode');
import path = require('path');

export class CommandCodeLensProvider implements vscode.CodeLensProvider {
onDidChangeCodeLenses?: vscode.Event<void>;

provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
var codeLenses = [];
const lines = document.getText().split('\n');
const directory = path.dirname(document.fileName);

const changeDirectoryConfig = vscode.workspace.getConfiguration().get('markdown-command-runner.change-directory');

var inCommand = false;
var currentCommand = '';
Expand All @@ -14,10 +18,11 @@ export class CommandCodeLensProvider implements vscode.CodeLensProvider {
const line = lines[i].trim();
if (inCommand) {
if (line === '```') {
const cdCmd = changeDirectoryConfig == "true" ? `cd ${directory};` : ''
const cmd: vscode.Command = {
title: 'Run command in terminal',
command: 'markdown.run.command',
arguments: [{ command: currentCommand }]
arguments: [{ command: `${cdCmd} ${currentCommand}` }]
};
codeLenses.push(
new vscode.CodeLens(new vscode.Range(new vscode.Position(commandStartLine, 0), new vscode.Position(commandStartLine + 1, 0)), cmd)
Expand Down