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

region-ast

v1.1.1

Published

A generic text region AST (Abstract Syntax Tree) following the unist specification

Readme

region-ast

author herve-perchec release pipeline-status coverage package downloads issues license

Table of contents

🍵 Introduction

region-ast is a specification for representing text regions (similar to VSCode regions) as an abstract syntax tree (AST). It implements the unist specification.

This library provides the type definitions, structure rules, unified plugins and utils for processing region-based text.

This specification defines a format for text nested within region markers. It is designed to handle line-by-line processing where lexical analysis identifies region boundaries.

It is widely useful for:

  • Parsing folding ranges in editors.
  • Extracting specific snippets from code files.
  • Transforming region-annotated text.

Where should I use this?

This package defines the AST structure. It does not implement the parsing logic for specific editors (like VSCode).

region-ast is not intended to be self-sufficient. It expects region markers to match to be able to parse regions in plain text. For example, see the vscode-region-parser package that implements region-ast to parse VSCode regions.

Otherwise, feel free to implement this specification to fit your own needs! 👍

🚀 Get started

This package is published on NPM.

npm install region-ast

🧬 Structure rules

The AST is a tree where each node represents a part of the document.

  1. Nesting: Regions can be deeply nested inside other regions.
  2. Completeness: A Region node is only formed if both a Start and an End marker are matched.
  3. Orphans: If a region start or end marker is "alone" (no corresponding pair found), it is treated as plain Text.
  4. Processing: The parsing logic is line-by-line.

🔹 Nodes

This specification defines the following nodes:

  • Root

    The Root node represents the entire document. It is the unique top-level node.

  • Region

    A Region represents a matched block containing a start marker, optional content, and an end marker.

    • Children: Must contain exactly one RegionStart (first child) and one RegionEnd (last child). It may contain any number of Region or Text nodes in between.
  • RegionStart

    Represents the specific line or token that initiates a region.

  • RegionEnd

    Represents the specific line or token that closes a region.

  • Text

    Represents any literal text that is not part of a successfully matched region structure. This includes:

    • Standard code or content.
    • Orphaned region markers (unpaired start/end tags).

💡 Please check the Types references and the hierarchy in the API documentation.

Example

Given the following source text (using VSCode-style markers as an example):

// #region Setup
const x = 10
// #endregion
console.log(x)

The resulting region-ast tree would look like this:

{
  type: 'root',
  children: [
    {
      type: 'region',
      name: 'Setup',
      children: [
        { type: 'regionStart', value: '// #region Setup\n' },
        { type: 'text', value: 'const x = 10\n' },
        { type: 'regionEnd', value: '// #endregion\n' }
      ]
    },
    {
      type: 'text',
      value: 'console.log(x)'
    }
  ]
}

🔗 API

This package expose some unified plugins and utils.

Unified

import { unified } from 'unified'
import region, { parse, stringify } from 'region-ast'

unified().use(region, { markers: [ /* ... */ ] })
// Parser only
unified().use(parse, { markers: [ /* ... */ ] })
// Compiler only
unified().use(stringify)

💡 Please check the plugins references documentation.

Utils

To create tree from string, use the fromString util:

import { fromString } from 'region-ast/utils'

const tree = fromString(
`// #region Setup
const x = 10
// #endregion
console.log(x)`
)

// => Tree: { type: 'root', children: [ ... ] }

Vice versa, to create string from tree, use the toString util:

import { toString } from 'region-ast/utils'

toString(tree)

A Region class is exposed and is intended to be extended. Overwrite the markers static property:

import { Region } from 'region-ast/utils'

class CustomRegion extends Region {
  public static markers: Marker[] = [
    // Define your markers
  ]
}

💡 Please check the utils references documentation.

🤝 Contribute

You would like to contribute to this project? You are welcome!

First, please check:

👑 Author

Made with ❤ by Hervé Perchec

⚖️ License

MIT

📰 Changelog

See all changes to this project in the CHANGELOG.md file.

🧱 Related packages


README.md - this file was auto generated with juisy README templater. Don't edit it.