https://github.com/tada5hi/bulletin-board-code
This library provides utitlites to parse BBCodes to HTML and HTML to BBCodes.
https://github.com/tada5hi/bulletin-board-code
bbcode bbcode-parser bbcode-to-html bulletin-board-code html-to-bbcode parse parser typescript
Last synced: 7 months ago
JSON representation
This library provides utitlites to parse BBCodes to HTML and HTML to BBCodes.
- Host: GitHub
- URL: https://github.com/tada5hi/bulletin-board-code
- Owner: tada5hi
- License: mit
- Created: 2022-06-05T14:05:09.000Z (over 3 years ago)
- Default Branch: develop
- Last Pushed: 2024-07-15T14:13:34.000Z (about 1 year ago)
- Last Synced: 2024-10-21T06:35:48.978Z (12 months ago)
- Topics: bbcode, bbcode-parser, bbcode-to-html, bulletin-board-code, html-to-bbcode, parse, parser, typescript
- Language: TypeScript
- Homepage:
- Size: 1.31 MB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 12
-
Metadata Files:
- Readme: README.MD
- Changelog: CHANGELOG.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Bulletin Board Code โ๏ธ
[](https://badge.fury.io/js/bulletin-board-code)
[](https://github.com/tada5hi/bulletin-board-code/actions/workflows/main.yml)
[](https://codecov.io/gh/Tada5hi/bulletin-board-code)
[](https://snyk.io/test/github/Tada5hi/bulletin-board-code)
[](https://github.com/semantic-release/semantic-release)**B**ulletin **B**oard **Code** (BBCode) is a library to transform a string from:
- BBCode to HTML
- HTML to BBCode**Table of Contents**
- [Installation](#installation)
- [Features](#features)
- [Usage](#usage)
- [BBCode to HTML](#bbcode-to-html)
- [HTML to BBCode](#html-to-bbcode)
- [Handlers](#handlers)
- [Set](#set)
- [Unset](#unset)
- [Types](#types)
- [ParserOptions](#parseroptions)
- [License](#license)## Installation
```bash
npm install bulletin-board-code --save
```## Features
- โจ bidirectional transformation (HTML & BBCode)
- ๐ support nested BBCodes
- ๐ ๏ธ automatic repair mode
- ๐งฉ set/unset custom handlers for transformation## Usage
For an overview of all predefined BBCodes,
take a look at the following [file](src/handler/constants.ts).
To see them in action, the test directory can be inspected.### BBCode To HTML
```typescript
import { Parser } from 'bulletin-board-code';// initialize the parser
const parser = new Parser();console.log(parser.toHTML('[b]foo[/b]'));
// foo```
### HTML To BBCode
```typescript
import { Parser } from 'bulletin-board-code';// initialize the parser
const parser = new Parser();console.log(parser.toBBCode('foo'));
// [b]foo[/b]
```## Handlers
A handler describes how parsed tokens should be transformed to HTML or BBCode.
Furthermore, the handler specifies conditions, when to match with a token.### Set
To set a (new) handler use the following approach:
```typescript
import { Parser } from 'bulletin-board-code';const parser = new Parser();
parser.setHandler('lazy', {
conditions: [{ attribute: { lazy: null } }],
bbcode: '[lazy]{0}[/lazy]',
html: 'lazy: {0}'
});console.log(parser.toHTML('[lazy]foo[/lazy]'));
// lazy: fooconsole.log(parser.toBBCode('lazy: foo'));
// [lazy]foo[/lazy]
```### Unset
To unset a handler use the following approach:
```typescript
import { Parser, unsetHandler } from 'bulletin-board-code';const parser = new Parser();
parser.unsetHandler('h1');
parser.unsetHandler(['h2', 'h3']);console.log(parser.toHTML('[h1]foo[/h1]'));
// [h1]foo[/h1]console.log(parser.toBBCode('
foo
'));
//foo
```## Types
### ParserOptionsThe following options can be passed to the parser as constructor argument.
```typescript
declare type ParserOptions = {
/**
* Add a set of handlers to the already predefined ones.
*
* default: {}
*/
handlers: Record,/**
* If to add a new line before block level elements
*
* default: false
*/
breakBeforeBlock: boolean,/**
* If to add a new line after the start of block level elements
*
* default: false
*/
breakStartBlock: boolean,/**
* If to add a new line before the end of block level elements
*
* default: false
*/
breakEndBlock: boolean,/**
* If to add a new line after block level elements.
*
* default: true
*/
breakAfterBlock: boolean,/**
* If to remove empty tags.
*
* default: true
*/
removeEmptyTags: boolean,/**
* If to fix invalid nesting, like block level elements inside inline elements.
*
* default: true
*/
fixInvalidNesting: boolean,/**
* If to fix invalid children. i.e.
* A tag which is inside a parent that doesnโt allow that type of tag as a child.
*
* default: true
*/
fixInvalidChildren: boolean,/**
* The default attribute quote type.
*
* default: QuoteType.auto
*/
quoteType: `${QuoteType}`,/**
* Lazy transformation without handler.
* Otherwise, library will attempt to construct html or bbcode without handler.
*
* default: true
*/
lazyTransformation: boolean
};
```## License
Made with ๐
Published under [MIT License](./LICENSE).