Categorygithub.com/mikkelricky/go-markdown-code-runner

# README

Markdown code runner

Show and run fenced code blocks in Markdown files.

Support languages

  • bash
  • php
  • shell, sh (will be run with bash)
  • zsh

Quick start

Assuming Go is installed, you can run a quick test with

go run github.com/mikkelricky/go-markdown-code-runner@latest show

to list all code blocks in README.md in the current folder.

Installation

Install Go and install go-markdown-code-runner with

go install github.com/mikkelricky/go-markdown-code-runner@latest

See Compile and install packages and dependencies for details on where go-markdown-code-runner is actually installed.

To set things straight and clean up, it may be worth running these commands:

# Create the default installation location
mkdir -p ~/go/bin
# Clear GOBIN to use the default installation location
go env -w GOBIN=''
go install github.com/mikkelricky/go-markdown-code-runner@latest

Add ~/go/bin to your PATH, e.g.

# ~/.zshrc
export PATH=$PATH:$HOME/go/bin

See Completions for details in how to set up completions for your terminal.

Usage

go-markdown-code-runner [options] [filename]

If no filename is specified, input is read from stdin or README.md is used.

Show all code block (in README.md):

go-markdown-code-runner show

Show how to run blocks:

go-markdown-code-runner show --verbose

Show a single block:

# By name, i.e. a code block with name=coding-standards-markdown
go-markdown-code-runner show --verbose coding-standards-markdown
# By index
go-markdown-code-runner show --verbose 5

Run a block:

# Run the block with name "test"
go-markdown-code-runner run example

Highlight the commands being run:

go-markdown-code-runner run example --echo '\n👉 '

(internally --echo uses PS4)

It works with both stdout and stderr:

go-markdown-code-runner run example-streams

# Silence stdout
go-markdown-code-runner run example-streams > /dev/null

# Silence stderr
go-markdown-code-runner run example-streams 2&> /dev/null

Completions

go-markdown-code-runner can automatically generate completions for four shells:

go-markdown-code-runner help completion

Zsh

Load completions in Zsh by adding

# ~/.zshrc
eval "$(go-markdown-code-runner completion zsh)"; compdef _go-markdown-code-runner go-markdown-code-runner

to your ~/.zshrc. If you're cool, you do it all from the command line:

cat >> ~/.zshrc <<'EOF'
eval "$(go-markdown-code-runner completion zsh)"; compdef _go-markdown-code-runner go-markdown-code-runner
EOF

And if you're even cooler, you use go-markdown-code-runner to run the code snippet above by its name:

go-markdown-code-runner run zshrc-install-completion --verbose

Examples

date
pwd
echo "$0"
echo "$0"
echo "$0"
<?php

echo PHP_VERSION, PHP_EOL;
echo "This is written on stdout"
(>&2 echo "This is written on stderr")
<?php

echo (new DateTimeImmutable())->format(DateTimeInterface::ATOM);
<html>
  <title><?php echo (new DateTimeImmutable())->format(DateTimeInterface::ATOM); ?></title>
</html>

# Packages

No description provided by the author
No description provided by the author