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

markdown-slice

v0.1.0

Published

Print a specific subset of a markdown document by using query selectors

Readme

markdown-slice

prints a subset of a markdown document

markdown-slice is a command-line tool and node API for selecting and printing a subset of a markdown document.

In order to select which part(s) of the document to print, the document is first parsed into an mdast-format AST. Then, ESQuery's CSS-selector-like language is used to target specific nodes of the AST. By specifying a start node, an end node, and which edge of each node to target (start or end), you can describe a subset of a markdown document. markdown-slice then prints the portion of the document which corresponds to your selection.

Usage (CLI)

markdown-slice - prints a subset of a markdown document

usage: markdown-slice <filename> [options]

options:
  --input, -i: Path to the input file to read. If unspecified, reads from stdin.
  --output, -o: Path to the output file to write to. If unspecified, writes to stdout.

  --from-start-of, --from-end-of: The (CSS-like) query selector string to determine where to start the slice
  --to-start-of, --to-end-of: The (CSS-like) query selector string to determine where to end the slice

  --from-start: Start the slice at the start of the document instead of via a query selector string
  --to-end: End the slice at the end of the document instead of via a query selector string

  --print: Print the AST of the file instead of slicing
  --help, -h: Show this help text

examples:
  # Print the first code block
  markdown-slice -i README.md --from-start-of 'code' --to-end-of 'code'

  # Print the first code block with lang "ts"
  markdown-slice -i README.md --from-start-of 'code[lang="ts"]' --to-end-of 'code[lang="ts"]'

  # Print the second code block with lang "ts"
  markdown-slice -i README.md --from-start-of 'code[lang="ts"] ~ code[lang="ts"]' --to-end-of 'code[lang="ts"] ~ code[lang="ts"]'

  # Print everything from the start of the document up until the table of contents
  markdown-slice -i README.md --from-start --to-start-of 'heading:has(text[value="Table of Contents"])'

  # Print everything after the "License" heading, including the heading itself
  markdown-slice -i README.md --from-start-of 'heading:has(text[value="License"])' --to-end

  # Print from the first h2 to right before the next heading
  markdown-slice -i README.md --from-start-of 'heading[depth=2]' --to-start-of 'heading[depth=2] ~ heading'

  # Print everything from after the "Description" heading until the next heading
  markdown-slice -i README.md --from-end-of 'heading:has(text[value="Description"])' --to-start-of 'heading:has(text[value="Description"]) ~ heading'

references:
  - The AST is in 'mdast' format: https://github.com/syntax-tree/mdast
  - The query selector stuff is done via 'ESQuery': https://github.com/estools/esquery

Example (CLI)

npx markdown-slice --input README.md --from-start-of 'heading:has(text[value="License"])' --to-end-of 'code[lang="sh"]'

Example (API)

All CLI flags starting with --from or --to have equivalent options that can be passed into the 'markdownSlice' function:

| CLI Flag | API Option | API Type | | --------------- | ----------- | -------- | | --from-start-of | fromStartOf | string | | --from-end-of | fromEndOf | string | | --to-start-of | toStartOf | string | | --to-end-of | toEndOf | string | | --from-start | fromStart | boolean | | --to-end | toEnd | boolean |

import { markdownSlice } from "markdown-slice";

const someMarkdown = `
# Hi

This is a paragraph

\`\`\`
this is a code block
\`\`\`

- this is a list
- with some items
`;

const aBitLessMarkdown = markdownSlice(someMarkdown, {
  fromStartOf: "code",
  toEnd: true,
});

console.log(aBitLessMarkdown);
// Prints:
//
// ```
// this is a code block
// ```
//
// - this is a list
// - with some items

License

MIT