@onachi/bbast
v0.1.6
Published
BBCode abstract syntax tree types and utilities based on unist
Maintainers
Readme
bbast
BBCode Abstract Syntax Tree types and utilities based on unist.
Installation
npm install bbast
# or
pnpm add bbast
# or
yarn add bbastUsage
import type { Root, BBContent, Text, Bold } from 'bbast'
import { isText, isBBTag, isParent } from 'bbast'
// Example AST for [b]Hello[/b] world
const ast: Root = {
type: 'root',
children: [
{
type: 'bold',
children: [
{
type: 'text',
value: 'Hello'
}
]
},
{
type: 'text',
value: ' world'
}
]
}
// Type guards
function processNode(node: BBContent) {
if (isText(node)) {
console.log('Text:', node.value)
} else if (isBBTag(node)) {
console.log('BBTag:', node.tagName)
} else if (isParent(node)) {
node.children.forEach(processNode)
}
}API
Node Types
Root Nodes
Root- The root node of a BBCode document
Content Nodes
Text- Plain text contentBBTag- Generic BBCode tag with childrenBBSelfClosingTag- Self-closing BBCode tagLineBreak- Line breakParagraph- Paragraph container
Formatting Nodes
Bold- Bold text[b]Italic- Italic text[i]Underline- Underlined text[u]Strikethrough- Strikethrough text[s]Code- Code text[code]Color- Colored text[color]Size- Sized text[size]Font- Font family[font]
Layout Nodes
Center- Center alignment[center]Left- Left alignment[left]Right- Right alignment[right]
Block Nodes
Quote- Quote block[quote]List- List container[list]ListItem- List item[*]or[li]Table- Table[table]TableRow- Table row[tr]TableCell- Table cell[td]or[th]
Media Nodes
Link- Hyperlink[url]Image- Image[img]
Union Types
BBContent- Union of all node typesBBParentContent- Union of all parent node typesBBLeafContent- Union of all leaf node types
Utility Functions
isParent(node)- Check if node is a parent nodeisLeaf(node)- Check if node is a leaf nodeisType(node, type)- Check if node has specific typeisText(node)- Type guard for text nodesisBBTag(node)- Type guard for BBTag nodesisRoot(node)- Type guard for root nodes
Type Utilities
NodeByType<T>- Extract nodes by typeBBData- Extended unist Data interfaceBBNode- Base BBCode node interfaceBBParent- Base BBCode parent interfaceBBLiteral- Base BBCode literal interface
Specification
This package follows the unist specification for syntax trees. Each node implements the base Node interface with additional BBCode-specific properties.
Node Structure
All nodes have:
type: String identifying the node typedata?: Optional data object for additional information
Parent nodes additionally have:
children: Array of child nodes
Literal nodes additionally have:
value: String content of the node
BBCode Extensions
BBCode nodes extend the base unist types with:
- Tag-specific properties (e.g.,
urlfor Link,colorfor Color) - Attribute handling for complex tags
- Support for both container and self-closing tags
License
MIT
