
very-small-parser is a lightweight (~4kb) dependency-free JavaScript library for parsing Markdown, HTML, and inline CSS in your web project.
It can be helpful in applications that require dynamic content rendering, content analysis, or text manipulation. For instance, you can build a custom Markdown previewer, a blog engine, or a tool that sanitizes user-generated HTML. You can also use it to convert Markdown to HTML, or manipulate CSS styles inline.
Features:
- Markdown Parsing: Converts Markdown to MDAST.
- HTML Parsing: Converts HTML to HAST.
- Inline CSS Parsing: Parse style attributes and CSS declarations.
- MDAST to Text: Pretty-print Markdown AST to text.
- MDAST to HAST: Convert Markdown AST to HTML AST.
- HAST to HTML: Pretty-print HTML AST to HTML.
- Text Detection: Detects if a string is likely Markdown.
- Custom Tabulation: Specify tabulation size for HTML.
Installation:
1. Install ‘very-small-parser’ via NPM:
# NPM $ npm install very-small-parser
2. Import the required modules:
import {
markdown, // Parse Markdown document
is, // Detect if text is likely to be a Markdown document
toText, // Pretty-print MDAST back to text
toHast, // Convert MDAST to HAST (Markdown AST to HTML AST):
html, // Parse HTML to HAST (Hypertext Abstract Syntax Tree)
} from 'very-small-parser';3. You can also import these modules directly from a CDN:
import {
markdown, // Parse Markdown document
is, // Detect if text is likely to be a Markdown document
toText, // Pretty-print MDAST back to text
toHast, // Convert MDAST to HAST (Markdown AST to HTML AST):
html, // Parse HTML to HAST (Hypertext Abstract Syntax Tree)
} from 'https://cdn.jsdelivr.net/npm/very-small-parser/+esm';Parsing Markdown:
1. Parse a Markdown document with block elements:
const ast = markdown.block.parse('CSS __Script__');2. Parse only inline Markdown markup:
const ast = markdown.inline.parse('CSS __Script__');3. Check if a text is likely to be a Markdown document:
// true
is('CSS __Script__');
// false
is('<b>CSSScript</b>');4. Convert MDAST back to text:
const mdast = markdown.block.parse('CSS __Script__');
// CSS __Script__
const text = toText(mdast);5. Transform MDAST to HAST:
const mdast = markdown.block.parse('CSS __Script__!');
const hast = toHast(mdast);
// <p>CSS <strong>Script</strong>!</p>
const html = toText(hast);Parsing HTML:
1. Convert HTML to HAST:
const ast = html.parse('<b>CSS</b> <i>Script</i>');2. Convert HAST back to HTML:
const hast = html.parse('<b>CSS</b> <i>Script</i>');
// '<b>CSS</b> <i>Script</i>!'
const html = toText(hast);3. Set the tabulation size for indentation when pretty-printing:
const tab = ' ';
const hast = html.parse('<div><b>CSS</b><i>Script</i></div>', tab);
const html = toText(hast);
// <div>
// <b>CSS</b>
// <i>Script</i>
// </div>Changelog:
v1.14.0 (07/27/2025)
- add Markdown frontmatter parsing support
v1.13.0 (06/20/2025)
- correct all linter and test errors
- unescape HTML attributes
- use single quotes for attributes
v1.12.0 (03/09/2025)
- treat basic newlines as spaces
- correctly print to Markdown inline line breaks







