Skip to content

kszongic/codeblock-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@kszongic/codeblock-cli

npm version npm downloads license node zero dependencies platform

Extract fenced code blocks from Markdown files. Zero dependencies. Works on Windows, macOS, and Linux.

Why?

Markdown files are full of runnable code but there is no easy way to extract them programmatically. codeblock-cli gives you surgical access to any fenced code block:

  • Filter by language - grab only the bash, yaml, or python blocks
  • Pick by index - get exactly the 3rd code block
  • List what is there - see all languages at a glance
  • Pipe-friendly - reads from stdin, writes to stdout

Install

npm install -g @kszongic/codeblock-cli

Or run directly: npx @kszongic/codeblock-cli README.md

Usage

codeblock README.md              # all blocks
codeblock --lang js README.md    # filter by language
codeblock -n 0 README.md         # first block only
codeblock --list README.md       # list languages
cat doc.md | codeblock --lang py # pipe from stdin

Options

Flag Description
-l, --lang Filter blocks by language
-n, --index Print only the nth block (0-based)
--list List languages of all blocks
--raw Output without trailing newline
-h, --help Show help
-v, --version Show version

Recipes

  • Run install commands from a README: codeblock --lang bash README.md -n 0 | bash
  • Test code examples in CI: Extract JS blocks and run them with node
  • Extract config snippets: codeblock --lang yaml SETUP.md -n 0 > config.yml
  • Audit languages in docs: Loop over markdown files with --list
  • Pipe from GitHub: curl -s <raw-url> | codeblock --lang python
  • Build a snippet library: find docs/ -name '*.md' -exec codeblock --lang bash {} \; > all.sh

Use Cases

  • Documentation testing - Run code examples in CI to prevent stale docs
  • Config extraction - Pull YAML/JSON/TOML from setup guides
  • Literate programming - Treat Markdown as runnable source
  • Content migration - Pull code from blog posts into files
  • Code review - Inspect what code a PR's docs show

How It Works

Single-pass line scanner that parses fenced code blocks (backtick and tilde). Handles nested fences, indented blocks, and optional language identifiers. Fast and predictable.

Comparison

Feature codeblock-cli grep/sed remark-cli
Zero deps Yes N/A No (50+)
Cross-platform Yes Unix only Yes
Filter by lang Yes Fragile Plugin
Pick by index Yes No No
List languages Yes No Plugin
Install size ~5 KB Built-in ~15 MB

Related Tools

License

MIT (c) 2026 kszongic

About

Extract fenced code blocks from Markdown files. Zero dependencies.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors