Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@panzer1119/bbcode-parser

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@panzer1119/bbcode-parser

BB code parser written in TypeScript.

latest
Source
npmnpm
Version
1.1.1
Version published
Weekly downloads
3
-57.14%
Maintainers
1
Weekly downloads
 
Created
Source

BBCodeParser

An extensible BB code parser written in TypeScript that can be used both in the browser and Node.js.

Usage

<script src="bbCodeParser.min.js"></script>
...
var parser = new BBCodeParser(BBCodeParser.defaultTags());
var inputText = "[b]Bold text[/b]";
var generatedHtml = parser.parseString(inputText);

Node.js: npm install bbcode-parser

var BBCodeParser = require('bbcode-parser');
var parser = new BBCodeParser(BBCodeParser.defaultTags());
var html = parser.parseString('[b]Bold text[/b]');

Custom tags

BBTag constructor:

  • tagName: The name of the tag.
  • insertLineBreaks: Indicates if the tag inserts line breaks (\n -> <br>) in the content.
  • suppressLineBreaks: Suppresses line breaks for any nested tag.
  • noNesting: If the tags doesn't support nested tags.
  • tagGenerator: The HTML generator for the tag. If not supplied the default one is used: <tagName>content</tagName>.
var bbTags = {};

//Simple tag. A simple tag means that the generated HTML will be <tagName>content</tagName>
bbTags["b"] = BBTag.createSimpleTag("b");

//Tag with a custom generator.
bbTags["img"] = BBTag.createSimpleTag("img", function (tag, content, attributes) {
	return "<img src=\"" + content + "\" />";
});

//Tag with a custom generator + attributes
bbTags["url"] = BBTag.createSimpleTag("url", function (tag, content, attributes) {
	var link = content;

	if (attributes["site"] != undefined) {
		link = escapeHTML(attributes["site"]);
 	}

	if (!startsWith(link, "http://") && !startsWith(link, "https://")) {
		link = "http://" + link;
	}

	return "<a href=\"" + link + "\" target=\"_blank\">" + content + "</a>";
});

//A tag that doesn't support nested tags. Useful when implementing code highlighting.
bbTags["code"] = new BBTag("code", true, false, true, function (tag, content, attributes) {
    return "<code>" + content + "</code>";
});

var parser = new BBCodeParser(bbTags);

Documentation

See the wiki.

Build

To run the build script you need:

  • Node.js
  • TypeScript (npm install -g typescript)
  • uglify-js (npm install -g uglify-js)

Keywords

BBCode

FAQs

Package last updated on 04 Sep 2021

Did you know?

Socket

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