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

mtj-parser

v0.2.1

Published

Markdown layer that builds on top of a parser for extra control

Downloads

9

Readme

MTJ-Parser

The Markdown-to-JSON Parser


Parsers already exist aplenty for transforming Markdown into HTML. However, I don't want to use the HTML output of a parser I don't know and inject it unsafely into a ReactJS component. The problem is that the best parser I could find that doesn't convert my input directly into HTML is markdown-it, but the output is not as easy to convert into nested components as I would like. It's more akin to an array of token-like objects, with things like "open paragraph" and a matching "close paragraph", with all the paragraph tokens between them. The design is too linear for me, it would be better to have a tree. So, rather than reinvent the wheel, I've wrapped markdown-it with my own pseudo-parser, converting the serial structure into a nested structure that is better better for building components around. It's more like unflattening (?) than text parsing, and since parsing text is a nightmare, I'll just piggyback off of puzrin's hard work.

The output is something that I can more easily manipulate, which makes me happier. Also, I'm learning, and at the end of the day, isn't that all that matters?

The rubric file shows all the currently supported Markdown formatting.

ToDo

[ ] - 100% test coverage

Install

npm install mtj-parser

How-to

Simple use the parseMarkdown function, which should return a happy, nested markdown document:

JavaScript:

import { parseMarkdown } from 'mtj-parser';

const mdString = `
# This is **Markdown**
`;

const mdObject = parseMarkdown(mdString);
// [
//   {
//     type: "heading",
//     parts: [
//       {
//         type: "text",
//         value: "This is "
//       },
//       {
//         type: "strong",
//         parts: [
//           {
//             type: "text",
//             value: "Markdown"
//           }
//         ]
//       },
//       {
//         type: "text",
//         value: ""
//       }
//     ],
//     size: 1
//   }
// ]

Node types

Being written in TypeScript means that there are types available for the different node types, and can be imported from the index of the library along with the parseMarkdown function.

The nodes are split into two categories: BaseNodes and SubNodes. The root of the object is a MarkdownDoc, which is just an array of BaseNodes. BaseNodes are the top-level Markdown elements (paragraph, lists, etc). Most BaseNodes contain SubNodes, which are the text styling and control elements (bold, images, breaks, etc).

MarkdownDoc

Returned from parseMarkdown(str)

type MarkdownDoc = BaseNode[];

BaseNode

Union type for all types of BaseNodes

type BaseNode
  = Paragraph
  | Heading
  | HorizontalRow
  | Fence
  | OrderedList
  | BulletList
  | Blockquote
  | Table;

SubNode

Union type for all types of SubNodes

type SubNode
  = Text
  | Link
  | Emphasis
  | Strong
  | Strikethrough
  | CodeInline
  | Image
  | HardBreak
  | SoftBreak;

BaseTypes and SubTypes

These two exports are enums whose values are assigned to the type members of the following nodes in order to quickly identify their type. The values of the enum members are strings, which is helpful for debugging.

Paragraph

interface Paragraph {
  type: BaseTypes.paragraph;
  parts: SubNode[];
}

Heading

interface Heading {
  type: BaseTypes.heading;
  parts: SubNode[];
  size: 1 | 2 | 3 | 4 | 5 | 6;
}

HorizontalRow

interface HorizontalRow {
  type: BaseTypes.horizontalRow;
}

Fence

interface Fence {
  type: BaseTypes.fence;
  value: string;
  lang: string;
}

ListItem

Not a node, but used by both Ordered and Bullet lists to contain the elements, which can be a Paragraph or either list type, which is how nested lists work.

type ListItem = Array<Paragraph | BulletList | OrderedList>;

OrderedList

interface OrderedList {
  type: BaseTypes.orderedList;
  list: ListItem[];
}

BulletList

interface BulletList {
  type: BaseTypes.bulletList;
  list: ListItem[];
}

Blockquote

interface Blockquote {
  type: BaseTypes.blockquote;
  parts: SubNode[];
}

Table

Tables make use of two non-node types, Cell and Row:

interface Cell {
  parts: SubNode[];
  align: alignment;
}

interface Row {
  columns: Cell[];
}

interface Table {
  type: BaseTypes.table;
  head: Row;
  body: Row[];
}

Text

interface Text {
  type: SubTypes.text;
  value: string;
}

Link

interface Link {
  type: SubTypes.link;
  parts: SubNode[];
  dest: string;   // url, file reference, or anchor reference
  title?: string; // Hover text
}

Emphasis

interface Emphasis {
  type: SubTypes.emphasis;
  parts: SubNode[];
}

Strong

interface Strong {
  type: SubTypes.strong;
  parts: SubNode[];
}

Strikethrough

interface Strikethrough {
  type: SubTypes.strikethrough;
  parts: SubNode[];
}

CodeInline

interface CodeInline {
  type: SubTypes.codeInline;
  value: string;
}

Image

interface Image {
  type: SubTypes.image;
  src: string;
  title?: string;
  alt?: string;
}

HardBreak

A hard break is made by following a line with 2 or more spaces and a newline

interface HardBreak {
  type: SubTypes.hardbreak;
}

SoftBreak

A soft break is made by following a line with 0 or 1 space and a newline

interface SoftBreak {
  type: SubTypes.softbreak;
}