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

universal-shortcodes

v0.9.2

Published

A universal shortcode parser that can work with Wordpress shortcodes, BBCodes and also more customized variants

Downloads

29

Readme

Universal Shortcodes

A universal parser for wordpress shortcodes, BBCode and related markup languages. It is (going to be) highly customizable but for now it should support out of the box everything that wordpress does.

How to use

  1. Install the library npm i --save universal-shortcodes
  2. Import the library:
    1. In Node.js with var compileShortcodes = require('universal-shortcodes).compileShortcodes
    2. In ES6 with import {compileShortcodes} from 'universal-shortcodes';
  3. Call compileShortcodes passing your text and a callback handler for transforming shortcodes. The callback is defined below.

compileShortcodes callback

The second argument of compileShortcodes is a callback that takes three arguments:

  1. the name of the shortcode;
  2. an object of arguments; named arguments have the key as specified; unnamed arguments are declared as keys 0, 1, 2... from the left;
  3. contents of the shortcode, in case of self-closed shortcodes it's an empty string.

For example, given shortcode [test "first" "second" value="val1" other="val2"]Inner text[/test] the following arguments would be passed:

shortcode = 'test'
args = {
  0: 'first',
  1: 'second',
  value: 'val1',
  other: 'val2'
}
contents = 'Inner text'

Example usage

import {compileShortcodes} from 'universal-shortcodes';

const text = 'Before anything [strong key="value"]inside strong[/duck] After everything';
const result = compileShortcodes(text, (shortcode, args, contents) => {
	const argsArray = Object.keys(args).map(key => {
		return `data-${key}="${args[key]}"`;
	});
	
	let argsString = argsArray.join(' ');
	argsString = argsString ? ` ${argsString}` : '';
	
	if (contents){
		return `<${shortcode}${argsString}>${contents}</${shortcode}>`;
	} else {
		return `<${shortcode}${argsString}/>`;
	}
});

console.log(result);
// Before anything <strong data-key="value"/>inside strong<duck/> After everything

Internals

Internal methods are exposed via property _universalShortcodes - their APIs are unlikely to change should be safe to use if you ever feel a need to do it.

Options

  1. shortcodeOpenCharacter (string 1 character), default [ - specifies the character used as the opening of a shortcode.
  2. shortcodeCloseCharacter (string 1 character), default ] - specifies the character used as the closing of a shortcode.

Currently the options object does not change anything.

Todo:

  1. Specifying delimiter characters for shortcodes
  2. Specifying how to handle incorrectly nested shortcodes (crash)