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
```
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.
Extract all Go code blocks:
./bin/mdextract -tags go README.mdExtract code blocks with specific tags and write to a file:
./bin/mdextract -tags go,ci -output extracted.go README.mdExtract example files in code blocks:
```yaml ci
this:
is: an example
of: a yaml file
```
./bin/mdextract -tags yaml,ci -output example.yaml README.mdThe action requires Go to be available in the workflow:
- uses: actions/setup-goThe inputs are documented in the action.yml file.
Examples are available in .github/workflows/example.yml.