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

@textlint/markdown-to-ast

v16.1.3

Published

Parse Markdown to AST with location info.

Readme

@textlint/markdown-to-ast

Parse Markdown to AST with location info.

This library is a CommonMark + GFM parser that produces TxtNodes directly. It has no third-party dependencies.

Markdown -> markdown-to-ast -> TxtNodes

Supported syntax:

  • CommonMark 0.31.2
  • GFM: tables, strikethrough, task list items, and autolink literals
  • Footnotes([^1] and [^1]: note) and inline notes(^[note])
  • Front matter: YAML(---), TOML(+++), JSON(;;; or {)

The parser follows CommonMark for everything CommonMark defines, and GFM for the extensions GFM defines. The node of each syntax, its properties, and its range are defined in the Markdown parsing requirements.

Front matter and footnotes are not part of CommonMark/GFM, so their nodes are extension nodes with lowercase type names: yaml, toml, json, footnoteReference, footnoteDefinition, and footnote.

The AST consists of TxtNodes. A TxtNode of the AST has following properties:

  • loc - Nodes have line and column-based location info.
  • range - Nodes have an index-based location range (array).
  • raw - Node have a raw text.
  • value - Node have a value of text.

raw, range, and loc of a node always describe the same span of the text.

The interface are defined in textlint/txtnode.md

This library is a part of textlint/textlint.

If you need a general-purpose Markdown parser (mdast, HTML output, plugins), please use remark directly.

Design

The parser is written with data-oriented design for performance. See the Markdown parsing design for details.

  • Nodes are stored in flat typed arrays (struct of arrays) and refer to each other by index while parsing
  • Strings are not copied while parsing. raw and value are sliced from the source text when the AST is emitted
  • TxtNode objects are created only once at the end. There is no intermediate AST such as mdast
  • Working buffers are reused between parse() calls
  • Parsing time is proportional to the size of the text, including for inputs that repeat unclosed syntax

Nesting limit

The parser does not overflow the call stack for deeply nested inputs.

  • The nesting depth is limited to 100 for block containers (BlockQuote, List, ListItem, footnoteDefinition) and 100 for inline nodes (Emphasis, Strong, Delete, Link, LinkReference, footnote)
  • The markers beyond the limit are not parsed as syntax. They are kept as text
  • The tree is built and emitted with loops and explicit stacks instead of recursion

DEMO

Installation

npm install @textlint/markdown-to-ast

Usage

import { parse } from "@textlint/markdown-to-ast";

const markdown = "It's a *text*";
const AST = parse(markdown);
/*
{
    "type": "Document",
    "children": [
        {
            "type": "Paragraph",
            "children": [
                {
                    "type": "Str",
                    "value": "It's a ",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 0
                        },
                        "end": {
                            "line": 1,
                            "column": 7
                        }
                    },
                    "range": [
                        0,
                        7
                    ],
                    "raw": "It's a "
                },
                {
                    "type": "Emphasis",
                    "children": [
                        {
                            "type": "Str",
                            "value": "text",
                            "loc": {
                                "start": {
                                    "line": 1,
                                    "column": 8
                                },
                                "end": {
                                    "line": 1,
                                    "column": 12
                                }
                            },
                            "range": [
                                8,
                                12
                            ],
                            "raw": "text"
                        }
                    ],
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 7
                        },
                        "end": {
                            "line": 1,
                            "column": 13
                        }
                    },
                    "range": [
                        7,
                        13
                    ],
                    "raw": "*text*"
                }
            ],
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 13
                }
            },
            "range": [
                0,
                13
            ],
            "raw": "It's a *text*"
        }
    ],
    "loc": {
        "start": {
            "line": 1,
            "column": 0
        },
        "end": {
            "line": 1,
            "column": 13
        }
    },
    "range": [
        0,
        13
    ],
    "raw": "It's a *text*"
}
*/

If you want to know real use-case, please see textlint/textlint.

Options

parse(text, options) takes these options. All of them are false by default.

  • cjkFriendlyEmphasis: Recognize emphasis, strong emphasis, and strikethrough next to CJK punctuation, as the CommonMark CJK-friendly Amendments define. **強調。**この文 becomes Strong and Str.
const AST = parse("**強調。**この文", { cjkFriendlyEmphasis: true });

Tests

npm test

Create fixtures

See tools/ directory.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT