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: "go", "ci" and "test"
package main
```
The GitHub Action is available with ntnn/mdextract and can be used to extract code blocks in a workflow step:
- name: Extract code blocks
uses: ntnn/mdextract
with:
input: README.md TEST.md
tags: go,ci
output: extracted.goThe outputs are documented in the action.yml file.
More examples are available in the example workflow.
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 if run with tags python and ci")
```
Blocks without a file tag are ignored in multi mode.
```python ci
print("This code block will be ignored if run with tags python and ci")
```
Extract all Go code blocks:
./bin/mdextract -tags go -output - 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.md