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

pico-md

v2.0.1

Published

A featherweight markdown parser that's easy to extend

Downloads

2

Readme

#️⃣ pico-marked

A featherweight markdown parser that's easy to extend

Features

  • Easily extendable
  • Option to not sanatize HTML
  • Able to extend the Markdown rule sets
  • Handles metadata using simple trnp syntax
  • Only one way to do things. The Markdown spec has multiple ways of annotating the same thing, pico-md only has one method per annotation.
  • No dependacies
  • Only 175 lines of code
npm i pico-md

const md = require('pico-md');

md(`# This is _Markdown!_

This is parsed using [\`pico-md\`](https://github.com/stolksdorf/pico-md).

`);

Spec

Inline Rules

  • *bold*
  • _italic_
  • ~strikeout~
  • ![image alt text](https://image.url)
  • [link text](https://link.url)
  • \inline code``
  • {{div,with,classes
  • }} (closing div)

Block Rules

Code Block

\`\`\`js
A block of
unformatted *code*
\`\`\`

Block Quote

> A _multiline_
> blockquote!

Horizontal Rule

Above the line
---
Below the line

Headers

## I am a second level header

##### I am a fifth level header

Tables

| Table | Column      |
|-------:|  :---: |
| With  | _alignment_ |

| Table | Column |
| That's| just simple|

Lists

- Nested
  - Unordered
    - Lists
- Ordered
  1. Lists
  1. That disregard
  60. numbers

4. This ordered List
1. starts at 4

Modifying the rulesets

Options

allowHtml: true, false, default: false

By default pico-md will "sanatize" any HTML in the input. It only odes a naive HTML sanatization. If you need something more intense, I suggest using another library to pre-process your text.

meta: undefined, true, or false, default: undefined

Sets how the library will return metadata. If undefined, it will return an object contain both the metadata and the HTML if metadata is present; If metadata is not present, it will just return the HTML. If aset to true if will always return an object, regardless if there's metadata or not. If set to false it will always return just a string, even if there is metadata present.

Metadata

You can store metadata about the markdown file at the top of the file inbetween ---. The text must start with ---. pico-md uses simple trnp syntax for converting the metadata into a JSON object.

---
title: Blogging Like a Regular Guy
tags : - post
       - updates
published: true
---

# Blogging Like a Regular Guy
Hello, welcome to my blog ...

How It Works

pico-md is broken into two parsers: a block-level parser, and a inline-level parser. The inline parser handles rulesets that occur within flowing text; bolds, italics, links, images, etc. The block-level parser is looking for the large chunks of markdown that need to be parsered speacially; Headers, tables, code blocks, blockquotes, paragraphs, etc. Both rulesets follow the same structure, an array of rules where each rule is a 2 two element array of a regular expression and a function.

The inline parser loops over each rule and applies the function to all tokens that match the regex.

The block parser works slightly differently. It loops over each rule's regex and picks the one the matches the earliest within the remaining text, it then runs the rule's function on the match and adds the returned value to the result. If there's any text above the match that wasn't part of it, that text is ran through the fallback rule, which normally handles the Markdown paragraph rules. Many of the block level rules use the Inline parser within them.

Why Not Use An Existing Library?

I've used many many markdown parsers over my career, and unfortunately due to the Markdown Spec not being well-defined, or consistently implemented it has led to a fair amount of fragmentation in this space. One library might allow you to pass through HTML, but not have the strikeout syntax, another might allow metadata.

On top of that I found the code behind the markdown parsers I've used to be quite hard to follow and able to modify or add tweaks to if needed. Most use a pretty bespoke lexer and tokenizer process tailored to the exact spec they are implemented, meaning add a new rule or slightly changing one (sush as _ meaning italics instead of *) is nearly impossible or causes bugs to other places.

pico-md was built in a way that reduces code iteractions between different rules within the markdown spec, so it is much easier to modify and tweak.

Also pico-md is incredibly lightweight, stripping out more obscure parts of the spec, and eliminating the need for dependacies. This library is able to be dropped into any frontend or backend project, making it suitable for isomorphic web development as well.