DEV Community

loading...

Markdown like syntax parser

Rafi
I'm a developer who works mostly with Javascript.
・1 min read

Sometimes you want markdown like syntax but not exact markdown specification. I build a really tiny markdown like syntax parser that you can use out of box or use the functions it exposes to write a custom markdown parser

you can install the package using npm or yarn

$ npm i pico-markdown-parser
Enter fullscreen mode Exit fullscreen mode

Simple usage

import pico from "pico-markdown-parser";

const htmlOutput = pico(`
  # Hello

  This is a sample text!!
`);
Enter fullscreen mode Exit fullscreen mode

Building your own parser

This parser is composed of many small parsers you can import those small parsers from the library and put them together in ways you see fit (like Lego pieces).

import {
  choice, 
  many,
  heading,
  bold,
  anything 
 } from "pico-markdown-parser";

// This new parser now supports only bold and heading
const parser = (input) =>
  many(choice([heading, bold, anything]))
    .run(input)
    .result.join("");

const htmlOutput = parser(`
  # Hello

  This is a **sample** text!!
`);

Enter fullscreen mode Exit fullscreen mode

If the small parsers it gives does not suit your need you can even write your own parser and combine it. This parser is based on arcsecond

Here is the repo. PR's are welcomed !!!

Discussion (0)