Socket
Socket
Sign inDemoInstall

shortcode-tree

Package Overview
Dependencies
0
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    shortcode-tree

Parser library for reading short codes (BB codes) into a tree structure


Version published
Weekly downloads
145
increased by61.11%
Maintainers
1
Install size
134 kB
Created
Weekly downloads
 

Readme

Source

shortcode-tree

A node.js parser library for reading shortcodes.

npm Build Status

Introduction

shortcode-tree offers generic parsing functionality for text containing short codes (also known as bb codes).

This library does not convert short codes to HTML (like many other libraries do), but it converts generic shortcode/HTML input to pure JavaScript objects.

No set up is required, and you do not need to pre-define a list of shortcodes.

Example:

ShortcodeParser.parseShortcode('[image id=123 src="bla.jpg" align="center"/]');

// Shortcode {
//     name: 'image',
//     content: null,
//     properties: { id: '123', src: 'bla.jpg', align: 'center' },
//     isSelfClosing: true,
//     codeText: '[image id=123 src="bla.jpg" align="center"/]',
//     offset: 0 }

Features

  • Parse sets of mixed shortcodes and text/HTML into a tree structure
  • Parse individual shortcode fragments
  • Supports self-closing tags
  • Supports tag properties (with or without string literals)
  • Stringify Shortcode objects to shortcode text or HTML equivalent
  • Extract only text / HTML from a document containg shortcodes

Installation

Installation with npm:

npm install shortcode-tree --save

Usage

Start with the raw text you want to process, and feed it to the parse function:

var ShortcodeTree = require('shortcode-tree').ShortcodeTree;

var rootNode = ShortcodeTree.parse(inputText);
console.log(rootNode.children.length);

This function will return a ShortcodeNode object. Each ShortcodeNode may contain a set of child nodes.

If you have regular text or HTML mixed in at the same level as a shortcode, a ShortcodeNode may also contain TextNode children to hold these items. Text nodes themselves cannot hold children, only text.

The ShortcodeNode object

A shortcode node contains the following fields:

FieldTypeDescription
textstringThe raw text content (code) of this node.
shortcodeShortcode or nullInformation about the parsed Shortcode represented by this node. Will be null if this is the root node.
childrenarrayList of children. May contain ShortcodeNode and TextNode items.

The Shortcode object

A parsed shortcode. Typically available through the shortcode field of a shortcode node.

Fields
FieldTypeDescription
namestringTag name
propertiesobjectKey/value object with property values indexed by their name
contentstring or nullThe raw, unparsed content of the tag. May contain HTML and other short codes. NULL for self-closing tags.
isSelfClosingboolIndicates whether this is a self-closing tag without any content.
codeTextstringThe raw shortcode text, as it was parsed.
offsetintegerOffset index, relative to the original input string.
Methods
SignatureReturnsDescription
stringify()stringFormats the data in the Shortcode object to shortcode text
stringifyHtml([string/null] tagName)stringFormats the data in the Shortcode object to HTML text
hasProperty([string] key)booleanGets whether property with name key exists.
getProperty([string] key)value or nullGets value of property with name key, or NULL if not set.
setProperty([string] key, value)voidAdd or update property with given key and value.
addChild([Shortcode] shortcode)voidAppend a shortcode item to this shortcode's content using stringify.
appendContent([string] content)voidAppend content to the existing content.

The TextNode object

A piece of raw text or HTML that was placed on the same level as another shortcode. This is always a child of a shortcode node.

FieldTypeDescription
textstringRaw text or code

Dumping tree structure

By calling traceTree() on a node, you can dump a simple visualisation of the parsed tree structure.

var sampleInput =
    "[row]" +
        "[col]" +
            "<h1>My article</h1>" +
            "[img src=\"image.jpg\"/]" +
        "[/col]" +
        "[col]" +
            "<p>Just a boring text sample column</p>" +
        "[/col]" +
    "[/row]"

var rootNode = ShortcodeTree.parse(sampleInput);
rootNode.traceTree();

The above example would generate the following console output:

+++++ Root Node +++++
[Shortcode Node: row], content: [col]<h1>My article</h1>[img src="image.jpg"/][/col][col]<p>Just a boring text sample column</p>[/col]
--- [Shortcode Node: col], content: <h1>My article</h1>[img src="image.jpg"/]
------ [Text Node], content: <h1>My article</h1>
------ [Shortcode Node: img], content: null
--- [Shortcode Node: col], content: <p>Just a boring text sample column</p>

Advanced usage

Parsing a single short code fragment

To parse one individual short code, use the ShortcodeParser:

var parser = require('shortcode-tree').ShortcodeParser;

var shortcode = parser.parseShortcode("[b]example content[/b]");
console.log(shortcode.content); // example content

The parseShortcode method returns a Shortcode object.

Custom parser options

When calling parseShortcode, you can add an options object as a second argument.

parser.parseShortcode("[b]example content[/b]", { /** insert options **/ })

The following options are available:

OptionTypeDefaultDescription
modestringnormalParser mode to operate in. See table below.
offsetinteger0Offset from the start of the input string, where parsing should begin.
throwErrorsbooleantrueIf enabled: On shortcode parse error, an Error is thrown. If disabled: false is returned on parse error.
precisebooleanfalseIf things aren't working as expected, enable this for deep recursive parsing. Reduces performance exponentially.
selfClosingTagsarray[]You can specify a list of tags that should always be treated as self-closing. Needed when they don't use the "[selfcloser/]" syntax.
predictSelfClosingbooltrueIf enabled, self-closing tags will be predicted by checking if any closing tags can be found anywhere in the input buffer. Improves stability in most situations, but breaks if your tags are "sometimes" self closing.

The default options are defined in ShortcodeParser.DEFAULT_OPTIONS.

Parser modes
ConstantValueDescription
MODE_NORMALnormalParses text into a Shortcode object.
MODE_GET_OPENING_TAG_NAMEtag_nameHalts parsing once the tag name is found, and returns it as a string.

Extracting one level of shortcodes from text

The ShortcodeExtractor is a component that can extract a set of Shortcodes from a single piece of text, without traversing deeper than one level.

var extractor = require('shortcode-tree').ShortcodeExtractor;

var shortcodes = extractor.extractShortcodes("Hey, [b]bold[/b] and [i]beautiful[/i].");
console.log(shortcodes);

// [ Shortcode {
//     name: 'b',
//     content: 'bold',
//     properties: {},
//     isSelfClosing: false,
//     codeText: '[b]bold[/b]',
//     offset: 5 },
//   Shortcode {
//     name: 'i',
//     content: 'beautiful',
//     properties: {},
//     isSelfClosing: false,
//     codeText: '[i]beautiful[/i]',
//     offset: 21 } ]

The extractShortcodes method returns an array of Shortcode objects.

Extracting text/HTML from a shortcode fragment

var treeParser = require('shortcode-tree').ShortcodeTree;

var input = `root [row][column]deep[/column][/row]`;
treeParser.extractTextContent(input);

// returns: "root deep"

FAQs

Last updated on 15 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc