BBCode
BBCode is what most forum software stores: phpBB before 3.2, vBulletin, XenForo, SMF, MyBB and many others. Every forum has its own dialect, so the parser knows the common tags and their aliases, repairs tags that are closed in the wrong order, and lets you register handlers for the tags only your forum has.
Quick start
Section titled “Quick start”require "markbridge/bbcode"
bbcode = "[b]Hello[/b] [url=https://example.com]world[/url]!"result = Markbridge.bbcode_to_markdown(bbcode)result.markdown# => "**Hello** [world](https://example.com)!"result is a Markbridge::Conversion. Next to markdown it has unknown_tags, diagnostics (what the parser had to repair) and errors.
To get the AST without rendering it:
parse = Markbridge.parse_bbcode(bbcode)parse.ast# => an AST::Document with Bold("Hello"), Text(" "), Url("world") and Text("!")Supported tags
Section titled “Supported tags”Tag names are read in any case, [B] like [b]. The tags in one row are aliases: they have the same handler.
Options and other forms
Section titled “Options and other forms”- Code language:
[code=ruby]or[code lang=ruby]. - Ordered list:
[list=1], the same as[ol]. - Quote with an author:
[quote="author, post:1, topic:2"]. - Links:
[url=href]text[/url]or[url]href[/url].[email=addr]text[/email]and[img=src]alt[/img]work the same way. - A link without a text of its own is written as the bare URL, not as
[href](href). Discourse links a bare URL itself and shows a onebox for it when it stands on its own line. - Relative links and images are written as they are. Discourse drops an address without a scheme and without a leading slash, so give the renderer the address of the old forum as
base_url:. - An option without quotes runs up to the
], with its spaces:[quote=John Doe]quotes “John Doe”. It ends earlier at a named attribute, so[quote=John Doe post_id=4]has the option “John Doe” and the attributepost_id. A line break ends it too.
HandlerRegistry.default is the exact list.
Using the parser directly
Section titled “Using the parser directly”bbcode_to_markdown runs the parser and the renderer for you. You can also call them yourself:
parser = Markbridge::Parsers::BBCode::Parser.newast = parser.parse("[b]bold[/b] with [unknown]mystery[/unknown]")
parser.unknown_tags# => {"unknown" => 2} # count of open + close tokens seen
renderer = Markbridge::Renderers::Discourse::Renderer.newrenderer.render(ast)# => "**bold** with mystery"Markbridge.parse_bbcode is easier in most cases. It returns a Parse that has the AST, unknown_tags and diagnostics in one object. Calling the renderer yourself also skips normalizing and the final cleanup of the output, which Markbridge.render(parse) does for you.
Closing strategies
Section titled “Closing strategies”BBCode from real forums often closes tags in the wrong order, or not at all. A closing strategy decides what the parser makes of that. There are two:
- Reordering is the default. It looks ahead up to five closing tags and matches them with the open tags, so
[b][i]text[/b][/i]is bold and italic text. - Strict never reorders. A closing tag closes the open tags inside it, and a closing tag that matches nothing stays as text. Use it when your source is clean and you want to see every tag that is not.
To use the strict one:
require "markbridge/bbcode"
handlers = Markbridge::Parsers::BBCode::HandlerRegistry.build_from_default do |registry| reconciler = Markbridge::Parsers::BBCode::ClosingStrategies::TagReconciler.new(registry:) registry.closing_strategy = Markbridge::Parsers::BBCode::ClosingStrategies::Strict.new(reconciler) end
Markbridge.bbcode_to_markdown("[b][i]text[/b][/i]", handlers:)Unknown tags
Section titled “Unknown tags”A tag without a handler does not raise. The tag itself is dropped and its content is parsed like any other content:
Markbridge.bbcode_to_markdown("[unknown]inner text[/unknown]")# => "inner text"unknown_tags on the result counts them, opening and closing tags each: {"unknown" => 2} here. Collect the counts over all your posts to see which tags need a handler.
Limits
Section titled “Limits”- Nesting stops at 100 levels. A tag that opens deeper than that stays in the output as text, and
diagnostics[:depth_exceeded_count]counts it. - A closing tag closes at most five open tags inside it.
Reorderingalso looks no further than five tags to find a match.
Customizing
Section titled “Customizing”Register a handler for a tag the parser does not know, or to replace a built-in one:
handlers = Markbridge::Parsers::BBCode::HandlerRegistry.build_from_default do |registry| registry.register("callout", MyCalloutHandler.new) end
Markbridge.bbcode_to_markdown(input, handlers:)Extending Markbridge shows the handler, the AST node and the renderer tag for a new tag step by step. To change a built-in handler only for some cases, wrap it: see Wrapping a default handler.