npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kym6464/md-extract

v1.1.5

Published

Extract sections of a markdown file with a regular expression

Downloads

1,457

Readme

@kym6464/md-extract

Extract sections of a markdown file with a regular expression.

Installation

# Install globally
npm install -g @kym6464/md-extract

# Or use with npx (no installation required)
npx @kym6464/md-extract "pattern" document.md

Usage

Given a document called my-document.md:

# Welcome

This is my amazing markdown document.

## Extract me!

This section should be pulled out.

## Another section

This won't match the pattern.

You can extract the second section with the following command:

$ md-extract "Extract me!" my-document.md
## Extract me!

This section should be pulled out.

Options

Usage: md-extract [options] <pattern> <file>

Extract sections of a markdown file with a regular expression

Arguments:
  pattern                         Pattern to match against headings
  file                            Path to markdown file

Options:
  -V, --version                   output the version number
  -a, --all                       Print all matching sections (don't quit after first match)
  -s, --case-sensitive            Treat pattern as case sensitive
  -n, --no-print-matched-heading  Do not include the matched heading in the output
  --no-children                   Exclude content under child headings
  -h, --help                      display help for command

Examples

Extract all sections matching a pattern:

md-extract --all "^API" documentation.md

Case-sensitive matching:

md-extract --case-sensitive "TODO" project.md

Extract content without the heading:

md-extract --no-print-matched-heading "Summary" report.md

Extract only direct content, excluding child sections:

md-extract --no-children "Welcome" my-document.md

md-headings

List all headings in a markdown document, displayed as an indented tree.

$ md-headings my-document.md
Welcome
  Extract me!
  Another section

It also reads from stdin, so you can pipe content into it:

curl -sL https://example.com/doc.md | md-headings

Use --format json to get a nested tree with character counts per section (useful for AI agents deciding how to read content):

$ md-headings --format json my-document.md
[
  {
    "heading": "Welcome",
    "chars": 149,
    "ownChars": 49,
    "children": [
      { "heading": "Extract me!", "chars": 51 },
      { "heading": "Another section", "chars": 49 }
    ]
  }
]

Parent nodes include ownChars — the character count excluding children. Use this to predict how much content md-extract --no-children will return.

Or --format toon for a more compact representation:

$ md-headings --format toon my-document.md
[1]:
  - heading: Welcome
    chars: 149
    ownChars: 49
    children[2]{heading,chars}:
      Extract me!,51
      Another section,49

Options

Usage: md-headings [options] [file]

List headings in a markdown document

Arguments:
  file                  Path to markdown file (reads from stdin if omitted)

Options:
  -V, --version         output the version number
  -f, --format <type>   Output format: plain, json, toon (default: "plain")
  -h, --help            display help for command

Library Usage

You can also use this as a JavaScript library:

import { extractFromPath } from "@kym6464/md-extract";

const regex = /Extract me!/i;
const matches = await extractFromPath("my-document.md", regex);
console.log(matches); // Array of matching sections

Credits

This code was ported to JavaScript from sean0x42/markdown-extract.