
Company News
AWS Security Hub Adds Socket for Supply Chain Security
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.
@mdedit/markdown-parser
Advanced tools
AST-based markdown parsing utilities for the mdedit project.
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 ASTmdast-util-to-markdown - Serialize AST back to markdownmdast-util-gfm - GitHub Flavored Markdown supportmdast-util-frontmatter - YAML and TOML frontmatter supportmicromark-extension-frontmatter - Frontmatter tokenizationunist-util-visit - Traverse and manipulate AST nodesThe AST-based approach provides several advantages over manual parsing:
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): stringReturns 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): stringTransforms all heading text using the provided transformer function.
const result = transformHeadings(markdown, (text, depth) => {
return titleCase(text);
});
generateTableOfContents(markdown: string, options?: { maxDepth?: number }): stringGenerates a table of contents from the markdown headings.
const toc = generateTableOfContents(markdown, { maxDepth: 3 });
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";
}
}
import { extractCodeBlocks } from '@mdedit/markdown-parser';
const blocks = extractCodeBlocks(article);
blocks.forEach(block => {
const codeBlock = block.value;
// Process code block
});
The following functions in apps/app/src/utils/articleUtils.ts have been refactored to use AST-based parsing:
cleanupIpynbOutput - Uses transformCodeBlocksdetectCodeSnippetLanguages - Uses transformCodeBlockscodePrettify - Uses transformCodeBlockscodeScreenShots - Uses transformCodeBlocksgistify - Uses transformCodeBlocksgetTextWithoutCodeBlocks - Uses getTextWithoutCodeBlocksconvertHeadingsToTitleCase - Uses transformHeadingsgenerateToC - Uses generateTableOfContentsRun the manual tests:
cd packages/markdown-parser
ts-node src/test-manual.ts
FAQs
AST-based markdown parsing utilities for the mdedit project.
We found that @mdedit/markdown-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is now in the AWS Security Hub Extended plan. Adopt it through AWS, apply committed spend, and block malicious open source packages.

Research
/Security News
Popular npm packages keyv and cacheable compromised.

Security News
A misconfiguration gave three Anthropic models internet access, and one, believing it was in a simulation, shipped a credential-stealing package to PyPI.