Using Docrunner to Build Better Documentation

Docrunner is a powerful command line tool that provides a seamless solution for testing code within markdown files, guaranteeing that your readers always have access to accurate and working examples. It empowers developers to validate their documentation quickly, boosting confidence in the reliability of their code.

In this blog post, we will explore the capabilities of Docrunner and delve into its installation process, usage, and supported languages. Whether you’re a technical writer, open-source contributor, or software developer looking to improve your documentation, Docrunner promises to be a valuable addition to your toolkit.

Installation

Docrunner’s installation process is extremely simple, a simple command on most systems:

Shell (Mac, Linux):

$ curl -fsSL https://raw.githubusercontent.com/DudeBro249/docrunner/stable/installers/install.sh | sh

PowerShell (Windows):

$ iwr -useb https://raw.githubusercontent.com/DudeBro249/docrunner/stable/installers/install.ps1 | iex

If none of these methods work, you can also install the latest docrunner binary from the releases. Make sure to add it to PATH so you can access it from anywhere

Core Concepts

Now that we’ve installed Docrunner, let’s see how to validate some documentation:

Create a file called sample.md and add the following:

### Printing in Python:
```python
print('Hello world')
```

Now, we can test the python code in sample.md using docrunner. Run the following command:

$ docrunner run --language python --markdown-path ./sample.md

This will locate and run all python code in sample.md. Here’s the expected output:

You’ll notice that running the command not only runs the Python code within sample.md, but also creates a folder titled docrunner-build-py within the root directory:

Docrunner needs to create runnable files before executing the code in your markdown files. The build directory docrunner-build-py is where these files are stored.

INFO:

You can use the --multi-file flag with the docrunner run command to make each snippet run in a different file. Run docrunner run --help for all available options.

Configuring Options using TOML

Although docrunner allows you to pass in execution options directly through the command line, you’ll find that it is much easier to use a configuration file, simplifying your final command.

Docrunner uses TOML for configuration.

Use the following command to initialize docrunner configuration for your project:

$ docrunner init

This creates a file called docrunner.toml, where you’ll store all your custom options. You’ll also notice that docrunner populates the file with some default settings:

[docrunner]
markdown_paths = ['README.md']
multi_file = false
recursive = false

Available Settings

Let’s take a look at some of the settings docrunner allows you to change:

language

Example:

[docrunner]
language = 'python'

markdown_paths

Example:

[docrunner]
markdown_paths = [
    'README.md',
    './documentation'
]

directory_path

Example:

[docrunner]
directory_path = './custom_folder_with_dependencies'

multi_file

Example:

[docrunner]
multi_file = true

startup_command

Example:

[docrunner]
startup_command = 'npm run start'

recursive

Example:

[docrunner]
recursive = true

dotenv

Example:

[docrunner]
dotenv = './.env'

Configuration Example

Here’s an example which uses all of the above options:

[docrunner]
language = 'python'
markdown_paths = [
    'README.md',
    './documentation'
]
directory_path = './custom_folder_with_dependencies'
multi_file = true
startup_command = 'npm run start'
recursive = true
dotenv = './.env'

You can now simply use the command docrunner run without any extra options or flags, since all settings are stored in your docrunner.toml file.

Leveraging Comments in your Markdown

Ignoring Snippets

Let’s say you have a code snippet that you would like Docrunner to ignore in your markdown, because, for example, the example code you wrote connects to a SQL database that doesn’t exist.

In cases like this, you can use the <!--docrunner.ignore--> comment above the snippet to let Docrunner know that it should skip, like so:

<!--docrunner.ignore-->
```py
print('This code will not be written to a file or executed')
```

List of Available Comments

INFO:

You can stack multiple docrunner comments on the same code snippet

CI/CD Integration

Running tests manually and on local machines is extremely important and can be very useful when developing documentation. However, performing a final documentation check before your markdown is pushed to your live website or before your package is updated is vital.

To solve this, docrunner comes with its very own github action, setup-docrunner.

Here is an example of a workflow that you could create to use Docrunner in your CI/CD pipeline before your documentation changes are published:

name: Test Documentation with Docrunner

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test-docrunner-action:
    runs-on: ubuntu-20.04

    name: Test Documentation
    steps:
      - uses: actions/checkout@v2

      - name: Set Docrunner up on Workflow
        uses: DudeBro249/setup-docrunner

      - name: Use Docrunner
        run: docrunner run

Acknowledgements

Thanks for reading! If you liked Docrunner, consider starring (⭐) the repository.

You can also check out the documentation for Docrunner. It provides more details on Docrunner’s commands as well as other features such as using environment variables in your code snippets.