MarkdigToBBCode 1.0.1
See the version list below for details.
dotnet add package MarkdigToBBCode --version 1.0.1
NuGet\Install-Package MarkdigToBBCode -Version 1.0.1
<PackageReference Include="MarkdigToBBCode" Version="1.0.1" />
<PackageVersion Include="MarkdigToBBCode" Version="1.0.1" />
<PackageReference Include="MarkdigToBBCode" />
paket add MarkdigToBBCode --version 1.0.1
#r "nuget: MarkdigToBBCode, 1.0.1"
#:package MarkdigToBBCode@1.0.1
#addin nuget:?package=MarkdigToBBCode&version=1.0.1
#tool nuget:?package=MarkdigToBBCode&version=1.0.1
BBCode-Markdown Conversion Library
AI Development Disclaimer
This paragraph isn't AI generated. This library is my first attempt at actually using AI tools to write a library. It works better than I want to admit, especially when you focus it to take small steps and guide it to write code that passes tests. The other paragraphs of this readme are AI generated. I'm sorry, I hate documentation. And writing code apparently.
I initially used Google Gemini to design the fundamental approach but quickly got tired of the copy-pasting and sync work it requires, so I switched to VS26 with CoPilot integration halfway through.
Brief overview
A focused, two-way conversion library that reliably translates between Markdown and BBCode. It uses Markdig to produce a Markdown AST and a compact, test-driven BBCode parser. The library prioritizes deterministic, loss-minimizing conversions, correct nesting, and straightforward extensibility for custom tags. Target framework: .NET 8.
Key features
- Bi-directional conversion: Markdown → BBCode and BBCode → Markdown.
- Preserves structure and nesting for headings, lists, links, images, code, emphasis, quotes, and horizontal rules.
- Extensible renderer and element model for custom BBCode tags or custom Markdown mappings.
- Diagnostics for mismatched tags and best-effort recovery during parsing.
- Minimal external dependency:
Markdig.
Architecture overview
The implementation separates parsing, conversion, rendering, and configuration into small, testable components.
Parser layer
Markdig(Markdown → AST): yieldsBlockandInlinenodes.- BBCode Tokenizer + Parser: converts raw BBCode into a token stream and then into
BBCodeElementinstances.
Conversion / Renderer layer
- Markdown → BBCode
BBCodeRendererclasses traverse Markdig nodes and emit BBCode fragments.- Each Markdig node type maps to a renderer class responsible for escaping, attributes, and tag emission.
- BBCode → Markdown
BBCodeParsertokenizes input and maps tokens to concreteBBCodeElementsubclasses (for example,BoldElement,UrlElement,CodeBlockElement).- An element visitor / renderer converts
BBCodeElementinstances into Markdown text. A stack-based nesting handler ensures correct open/close semantics and enables recovery from mismatches.
- Markdown → BBCode
Core models & infrastructure
BBCodeElement(base) with well-named subclasses.RenderingContextto configure rules (heading size mapping, code-language handling, link strategies).- Tokenizer (single consolidated regex for initial tokenization) and a stack-based nest handler for robust parsing.
ConversionResultencapsulatesOutputandDiagnosticsfor consumers.
Design goals
- Deterministic outputs for identical inputs.
- Easy extensibility: add a new tag by implementing one small element class and registering its renderer.
- Thorough test coverage: unit tests for individual renderers/parsers and integration tests for round-trip stability.
Extensibility & integration points
- Add tags: implement a
BBCodeElementsubclass and register a renderer in theElementFactory. - Customize Markdown rendering: subclass
BBCodeRendererand override node renderers. - Configure mappings in
RenderingContext(e.g., mapH1to[size=24]vs[b][size=18]). - Surface
ConversionResult.Diagnosticsto present parsing warnings/errors in host applications.
Error handling & edge cases
- Unknown tokens are emitted as plain text nodes — no silent data loss.
- Mismatched tags: stack handler attempts best-effort recovery and populates diagnostics.
- Ambiguous nesting and mixed inline/block content resolve deterministically per stack rules; tests cover representative scenarios.
Usage examples
- Basic: Markdown → BBCode (Rendering)
string markdown = "# Project Status\n\n- Completed tasks.";
string bbCode = BBCodeRenderer.ToBBCode(markdown);
// renderer output: "[b][size=18]Project Status[/size][/b]\n\n[list]\n[*]Completed tasks.\n[/list]"
- Basic: BBCode → Markdown (Parsing)
string bbCode = "[b]Final Report[/b][hr]Done.";
string markdown = BBCodeParser.ToMarkdown(bbCode);
// parser output: "**Final Report**\n---\nDone."
- ASP.NET Core endpoint (quick integration)
[HttpPost("/convert")]
public IActionResult Convert([FromBody] ConvertRequest req)
{
var converted = req.Format == "md-to-BB" ? MarkdownToBBCodeConverter.Convert(req.Content) : BBCodeParser.ToMarkdown(req.Content);
}
- Batch migration script (archive migration)
foreach (var file in Directory.EnumerateFiles("archives", "*.BBcode"))
{
var BB = File.ReadAllText(file);
var md = BBCodeParser.ToMarkdown(BB);
File.WriteAllText(Path.ChangeExtension(file, ".md"), md);
}
- CLI / Tooling
- Use converters in CI or as a
dotnettool to migrate content in bulk or to validate round-trip stability.
- Extending with a custom tag (concept)
- Implement
CustomElement : BBCodeElement. - Implement
CustomElementRendererto produce the desired Markdown or BBCode according toRenderingContext. - Register the element/renderer pair in
ElementFactoryand add tests proving round-trip behavior.
Testing & contributing
- Add unit tests for new parser/renderer behavior and integration tests that verify round-trip stability.
- Follow the project's
CONTRIBUTING.mdand.editorconfigfor formatting, testing, and branching rules.
Notes
- Target: .NET 8.
- Intended to be embeddable in libraries, web APIs, migration tools, and forum engines.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Markdig (>= 0.44.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.