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

markty

v0.0.4

Published

Tiny Markup Creator.

Downloads

3,170

Readme

Write your own markup parser as micro building blocks.

Demos

Why bother with this ?

We all love markdown, pandoc and the likes, but sometimes we may need extra-features, or LESS features. I have been through a bunch of markdown parsers myself and found a limited choice in that you always have to choose between 100kb-I-do-not-really-need-this libs, and 1kb-I-wish-this-existed libs.

Markty provides a zero calory core (just a cursive function using .exec()), then we enrich this core with some plugins that are basically a set of regex rules. Combining the core + the rules = you choose your balance between size/value.

At the end of the day, this library will be useful if and only if it can gather a maximum rule set we could pick ideas/code from. Originally I thought about giving it as a Gist, but I think it's handy to be able to get it through npm as well :)

So we depend on YOU !!

Let's create a shiny new Markdown rule !

Requirement: make all text surrounded by two ~, striked-through.

e.g: ~~erase me~~ should become <s>Erase me</s>

First install, npm install markty --save then:

import markty from 'markty'

// we need a string to be parsed
let myString = `Please ~~erase this!~~`

// 1. starts with ~~, not followed by whitespace char, 
// 2. then match anything but exclusing any of: two line-breaks, a possible whitespace char, a whitespace char + one ~
// 3. match the first pattern (~~)
// That means only ~~hello world~~ will match but not: ~~ hello world~~, ~~hello world ~~, ~~ hello world ~~, etc...
// make this pattern look for the string globally
let matchingRule = /(\~{2})(?!\s)((?:(?!\n\n|\s?\~).)+)(\1)/g

// Answers the question: what should we do once we matched this ?
let businessRules = (string, match) => {
  if (match[2]) {
    // match returns an array containing all regex groups matched
    // in this case, match[1] is the 2nd group of our matchingRule regex
    // so match[1] represents the actual string between ~~
    return '<s>' + match[2] + '</s>'
  } 
}

// Call markty to process rules
let myParsedString = markty(myString, matchingRule, businessRules)

console.log(myParsedString)
// returns:
// Please <s>~~erase this!~~</s>

Nesting

Here is where Markty starts to show its value. By using the true option like this:

markty(myString, matchingRule, businessRules, true)

we tell Markty to continue processing it's own output until it finds any matchingRule's pattern again. That means it indefinitely scans the output string for all possible occurences of the matchingRule. This is useful in nesting situations. For example:

// This rule matches any string surrounded with doubling  `/`, `*`, `=`, `-`, `-`, `_`, `^`, and `~`.
let matchingRule = /([/*=\-_^~]{2})(?!\s)((?:(?!\n\n|\s?\1).)+)(\1)/g

We owe to do somthing with those matches, like

if a string is found enclosed by two == : surround it with <mark></mark>

if a string is found enclosed by two // : surround it with <em></em>

etc...

A business rule for this regex could look like:

let businessRules = (string, match) => {
  let [token, opening, content, closing] = match, t

  const TAGS = ['em', 'strong', 'mark', 's', 'u', 'sup', 'sub']
  const PATTERNS = ['//', '**', '==', '--', '__', '^^', '~~']

  if (content) {
		t = TAGS[PATTERNS.indexOf(open)]
  	return `<${t}>${content}</${t}>`
  }
}

Then we can use Markty to process a random string:

let myString = `
==//__**--marked italic underlined bold striked--**__//==
`

let myParsedString = markty(myString, matchingRule, businessRules)

console.log(myParsedString)
// returns:
// <mark>//__**--marked italic underlined bold striked--**__//</mark>

As you can see, this is not quite what we wanted: <mark>//__**--marked italic underlined bold striked--**__//</mark>.

To let Markty parse this result again, we need to allow it so by setting the last option to true:

let myParsedString = markty(myString, matchingRule, businessRules, true) // added true to indefinitely match

console.log(myParsedString)
// returns:
// <mark><em><u><strong><s>marked italic underlined bold striked</s></strong></u></em></mark>

Huray !! Markty RE-processed the output: RE-tried the matchingRule and RE-processed the businessRules until no further matchingRule was found.

Is this markdown ?

Yes and No. If you ask: "will it be commonMark compliant ?", then "NO NEVER".

Now, yes, you can re-write some markdown rules using Markty, and you could eventually use it as a PRE/POST-processor of any other parser: be free :)

Credits

This was HEAVILY inspired by snarkdown (big kudos to @Jason Miller).