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

shortcode-to-tree

v1.0.2

Published

Parse shortcode text into a tree structure

Downloads

9

Readme

shortcode-to-tree

npm version

Shortcodes are a nice compromise for human editors between the simplicity of markup languages like markdown and the complexities of html.

However, all shortcode parsers I have found focus purely on the "how to render this as html" aspect of it, but wouldn't it be nice if you could have it in a tree data structure before rendering so that it can be walked to allow operations like mapping or side-loading data.

Usage

npm install shortcode-to-tree
yarn add shortcode-to-tree

parser

This function is the main powerhouse of this library. You just need to call it with the text that you'd like converted.

import { parser, createSimpleTag } from 'shortcode-to-tree';

const input = 'Hello [b]World![/b]';

const tree = parser(input, {
  b: createSimpleTag('b'),
});

console.log(JSON.stringify(tree, null, 2));

/*
{
  "type": "element",
  "name": "root",
  "elements": [
    {
      "type": "text",
      "text": "Hello "
    },
    {
      "type": "element",
      "name": "b",
      "elements": [
        {
          "type": "text",
          "text": "World!"
        }
      ]
    }
  ]
}
*/

You will notice in the output above that there is this magic root element. All trees need roots; and the parser automatically wraps your input shortcode for you (unless you provide one).

Custom shortcodes can also be provided. The two helper functions createSimpleTag & createAttributeTag will aid you in this. To utlise these in the parser you can provide as hashmap of them to the parser:

import { parser, createAttributeTag } from 'shortcode-to-tree';

const input = 'Hello [foo bar="baz"]World![/foo]';

const tree = parser(input, {
  foo: createAttributeTag('foo'),
});

console.log(JSON.stringify(tree, null, 2));

/*
{
  "type": "element",
  "name": "root",
  "elements": [
    {
      "type": "text",
      "text": "Hello "
    },
    {
      "type": "element",
      "name": "foo",
      "attributes": {
        "bar": "baz"
      },
      "elements": [
        {
          "type": "text",
          "text": "World!"
        }
      ]
    }
  ]
}
*/

Above you will now notice that the attributes for that shortcode have been supplied via the attributes key on a given element.

createSimpleTag

This shortcode handler is for tags which do not support attributes, such as [b] or [i].

import { parser, createSimpleTag } from 'shortcode-to-tree';

const tags = {
  foo: createSimpleTag('foo'),
};

const input = 'Hello [foo bar="baz"]World![/foo]';

const tree = parser(input, {
  foo: createAttributeTag('foo'),
});

console.log(JSON.stringify(tree, null, 2));

/*
{
  "type": "element",
  "name": "root",
  "elements": [
    {
      "type": "text",
      "text": "Hello "
    },
    {
      "type": "element",
      "name": "foo",
      "elements": [
        {
          "type": "text",
          "text": "World!"
        }
      ]
    }
  ]
}
*/

createAttributeTag

As shown above this is for shortcodes which require attribute support. All attributes are treated as strings.

import { parser, createAttributeTag } from 'shortcode-to-tree';

const tags = {
  foo: createAttributeTag('foo'),
};

const input = 'Hello [foo bar="baz"]World![/foo]';

const tree = parser(input, {
  foo: createAttributeTag('foo'),
});


console.log(JSON.stringify(tree, null, 2));

/*
{
  "type": "element",
  "name": "root",
  "elements": [
    {
      "type": "text",
      "text": "Hello "
    },
    {
      "type": "element",
      "name": "foo",
      "attributes": {
        "bar": "baz"
      },
      "elements": [
        {
          "type": "text",
          "text": "World!"
        }
      ]
    }
  ]
}
*/

Default tags

This package exposes several pre-configured tags. These can be imported and used like so:

import { defaultTags, parser } from 'shortcode-to-tree';

const tree = parser(input, defaultTags);

If you want to add more tags on top of this you can just merge the default tags into your custom tags:

import { defaultTags, createSimpleTag, parser } from 'shortcode-to-tree';

const tags = {
  ...defaultTags,
  foo: createSimpleTag('foo'),
}

const tree = parser(input, tags);

The default tags are:

  • b (simple tag)
  • i (simple tag)