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
121
decreased by-37.95%
Maintainers
1
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"/]" }

Features

  • Parse individual BB code tags
  • Supports self-closing tags
  • Supports tag properties, with or without string literals and escape characters

Installation

Installation with npm:

npm install shortcode-tree --save

Usage

Parsing a single short code

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, as documented below.

The Shortcode object

PropertyTypeDescription
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.

Extracting multiple shortcodes from text

If there are multiple shortcodes within one piece of text, you can extract them using the ShortcodeExtractor:

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

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

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

This function will only extract shortcodes from one level, and does not process any child tags.

FAQs

Last updated on 20 Nov 2017

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