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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mdast-flat

v1.6.0

Published

- Flat version of [MDAST format](https://github.com/syntax-tree/mdast). - Nodes are stored in a flat `nodes` array instead of a nested tree. - Table of contents is available in `contents` key. - All definitions are available in `definitions` map. - All fo

Downloads

97

Readme

mdast-flat

  • Flat version of MDAST format.
  • Nodes are stored in a flat nodes array instead of a nested tree.
  • Table of contents is available in contents key.
  • All definitions are available in definitions map.
  • All footnotes are available in footnotes map.

Main difference from MDAST is that Parent node children property is an array of numbers (instead of array of nodes). Numbers are indices into nodes array, which contains all nodes.

interface FlatParent <: Parent {
  idx: number
  children: [number]
}

idx is index of the current node, children contains array of children indices.

Full document schema:

interface MdastFlat {
  nodes: [Root | FlatParent | Literal]
  contents: [number]
  definitions: {[identifier]: number}
  footnotes: {[identifier]: number}
  footnoteOrder: [number]
}
  • node — a flat array of document nodes.
  • contents — an array nodes indices, which are heading nodes.
  • definitions — a map of definition identifiers into nodes indices.
  • footnotes — a map of footnote identifiers into nodes indices.
  • footnoteOrder — ordered list of footnote node indices.

Nodes

MDAST-Flat nodes have the following attributes.

  • All the same attributes as MDAST tokens, except see below.
  • children attribute is an array of numbers that index into nodes list.
  • idx a number, which is the index of current node in nodes list.
  • parent a number, which is the index of parent node in the nodes list.

Usage

import {mdastToFlat, flatToMdast} from 'mdast-flat';

const flat = mdastToFlat(mdast1);
const mdast2 = flatToMdast(flat);

Reference

  • mdastToFlat(mdast) — converts MDAST to MDAST-Flat.
  • flatToMdast(flat) — converts MDAST-Flat to MDAST.
  • replace(flat1, idx, flat2) — replaces node idx in flat1 by flat2.

Example

Let's say you have the following Markdown.

[Click me][click]

[click]: https://github.com/

You could convert it to MDAST using md-mdast.

type: root
children:
- type: paragraph
  children:
  - type: linkReference
    children:
    - type: text
      value: Click me
    identifier: click
    referenceType: full
- type: definition
  identifier: click
  title:
  url: https://github.com/

Using mdastToFlat() function you can covert it to MDAST-Flat.

nodes:
- type: root
  children:
  - 1
- type: paragraph
  children:
  - 2
- type: linkReference
  children:
  - 3
  identifier: click
  referenceType: full
- type: text
  value: Click me
- type: definition
  identifier: click
  title:
  url: https://github.com/
contents: []
definitions:
  click: 4
footnotes: {}
footnoteOrder: []

Replacing node with document

You can use replace() function to insert a Markdown document inside another Markdown document instead of some specified node.

Consider you have a Markdown document.

1

replace me

And another document.

2

Let's say you have parsed both documents into flat1 and flat2 MDAST-Flat objects, respectively. Now you want to insert the second document inside the first document in place of replace me paragraph (which has idx of 3 in flat1);

const merged = replace(flat1, 3, flat2);

The result is MDAST-Flat merged object, which merges all nodes using new portal node. It also merges contents, definitions and footnotes, if there are any.

nodes:
- type: root
  children:
  - 1
  - 3
  idx: 0
- type: paragraph
  children:
  - 2
  idx: 1
- type: text
  value: '1'
  idx: 2
- type: portal
  idx: 3
  original:
    type: paragraph
    children:
    - 4
    idx: 3
  children:
  - 5
- type: text
  value: replace me
  idx: 4
- type: root
  children:
  - 6
  idx: 5
- type: paragraph
  children:
  - 7
  idx: 6
- type: text
  value: '2'
  idx: 7
contents: []
definitions: {}
footnotes: {}
footnoteOrder: []

Resulting Markdown equivalent is:

1

2

License

Unlicense — public domain.