bbcode-compiler
TypeScript icon, indicating that this package has built-in type declarations

0.1.5 • Public • Published

BBCode Compiler

This is a fast BBCode parser and HTML generator with TypeScript support.

Note: This package is only available in ESM format.

Usage

import { generateHtml } from 'bbcode-compiler'

// <strong>Hello World</strong>
const html = generateHtml('[b]Hello World[/b]')

Extending With Custom Tags

import { generateHtml, getTagImmediateText, htmlTransforms, getWidthHeightAttr } from 'bbcode-compiler'

const customTransforms: typeof htmlTransforms = [
    // Default tags included with this package
    ...htmlTransforms,

    // You can override a default tag by including it after the original in the transforms array
    {
        name: 'b',
        start: () => '<b>',
        end: () => '</b>',
    },

    // Create new tag
    // You should read the TypeScript interface for TagNode in src/parser/AstNode.ts
    // You can also use the included helper functions like getTagImmediateText and getWidthHeightAttr
    {
        name: 'youtube',
        skipChildren: true, // Do not actually render the "https://www.youtube.com/watch?v=dQw4w9WgXcQ" text
        start: (tagNode) => {
            const src = getTagImmediateText(tagNode)
            if (!src) {
                return false
            }

            const matches = /youtube.com\/watch\?v=(\w+)/.exec(src)
            if (!matches) {
                return false
            }

            const videoId = matches[1]
            const { width, height } = getWidthHeightAttr(tagNode)

            return `
                <iframe
                    width="${width ?? 560}"
                    height="${height ?? 315}"
                    src="https://www.youtube.com/embed/${videoId}"
                    title="YouTube Video Player"
                    frameborder="0"
                    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
                    allowfullscreen
                ></iframe>
            `
        },
    },
]

// <iframe
//     width="560"
//     height="315"
//     src="https://www.youtube.com/embed/dQw4w9WgXcQ"
//     title="YouTube Video Player"
//     frameborder="0"
//     allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
//     allowfullscreen
// ></iframe>
const html = generateHtml('[youtube]https://www.youtube.com/watch?v=dQw4w9WgXcQ[/youtube]', customTransforms)

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.5
    3
    • latest

Version History

Package Sidebar

Install

npm i bbcode-compiler

Weekly Downloads

3

Version

0.1.5

License

MIT

Unpacked Size

132 kB

Total Files

96

Last publish

Collaborators

  • trinovantes