Skip to content
/ mdextract Public

Extract code blocks from markdown files

License

Notifications You must be signed in to change notification settings

ntnn/mdextract

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

mdextract

A Go tool and GitHub Action to extract code blocks from markdown files with support for filtering by tags, as well as extracting code blocks form withing HTML comments, e.g. to enable "hidden" code blocks that are only relevant for CI but not for human readers.

tags are words after the backticks on the first line of a fenced code block:

```go ci test
// This Go code block has tags: "ci" and "test"
package main
```

Multi mode and file tags

mdextract can be run in multi mode, in which case code blocks are extracted into multiple files based on the file tag:

```python ci file=script.py
print("This code block will be extracted to script.py")
```

Blocks without a file tag are ignored in multi mode.

Examples

Extract all Go code blocks:

./bin/mdextract -tags go README.md

Extract code blocks with specific tags and write to a file:

./bin/mdextract -tags go,ci -output extracted.go README.md

Extract example files in code blocks:

```yaml ci
this:
    is: an example
    of: a yaml file
```
./bin/mdextract -tags yaml,ci -output example.yaml README.md

GitHub Action Usage

Prerequisites

The action requires Go to be available in the workflow:

- uses: actions/setup-go

The inputs are documented in the action.yml file.

Examples are available in .github/workflows/example.yml.