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

xbbcode

v2.0.4

Published

An extensible BBCode parser.

Downloads

209

Readme

xbbcode.js

This is a JavaScript version of XBBCode.

xbbcode parses arbitrary text to find a properly nested tree of BBCode tags in square brackets, and renders these tags according to markup strings and rendering callbacks that it has been given during parser creation.

Usage

xbbcode.js creates an XBBCode class, which is also its only export. (This way, the module can be used both via require() and by simple inclusion in a web page.)

To create a parser, this class is instantiated with a single argument containing the mapping of BBCode tags to output, in the form of plugins described below:

plugins   ::= { name : renderer }*
name      ::= string (must match /\w+/)
renderer  ::= string | function

A string renderer consists of any text with the following optional placeholders: {content}, {option}, {name}, {attribute.*} or `{source} for any attribute name.

  • In [tag1=abc]xyz[/tag1], {option} is abc and {content} is xyz.
  • In [tag2 abc="xyz"][tag3][/tag3][/tag2], {attribute.abc} is xyz, {source} is [tag3][/tag3], and {content} is whatever [tag3][/tag3] is rendered as.

The {name} placeholder is replaced with the tag name.

A function renderer receives a BBCodeElement instance as its only argument. This object has the following methods, whose return values match the values in the previous section:

  • getName()
  • getContent()
  • getOption()
  • getAttribute(key)
  • getSource()

Sample

const XBBCode = require('xbbcode');
const filter = XBBCode.create({
  b:     '<strong>{content}</strong>',
  quote: '<q>{content}</q>',
  code:  '<code>{content}</code>',
  url:   tag =>
    (`<a href="http://example.com/outgoing?url=${encodeURI(tag.getOption())}">`
    + tag.getContent()
    + '</a>'),
  img:   '<img src="{source}" alt="Image({source}" />',
});

input = '[quote][code][url=http://example.org/][img]http://example.org/[b]image[/b].png[/img][/code][/url][/quote]';
console.log(filter(input));

// Note that the [url] tag is improperly nested; it closes after the [code] tag.
// It will therefore not be rendered, to avoid generating improperly
// nested output markup.
// The [img] tag is set not to allow BBCode inside it,
// Which prevents the [b] tag from being rendered.
// "<q><code>[url=http://example.org/]<img src="http://example.org/[b]image[/b].png" /></a></code>[/url]</q>"

The recommended way to use xbbcode.js is to import the src/xbbcode.js file.

On legacy platforms, it may be necessary to run make, which uses Babel to translate the file to a pre-ES2015 syntax, and then use ./xbbcode.js in the root folder.

LICENSE

The MIT License (MIT)

Copyright (c) 2014-2016 Christoph Burschka

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.