Result objects
A conversion gives you more than Markdown. You also get the tree it was written from, the tags the parser did not know, and notes about what the parser had to repair. For that reason Markbridge.*_to_markdown returns a Conversion object, not a String, and Markbridge.parse_* returns a Parse.
Conversion
Section titled “Conversion”Markbridge.*_to_markdown and Markbridge.convert return a Markbridge::Conversion:
result = Markbridge.bbcode_to_markdown(post.body, renderer: RENDERER)
result.markdown # the rendered Discourse Markdownresult.ast # the AST::Document used for renderingresult.format # :bbcode, :html, :text_formatter_xml, or :mediawikiresult.unknown_tags # Hash{String => Integer} — tag name to countresult.diagnostics # parser-specific diagnostics (e.g. auto-close counts)result.errors # render-time errors, when raise_on_error: falseConversion#to_s returns the Markdown, so puts result and "#{result}" work. Other String methods are not there: result.gsub(...) raises. Call result.markdown.gsub(...).
Markbridge.convert(input, format:) returns the same Conversion. Use it when the format is only known at run time, for example when your posts are in more than one format:
posts.each do |post| result = Markbridge.convert(post[:body], format: post[:format], renderer: RENDERER) # use result.markdown…endEvery *_to_markdown method has a parse_* method that stops after parsing. It returns a Markbridge::Parse: what a Conversion has, without markdown and without errors, which only rendering can have.
parse = Markbridge.parse_bbcode(input)
parse.ast # the AST::Documentparse.format # :bbcodeparse.unknown_tags # Hash{String => Integer}parse.diagnostics # parser-specific diagnosticsUse parse_* to look at the tree or to change it before rendering. Markbridge.render(parse, renderer:) renders a Parse and returns a Conversion, with the format, unknown_tags and diagnostics of the parse.
What the fields mean
Section titled “What the fields mean”unknown_tagshas the tags no handler was registered for, with a count each. The parser does not raise on them. Whether a post with unknown tags is fine, gets logged, or fails is your decision.diagnosticshas what the parser had to repair. The BBCode parser reports how many tags it closed itself (auto_closed_tags_count), how many it left as text because they were nested too deep (depth_exceeded_count), and code tags that were never closed (unclosed_raw_tags). The key:normalizationlists what the normalizer rewrote. It is missing when nothing changed.errorshas the errors of rendering when you passraise_on_error: false. Without that option an error is raised and the list stays empty. In a migration, expectMarkbridge::UndecidedFeatureError: a post has a color or a size, and the renderer was not told whether the site has the discourse-bbcode plugin (seefeatures:).