https://github.com/streamich/very-small-parser
Small, no dependencies, Markdown, HTML, and inline CSS parser. Just 4KB, available as ESM module from CDN.
https://github.com/streamich/very-small-parser
Last synced: 11 months ago
JSON representation
Small, no dependencies, Markdown, HTML, and inline CSS parser. Just 4KB, available as ESM module from CDN.
- Host: GitHub
- URL: https://github.com/streamich/very-small-parser
- Owner: streamich
- License: apache-2.0
- Created: 2024-11-24T11:22:19.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-08-07T18:33:20.000Z (11 months ago)
- Last Synced: 2025-08-07T20:37:40.098Z (11 months ago)
- Language: TypeScript
- Homepage:
- Size: 6.35 MB
- Stars: 22
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# `very-small-parser`
- JavaScript parser for Markdown, HTML, and inline CSS.
- [Tiny](https://cdn.jsdelivr.net/npm/very-small-parser/dist/module.js), just over 4KB.
- Runs in browser and Node.js.
- No dependencies.
- Markdown is parsed into [MDAST (Markdown Abstract Syntax Tree)](https://github.com/syntax-tree/mdast).
- HTML is parsed into [HAST (Hypertext Abstract Syntax Tree)](https://github.com/syntax-tree/hast).
## Usage
[__Live demo__](https://jsfiddle.net/yd5eL1cb/)
On the web you can simply import the module using a script tag.
Using ESM.sh:
```html
import { markdown } from '//esm.sh/very-small-parser';
const ast = markdown.block.parsef('Hello __world__!');
console.log(ast);
```
Using jsDelivr:
```html
import { markdown } from '//esm.run/very-small-parser';
const ast = markdown.block.parsef('Hello __world__!');
console.log(ast);
```
To use TypeScript types or import into a Node.js project, you can install the package from npm:
```sh
npm install very-small-parser
```
## Reference
### Markdown
Parse Markdown document (block elements):
```js
import { markdown } from 'very-small-parser';
const ast = markdown.block.parsef('Hello __world__!');
```
Parse Markdown inline markup only:
```js
const ast = markdown.inline.parse('Hello __world__!');
```
Detect if text is likely to be a Markdown document:
```js
import { is } from 'very-small-parser/lib/markdown/is';
is('Hello __world__!'); // true
is('Hello!'); // false
```
Pretty-print MDAST back to text:
```js
import { markdown } from 'very-small-parser';
import { toText } from 'very-small-parser/lib/markdown/block/toText';
const mdast = markdown.block.parse('Hello __world__!');
const text = toText(mdast); // Hello __world__!
```
Convert MDAST to HAST (Markdown AST to HTML AST):
```js
import { markdown } from 'very-small-parser';
import { toHast } from 'very-small-parser/lib/markdown/block/toHast';
import { toText } from 'very-small-parser/lib/html/toText';
const mdast = markdown.block.parse('Hello __world__!');
const hast = toHast(mdast);
const html = toText(hast); //
Hello world!
```
### HTML
Parse HTML to HAST (Hypertext Abstract Syntax Tree):
```js
import { html } from 'very-small-parser';
const ast = html.parse('Hello world!');
```
Pretty-print HAST to HTML:
```js
import { html } from 'very-small-parser';
import { toText } from 'very-small-parser/lib/html/toText';
const hast = html.parse('Hello world!');
const html = toText(hast); // 'Hello world!'
```
Specify tabulation size for indentation when pretty-printing:
```js
import { html } from 'very-small-parser';
import { toText } from 'very-small-parser/lib/html/toText';
const tab = ' ';
const hast = html.parse('
Helloworld!', tab);
const html = toText(hast);
//
// Hello
// world
// !
//
```
Convert HAST to MDAST (HTML AST to Markdown AST):
```js
import { html } from 'very-small-parser';
import { toMdast } from 'very-small-parser/lib/html/toMdast';
import { toText } from 'very-small-parser/lib/markdown/block/toText';
const hast = html.parse('
Hello world!
');
const mdast = toMdast(hast);
const text = toText(mdast); // __Hello__ _world_!
```
### JSON-ML
JSON-ML is a simple way to represent HTML as JSON. For example, the HTML
`Hello` is represented as `['b', null, 'Hello']`. The first element is
the tag name, the second is the attributes, and the rest are children.
This package contains converters for JSON-ML to HAST and back. See the [`/src/html/json-ml`](./src/html/json-ml/) directory.