Markdown Strip CLI
A Node CLI that reads a Markdown file and prints the plain text with all formatting syntax removed, preserving paragraph breaks.
Project
node-markdown-strip-cli
Entry point: mdstrip.mjs
Install
node --versionRun
node mdstrip.mjs readme.mdTest
node --test mdstrip.test.mjsExpected output
Prints the plain text content of the Markdown file with headings, emphasis, links, code spans, and list markers stripped, paragraphs preserved.Dependencies
- node >=20
What it does
Reads a Markdown file and applies a sequence of regex replacements to strip formatting: code blocks and spans are removed or unwrapped, headings lose their #, emphasis loses its *, links keep their text, list markers and blockquotes are removed, and excessive blank lines are collapsed.
import { readFileSync } from 'node:fs';
const md = readFileSync(process.argv[2], 'utf8');
const out = md
.replace(/```[\s\S]*?```/g, '') // code blocks
.replace(/\`([^`]*)\`/g, '$1') // inline code
.replace(/^#{1,6}\s+/gm, '') // headings
.replace(/\*\*([^*]*)\*\*/g, '$1') // bold
.replace(/\*([^*]*)\*/g, '$1') // italic
.replace(/\[([^\]]*)\]\([^\)]*\)/g, '$1') // links
.replace(/^[-*+]\s+/gm, '') // list markers
.replace(/^>\s+/gm, '') // blockquotes
.replace(/^\|.*$/gm, '') // tables
.replace(/\n{3,}/g, '\n\n'); // collapse blank lines
console.log(out.trim());
Run and verify
node mdstrip.mjs readme.md
Run the test command to check the implementation: node --test mdstrip.test.mjs.
Expected Output
For a Markdown file with a heading and a bold paragraph, the output is the plain text:
Heading text
Bold paragraph text
Limitations
The regex approach handles common Markdown but not nested formatting, HTML blocks, or reference-style links. A proper parser would be more robust but is heavier for a CLI.