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

@mdedit/markdown-parser

v0.2.0

Published

AST-based markdown parsing utilities for the mdedit project.

Readme

@mdedit/markdown-parser

AST-based markdown parsing utilities for the mdedit project.

Overview

This package provides utilities for parsing and transforming markdown content using Abstract Syntax Trees (AST) instead of manual line-by-line parsing. It leverages the mdast (Markdown Abstract Syntax Tree) ecosystem including:

  • mdast-util-from-markdown - Parse markdown to AST
  • mdast-util-to-markdown - Serialize AST back to markdown
  • mdast-util-gfm - GitHub Flavored Markdown support
  • mdast-util-frontmatter - YAML and TOML frontmatter support
  • micromark-extension-frontmatter - Frontmatter tokenization
  • unist-util-visit - Traverse and manipulate AST nodes

Benefits

The AST-based approach provides several advantages over manual parsing:

  1. More Reliable: Handles edge cases and complex markdown structures correctly
  2. Maintainable: Clear, declarative code that's easier to understand and modify
  3. Extensible: Easy to add new transformations or extractions
  4. Standards-Compliant: Uses the unified/remark ecosystem standards
  5. Type-Safe: Written in TypeScript with full type definitions
  6. Frontmatter-Aware: Preserves YAML and TOML frontmatter during transformations

API

extractCodeBlocks(markdown: string): CodeBlock[]

Extracts all code blocks from markdown content.

const blocks = extractCodeBlocks(markdown);
blocks.forEach(block => {
  console.log(`Language: ${block.lang}, Code: ${block.value}`);
});

extractHeadings(markdown: string): Heading[]

Extracts all headings from markdown content.

const headings = extractHeadings(markdown);
headings.forEach(heading => {
  console.log(`Level ${heading.depth}: ${heading.text}`);
});

getTextWithoutCodeBlocks(markdown: string): string

Returns the markdown content with all code blocks removed.

const text = getTextWithoutCodeBlocks(markdown);

transformCodeBlocks(markdown: string, transformer: (code: string, lang: string) => Promise<string>): Promise<string>

Transforms all code blocks in the markdown using the provided transformer function.

const result = await transformCodeBlocks(markdown, async (code, lang) => {
  // Transform code to screenshot
  const screenshot = await codeToScreenshot(code, lang);
  return screenshot;
});

transformHeadings(markdown: string, transformer: (text: string, depth: number) => string): string

Transforms all heading text using the provided transformer function.

const result = transformHeadings(markdown, (text, depth) => {
  return titleCase(text);
});

generateTableOfContents(markdown: string, options?: { maxDepth?: number }): string

Generates a table of contents from the markdown headings.

const toc = generateTableOfContents(markdown, { maxDepth: 3 });

Migration Guide

Before (Manual Parsing)

const lines = article.split("\n");
let codeBlockStarted = false;
let codeBlock = "";

for (let i = 0; i < lines.length; i++) {
  const line = lines[i];
  if (line.startsWith("```")) {
    codeBlockStarted = !codeBlockStarted;
  } else if (codeBlockStarted) {
    codeBlock += line + "\n";
  }
}

After (AST-Based)

import { extractCodeBlocks } from '@mdedit/markdown-parser';

const blocks = extractCodeBlocks(article);
blocks.forEach(block => {
  const codeBlock = block.value;
  // Process code block
});

Refactored Functions

The following functions in apps/app/src/utils/articleUtils.ts have been refactored to use AST-based parsing:

  • cleanupIpynbOutput - Uses transformCodeBlocks
  • detectCodeSnippetLanguages - Uses transformCodeBlocks
  • codePrettify - Uses transformCodeBlocks
  • codeScreenShots - Uses transformCodeBlocks
  • gistify - Uses transformCodeBlocks
  • getTextWithoutCodeBlocks - Uses getTextWithoutCodeBlocks
  • convertHeadingsToTitleCase - Uses transformHeadings
  • generateToC - Uses generateTableOfContents

Testing

Run the manual tests:

cd packages/markdown-parser
ts-node src/test-manual.ts